Ember-intl: Get current locale value (string or array?)

Created on 19 Nov 2018  路  8Comments  路  Source: ember-intl/ember-intl

  • [yes] I am on the latest ember-intl version
  • [yes] I have searched the issues of this repo and believe that this is not a duplicate

Environment

  • Ember Version: 3.5.0
  • Ember CLI Version: 3.5.0
  • Ember Intl Version: latest
  • Browser(s): chromium v68
  • Node Version: 8.11.3

I need the current locale at different instances in my application, to get the current locale I use:
this.get('intl').get('locale')

This always returns an array with a single string value, is there a way to just return te value instead of an array with a single element?

My value was set using
this.get('intl').setLocale('en')

How do I get a return of "en" instead of ["en"] ?

I was searching the docs when I noticed this:

Locale

Set/get the current locale for your application. The value can either be a String or an Array of Strings.

But couldn't find more info about when which return is given.

Thanks in advance

question

All 8 comments

You can do so with a computed property:

firstLocale: computed('intl.locale', function() {
  return this.get('intl.locale')[0];
})

Why doesn't this.get('intl.locale') just return the locale string? Currently, everytime you want to just get the active locale, you need to write a computed property (controllers, components, services, ...).

Because setLocale accepts an array of locales in order to support fallbacks. This is the reason, why there is no "one" locale. There is only a primary locale and a number of fallback locales.

You could access this.get('intl.locale.firstObject').

We could also think about adding a primaryLocale to the intl service for convenience.

@buschtoens With primaryLocale, do you mean the currently Active locale or the primary locale in the locales[].

If it's the current Active locale, that would be very helpful!

@Driezzz With primaryLocale I mean the first locale of the currently active locales, i.e. this.get('intl.locale.firstObject').

The locales array in config/ember-intl.js is really just a set, not an ordered array. There is no primary locale. 鈽猴笍

I know this is already closed but it'd just like to say that it'd be really cool to have the primaryLocale alias to intl.locales.firstObject in the intl service.

As I'm migrating from ember-i18n to ember-intl, I'll have to replace i18n.locale with intl.locale.firstObject (which can be confusing for some people as locale is singular but represents an array) everywhere. With the primaryLocale everything would be better :)

I have no objections to adding such a primaryLocale alias. It's likely most useful to devs who do not use the fallback mechanism, but could be confusing, if you use it. With proper documentation I would gladly accept such a PR. 馃槉

@buschtoens I've opened a PR adding primaryLocale, documentation about it, and a test 馃槉

Was this page helpful?
0 / 5 - 0 ratings