Multi-lingual support (translated fields) #7323
Replies: 32 comments 8 replies
-
Related conversations in KS 4: |
Beta Was this translation helpful? Give feedback.
-
Here is how I did it on a recent KS 4 project with multiple language and field text search. locales.js
Helper_StandardContent.js
ShopProduct.js
Product.js
|
Beta Was this translation helpful? Give feedback.
-
It looks like you haven't had a response in over 3 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contributions. :) |
Beta Was this translation helpful? Give feedback.
-
I have been researching this idea for some time now and I have a proposal. Firstly, in terms of releasing, I think it will be easier to do this in two steps: Localization for the Core/API and Localization for the Admin. Now, let's get into it. Localization CoreInitially, we need a way to define localization for "static messages." Things like validation messages, field labels, etc. So, I suggest adding a const keystone = new Keystone({
name: 'New Project',
adapter: new MongooseAdapter(),
locales: [localeEnUs(), localeEnUk(), localeDeutsch(), ...]
}); Locale functionsAs you can see, these locales do not store string value. They are functions with specific values (I have not thought of the implementation details but just the design): // I am using momentjs formatting here but it really doesn't matter
const localeEnUs = custom => ({
locale: 'en-us',
languageCode: 'en',
languageInNative: 'English',
defaultLongDateFormat: 'dddd, MMM Do YYYY',
defaultShortDateFormat: 'MM/DD/YYYY',
messages: {
api: {
'inbox.available.one': 'You have 1 item in your inbox',
'inbox.available.many': 'You have %d items in your inbox',
},
admin: {
...
},
...custom
}
}); This way, all the language information is contained in a single object. If a translation does not exist in a specific language, default translation will be used. Installing Locales via NPMSecondly, this type of containment with objects has an advantage. Since, a lot of messages are based on internal values (validation errors, admin translations), these messages should be provided by Keystone. Since, loading all locales can increase the bundle size significantly, we can separate them into separate NPM packages: yarn add @keystonejs/locale-en-us @keystonejs/locale-de-de
# or
yarn add @keystonejs-locales/en-us @keystonejs-locales/de-de Then store these locales in a separate monorepo (let's call it Custom translation messagesWhat if we need custom translation messages that is available to our frontend app or to a custom controller? We can just pass a JS object to the locale function to have custom variables available. Example: const customDeDe = require('./locales/de-de.json');
const customEnUk = require('@my-own-project/localizations');
const customEnUs = { ... };
const keystone = new Keystone({
name: 'New Project',
adapter: new MongooseAdapter(),
locales: [localeEnUs(customEnUs), localeEnUk(customEnUk), localeDeutsch(customDeDe), ...]
}); This allows for developer do whatever they want with their locales: they can store them in the same file as Keystone instance, store them in a separate module or just a JSON file, or publish their own localization module and import the object. Sky is the limit as long as an object is passed. Using translation stringsIn keystone, we provide a simple "translate" function. Typically, these functions have a very short name. For example, we can call it const myNumber = getNumber();
if (myNumber === 1) {
const msg = keystone.t('api', 'inbox.available.one', 'You have 1 item in your inbox');
} else if (myNumber > 1) {
const msg = keystone.t('api', 'inbox.available.many', 'You have %d items in your inbox', [myNumber]);
} Default value is implemented during function call. This way, if there is no value in the messages object for the current language, default one will be used. If you want to print custom values, you just need to use the domain name and the key of the custom message: const msg = keystone.t('myCustomModule', 'hello.world', 'Hello World); MigrationWe need to use APINow, there needs to be a way for apps to request for two things based on locale: Localized messages and localized field values (we will discuss this one later). Language DetectorThere are multiple ways to detect languages in APIs: Cookies, query strings, const keystone = new Keystone({
name: 'New Project',
adapter: new MongooseAdapter(),
locales: [localeEnUs(), localeEnUk(), localeDe(), ...],
defaultLocale: 'de-de' // default is always the first item in the `locales` array
}); Setting languageOnce the language is detected, we can store it in request context; thus, use the active language withing Getting language info (if needed)We can also add a field in GraphQL to retrieve locale info and messages: {
localeInfo {
nativeName
code
locale
messages {
admin {
key
value
}
}
}
} Admin LocalizationAdmin localization consists of couple of parts: Fields, View localization, and Language Switcher. FieldsTo make fields translatable, I suggest adding a Localization field that accepts a const { Text, Localized } = require('@keystonejs/fields');
keystone.createList('Post', {
fields: {
title: { type: Localized, field: Text },
},
}); This field uses implementation of an existing field and adds localized functionality to it. This functionality consists of storing field values in a localized manner, getting localized and all versions of the field (needed for admin), and showing localized field in admin. DatabaseFor Mongoose adapter, I suggest storing field in the following way: {
"field": {
"en-us": "...",
"en-uk": "..."
}
} For SQL adapter, I suggesting storing the field in a related table:
Retrieving all localized fields from GQLWe need a way to get these values from GQL. When dealing with GQL, we can add an additional field that is only available for admins (through Access Control). Example: {
posts {
titleLocalized {
locale
value
}
}
} Viewing Localized FieldsI think the easiest way to manage views for a localized field is to add Tabs on top of the field. Each tab shows the locale code the the fields are shown within the tab. When preparing the request for the view, a schema like this will be created: {
"en-us": "...",
"de-de": "..."
} However, view code has one minor issue. Labels for each input field is always visible. I suggest hiding them (screen reader friendly) when they localized and change the labels by adding localization in parenthesis. Example (not using <h4 className="label">Title</h4> {/* Visible Label */}
<Tab name="English">
<label className="sr-only" htmlFor="title-en-us">Title (English)</label
<input type="text" id="title-en-us" ... />
</Tab>
<Tab name="Deutsche">
<label className="sr-only" htmlFor="title-de-de">Title (Deutsche)</label>
<input type="text" id="title-de-de" ... />
</Tab> View LocalizationWe might need a way to localize, not just custom fields but the entire template (all the texts etc). I have an idea to do this in an optimized fashion. Firstly, we add a field to GQL that shows a "hash" of the all localizations. This value is generated only and stored once -- when the server is initialized and in keystone instance. Let's call this field a "localizationHash." This field can be used by all static apps. When admin app is opened, "localizationHash" is requested from the API. Then, this hash is compared with a That's AllI know it is a long post but I wanted to suggest my proposal in detail here. If you have any questions on this or don't like something, let me know. Once a specific design is agreed upon, I am willing to implement this functionality step-by-step. Future and Other ConsiderationOnce there is a localization system in place, we can use Access Controls for localized content. Developer should be able to restrict certain locales of certain fields. For example, developers should be possible for restrict a French content creator to only view and update fields that are for French localization. |
Beta Was this translation helpful? Give feedback.
-
Any development on this!? |
Beta Was this translation helpful? Give feedback.
-
I haven’t done anything on this because I wrote this as a proposal to be discussed. This is a big change to the framework; so, I wrote to this to get to an agreement before going into implementation. |
Beta Was this translation helpful? Give feedback.
-
Enthusiastically support @GasimGasimzada's idea as I'm going to have to bootstrap a multilingual support for my next project and I don't think my solution will be as good. |
Beta Was this translation helpful? Give feedback.
-
Any update on the proposal status? @GasimGasimzada |
Beta Was this translation helpful? Give feedback.
-
I'd be happy to help out as well. |
Beta Was this translation helpful? Give feedback.
-
It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :) |
Beta Was this translation helpful? Give feedback.
-
I could use this on a project I'm working on, it sounds like there's some enthusiasm for the proposal. @GasimGasimzada - it's been a while since you originally proposed this, would you still be willing to lead? I would also be willing to contribute where I can. I think this is a pretty worthwhile addition and I'll wind up doing something less complete for the project I'm currently working on if this doesn't happen, so I'd much rather contribute the hours that I'm going to spend on the problem to the implementation of this proposal as well. Any comments from the maintainers? Any reason why it's a bad idea for us to start working on this? |
Beta Was this translation helpful? Give feedback.
-
It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :) |
Beta Was this translation helpful? Give feedback.
-
Is there any update on this? Are you guys planning to do something about this or will KeystoneJS remain a one language system? Because in that case I'll need to (sadly) look somewhere else :( |
Beta Was this translation helpful? Give feedback.
-
Just to bump this and agree – it is only now dawning on me that Keystone seems to be lacking a very basic feature that IMO will sadly make it a no starter for many people. Hoping I have missed something and that there is a workaround! |
Beta Was this translation helpful? Give feedback.
-
This is definitely on our roadmap, and we're planning to come out with something in the second half of this year (2021) To put that in context, we're in the middle of working through some core updates to our GraphQL system, which will unblock a lot of higher level features including translation support, and other oft-requested features like singleton list types and orderable items in lists. The GraphQL system updates are expected to land in about a month (late June). A few people have asked how to prepare for translations ahead of built-in support, so while we have some design to do on how the feature will actually be implemented, here's the current thinking: You'll configure translations for the system, then enable them on a list/field basis. Behind the scenes, fields will be duplicated so for a system with
In the Admin UI, we'd have a select / tabs UI to let you switch between the different translations in the Edit Item form, and other UI for managing translations generally. We'll probably look at ways to make querying for a translation through the GraphQL API easier too. This is the least well defined part of the plan at the moment, but I can imagine it looking something like this:
... and that would select the values from the German translation for the translated fields. Right now, you can build (most of) this manually but we know that proper support will make it much easier to build multilingual apps with Keystone. If you do want to go with a workaround, it's pretty easy to abstract this with a function when you're defining lists in Keystone Next:
I also want to shout out to @GasimGasimzada for a great and thorough write-up above, we'll be referring to that when we get to the design stage for this and flesh out the idea I outlined above properly. If anybody else has other ideas or requirements to add here, that would also be really helpful for when we get to building this in the coming months. |
Beta Was this translation helpful? Give feedback.
-
@JedWatson that is great news, thank you! To me everything you described makes sense for my use cases. I think indeed having the ability to specify the language as part of the query would be super useful. I would also mention that any multilingual website that cares about SEO will also need slugs to be translatable. But I guess following your example above it would probably just mean adding several slug fields (slug_fr, slug_de, etc). |
Beta Was this translation helpful? Give feedback.
-
@vonba you're right about the slug field, was reaching for an example but something like a @gautamsi I'm expecting this would work with any field type; text, document, select, json etc. Is that what you meant? |
Beta Was this translation helpful? Give feedback.
-
@JedWatson Something like an e-commerce site might also want to display different images or other media for different locales/countries. I don't know if this is something that you would want to specifically want the API to deal with, but I think it's something that would come up. |
Beta Was this translation helpful? Give feedback.
-
@JedWatson I am selectively interested in automatic translation of document fields given block and json structure. |
Beta Was this translation helpful? Give feedback.
-
Same, I'm moving away from WordPress and I've tried Strapi (which does have native l10n support), but I found its admin UI too limited for my uses. I'm really hoping there's a strong focus on l10n in KeystoneJS 6 as it's otherwise much better than Strapi for my use case. As a web developer in Quebec, no l10n is absolutely a no-go unless your client wants fines up the ass from the provincial government or wants to serve a Quebec-only, french-speaking only client base. |
Beta Was this translation helpful? Give feedback.
-
Do we have a timeline for this? Any predictions when translations might be available? :) |
Beta Was this translation helpful? Give feedback.
-
Just a single data point: Not having a translation system built-in, and not having any solution that's ready to use, unfortunately disqualifies Keystone for us. We'd very much like to switch to a more modern CMS, but having to implement our own translation system before even being able to get started is a no-no. Gonna check back in a couple of years, I guess … |
Beta Was this translation helpful? Give feedback.
-
FWIW, here are the absolutely necessary features I can think of:
And a nice-to-have:
|
Beta Was this translation helpful? Give feedback.
-
Awesome that this is on the roadmap!! This is the only reason why we haven't made the switch to Keystone yet, and we would really love to switch because the DX and workflow are so great! For our use case we would need to support not just language codes like The approach you suggested for querying @JedWatson sounds ideal to me. This is also what we (and probably many others) are used to from the likes of Contentful and Strapi and it works very well in my opinion.
A super handy nice-to-have might be to mark a field to fall back to the default locale when empty. This way, you could for example provide the title of your post in all languages, while only supplying the image for the default locale, saving editors a bunch of time. I would also like to 2nd @acerspyro's suggestion of locale-aware access control. I think we would be able to code that using the existing access control API without any need for changes, but just wanted to mention that it's a use-case we face quite often. |
Beta Was this translation helpful? Give feedback.
-
any progress on this?? |
Beta Was this translation helpful? Give feedback.
-
I also think multilingual support is fundamental and should be a top priority. Not having multilingual support means the CMS cannot be used for a lot of projects and many developers will have to look for another solution, although they really like the system in general. There seems to be a lack of awareness of internationalization in projects (not only open source) developed and started in mainly english-speaking regions. Examples are WordPress, Webflow, shopify. Whereas projects started in non-english speaking countries like France, Germany, always implement strong multilingual support from the very beginning e.g. strapi, Contentful, neos.io It seems like it gets harder and harder to add real multilingual support the longer a project exists and is developed (Looking at WordPress and webflow, where it has been promised for years, and probably will take another few years, probably because it wasn't thought of from the beginning) So, the sooner the better, I think. |
Beta Was this translation helpful? Give feedback.
-
Any progress ? i also think that translatable fields are a must, we are gonna implement our own system for now but a in built system with graphql integration like in Payload CMS would be really nice to have. I think Its a pretty basic feature that every CMS should have. |
Beta Was this translation helpful? Give feedback.
-
This is my branch, which supports administration page in other languages at compile time Before the official support of i18n , the project mainly adds the ability to display other languages to some text on the admin page |
Beta Was this translation helpful? Give feedback.
-
FYI: Strapi addresses this issue by creating separate records for each language. On the roadmap, Nested Fields has higher priority than multilingual support. When Nested Fields supported, we may be able to realize multilingual support by replacing text(translated) field by a simple component including multi-language text. e.g. const lists = {
TranslatedText: nested({
fields: {
en: text(),
ja: text(),
},
}),
}; |
Beta Was this translation helpful? Give feedback.
-
I'll probably be going with payload because of this.. |
Beta Was this translation helpful? Give feedback.
-
For supporting multilingual sites and apps, it will help if Keystone itself has some concept of multiple languages. The conversation around this so far is based on a few ideas, namely:
This starts to take the shape of a technical design in which:
blahBody
field has no German translation.)This feature likely interacts with:
We should examine how these ideas are managed in other, related systems like Contentful.
Beta Was this translation helpful? Give feedback.
All reactions