Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Get the translation from another locale #419

Closed
pohodnya opened this issue Nov 24, 2016 · 4 comments
Closed

Get the translation from another locale #419

pohodnya opened this issue Nov 24, 2016 · 4 comments

Comments

@pohodnya
Copy link

Now it looks like this

currentLocale = @get('i18n.locale')
@set('i18n.locale', anotherLocale)
result = @get('i18n').t(key)
@set('i18n.locale', currentLocale)

Is it possible to specify a locale?

result = @get('i18n').t(key, { locale: anotherLocale })
@jamesarosen
Copy link
Owner

#381 reserves locale (and htmlSafe) as special keys in v4.x so that in v5.0 we can support this, be we haven't finished that migration. So it's planned, but I don't know when it will happen.

@nsharrok
Copy link

nsharrok commented Jan 10, 2017

Yes please. I would like this very much too.
I want to create an edit page where the user can select and edit the translation of a product name for all the supported locals in a single web form.
I've tried the above approach but that interfered with the rendering process.
I hacked the i18n t function adding loc variable. Works for me hack wise

t(key, data = {}, loc) {
...
const locale = loc ? new Locale(loc, getOwner(this)) : this.get('_locale');

Do you know of an official hack I could use in the meantime?

@Fl0rianFischer
Copy link

Fl0rianFischer commented Apr 27, 2018

If anyone wonders how to do this until it's officially implemented:

We wrote our own little service that would fetch new translation files and created our own translate method that closely resembles Ember.I18n ones (not sure why it doesn't render properly..):

`import compileTemplate from 'ember-i18n/utils/i18n/compile-template';

t(key, data = {}, code) {
const translation = this.get('translations.${code}');
Ember.assert('Translations for code ${code} not loaded', !!translation);

const resolvePath = (path, object) => {
  return path.split('.').reduce((prev, curr) => {
    return prev ? prev[curr] : undefined;
  }, object);
};

const template = resolvePath(key, translation);
if (!template) return 'Missing translations for ${key}';

const compiled = compileTemplate(template)(data);
return compiled;

}`

@jamesarosen
Copy link
Owner

jamesarosen/ember-i18n has been deprecated in favor of ember-intl.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants