-
Notifications
You must be signed in to change notification settings - Fork 183
Add support for translation fallback #256
Comments
Would you mind writing this up more fully in RFC format? Some questions you might want to address:
|
I thought this was about having a locale outside that Language Tag at the root (e.g. |
Ah, I totally missed the point that t already supports the fallback. My first intention indeed was to support a language outside the chain. In my current project for example 'en' is the fallback no matter which language is used. I'm not sure though if support for this is needed in ember-i18n. If you think it would be nice I will change the descr. above. |
I'm going to make sure the docs clearly say that fallbacks within the language tag are supported. |
The docs didn't have it. Here you go: https://github.com/jamesarosen/ember-i18n/wiki/Doc:-Defining-Translations#location-fallback |
If that's all you needed, feel free to close this issue. If you still want to pursue letting an app put an arbitrary locale at the root of the chain, I'd love to hear more. #262 may be relevant to that discussion. |
I would certainly love to have that functionality. I sometimes have to wait for the Chinese translations to our app, and would prefer to simply have a fallback functionality in these cases. |
Alright I will update the issue description than. |
Yes I too have a same requirement as @franzliedke, even in our case, we have a default locale to override the keys if the same is missing in other language is missing. That is, lets have two different languages say A and B. And out which A is set to be default language. Now the user logs in to the system and has B as his language and in a page there seems to be a key which does not present in B and for the same need to return the value from the default language. considering the default translation file will have all the keys |
Given the current implementation, which limits translations to a single object per locale (e.g. Let's assume you develop in English and that locale file is always fully populated. You could then do // app/locales/es/translations.js:
import Ember from "ember";
import en from "../en/translations";
export default Ember.merge(Ember.copy(en), {
"some.string": "Spanish override"
}); That way, Of course, RFC #255 complicates that since it proposes multiple files per locale. |
Being able to specify fallback languages would be great! We're currently using the work-around you suggested (thanks James!), but native support for (related, having an event fired when fallback was triggered would make it easy to get notified about missing translations, while still showing something useful to the end-user) |
👍 for this. I was quite surprised that it didn't do fallback out of the box yet, especially because you're supposed to set |
This will likely be among the next things I work on for this project, but I don't know when that will be. |
+1 |
Resolving the locale via Java has the concept of a root locale. Maybe adopting something similar would simplify the fallback chain and remain unopinionated about what the default locale should be? |
I definitely recommend trying |
For anyone with this issue, you can patch something together with missing-message: // FILE AT app/utils/i18n/missing-message.js
//
// NOTE if ember-i18n add support for fallback locales we won't need this
// https://github.com/jamesarosen/ember-i18n/issues/256
import Ember from 'ember';
import Locale from 'ember-i18n/utils/locale';
const FALLBACK_LOCALE = 'en';
let missingMessage = function(locale, key, data) {
if (locale === FALLBACK_LOCALE || window.env === 'development') {
return `Missing translation: ${key}`;
} else {
// NOTE This relies on internal APIs and is brittle.
// Emulating the internals of ember-i18n's translate method.
let i18n = this;
let count = Ember.get(data, 'count');
let defaults = Ember.makeArray(Ember.get(data, 'default'));
defaults.unshift(key);
let localeObj = new Locale(FALLBACK_LOCALE, Ember.getOwner(i18n));
let template = localeObj.getCompiledTemplate(defaults, count);
return template(data); // english fallback
}
};
export default missingMessage; |
@sandstrom, a small fix of your patch: // FILE AT app/utils/i18n/missing-message.js
//
// NOTE if ember-i18n add support for fallback locales we won't need this
// https://github.com/jamesarosen/ember-i18n/issues/256
import Ember from 'ember';
import Locale from 'ember-i18n/utils/locale';
const FALLBACK_LOCALE = 'en';
let missingMessage = function(locale, key, data) {
if (locale === FALLBACK_LOCALE || window.env === 'development') {
return `Missing translation: ${key}`;
} else {
// NOTE This relies on internal APIs and is brittle.
// Emulating the internals of ember-i18n's translate method.
let i18n = this;
let count = Ember.get(data, 'count');
let defaults = Ember.makeArray(Ember.get(data, 'default'));
defaults.unshift(key);
let localeObj = new Locale(FALLBACK_LOCALE, Ember.getOwner(i18n));
let template = localeObj.getCompiledTemplate(defaults, count);
return template(data); // english fallback
}
};
export default missingMessage; Without imports I have error: Ember/Locale is undefined |
@sandstrom thanks for the patch! @jamesarosen any updates on this issue? I'm willing to take a stab at building it into the library if nothing's already been started. |
@mohanyin afaik nothing has been started, feel free to make a stab at it 😄 |
jamesarosen/ember-i18n has been deprecated in favor of ember-intl. |
Problem
Currently if a user has multiple locale translations defined, it has to define every translation key for each locale, even if some translations may be the same in a locale chain (i.e. en_US -> en).
Goal
Add the ability to omit translations keys in translations which are or may be defined in other translations files within the same locale chain.
Proposal
When a translation is being looked up via
t('somekey')
and the result is undefined, we need to track down if the translation is defined in one of the other translations in the current locale chain. This means that we have to lookup the current locale chain and see if there exists a translation in one of the parent locales (i.e. when a translation is not defined in 'en_US', we have to look if the translation exists in 'en', if so we return that translation).Some key notes:
Considerations
It would be possible to let the defaultLocale be the endpoint of the chain, so if the translation cannot be found in the current locale chain, we will finally fallback to the defaultLocale. I'm not sure yet if this is desirable.
If there are plans to implement this, the defaultLocale defined in the ENV config should be stored and never be changed when setting the locale property.
Alternative Approach
An alternative approach would be to merge all translations within one locale chain. This requires to merge translations when initializing the translations and also when new translations are being added via
addTranslations
.Some key notes:
Drawbacks:
The text was updated successfully, but these errors were encountered: