kgish
October 10, 2015, 8:29pm
1
I’ve been using the ember-cli-i18n
addon and it works great.
One limitation of this method however is that it’s client side only.
Instead, I’d like to be able to pull a given locale from a backend database through a rails api.
That way I can provide a crud dashboard for translators who can add multiple languages for certain entries or whatever.
On the client I’d continue using the ember-cli-i18n
templating stuff {{t 'entry.title'}}
as before so that wouldn’t change.
Anyone have ideas about the best way of doing this?
ember-cli-i18n is deprecated, you should be on ember-i18n. That said, both ember-intl and ember-i18n have the ability to load translations during runtime. So, on app boot you can fetch your translations and register them.
ember-i18n hook:
https://github.com/jamesarosen/ember-i18n/blob/master/addon/services/i18n.js#L52-L59
ember-intl hook:
And here is an example of me pushing down translations in my initial app payload:
It reads the yaml file, writes to a meta tag…
next();
});
});
app.use(function writeIntlStateToDom(req, res, next) {
if (req.url === '/ember-cli-live-reload.js') {
return next();
}
fs.readFile(DEFAULT_INDEX, { encoding: 'utf8'}, function(err, data) {
const $ = cheerio.load(data);
const $head = $('head');
$head.append(meta('translations', serialize(res.locals.translations)));
$head.append(meta('locale', res.locals.locale.toLowerCase()));
During initialization, I read that meta tag and call ember-i18n’s hook as described above…
let locale = Ember.$('meta[name=locale]').attr('content');
let translations = Ember.$('meta[name=translations]').attr('content');
if (locale && translations) {
let i18n = instance.container.lookup('service:i18n');
i18n.addTranslations(locale, JSON.parse(translations));
i18n.set('locale', locale);
}
}
And lastly, a wiki article on discussing this topic Example: Fetching Translations Live · jamesarosen/ember-i18n Wiki · GitHub
1 Like
kgish
October 11, 2015, 1:14pm
3
Sorry, my bad. I am using ember-i18n
and not the deprecated version.
This is all great stuff, thanks!