Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the detected locale part #4

Open
sebastien-p opened this issue Oct 19, 2012 · 2 comments
Open

Expose the detected locale part #4

sebastien-p opened this issue Oct 19, 2012 · 2 comments

Comments

@sebastien-p
Copy link

Let's say I want to write a requirejs plugin which would be based on i18n but including some more features like :

  • Dynamic generation of an array of all supported languages for a given bundle ([{ code: "en", name: "English", root: true, current: false }, { code: "fr", name: "Français", current: true }, ...])
  • ...

Why ? Mainly in order to help providing a language picker (select menu, flags or whatever) in my applications.

Generating the array is easy but knowing which locale part has been detected by the i18n plugin is not currently possible because the value isn't exposed somehow.

In addition to making my life easier, I think appending that functionality to the official i18n requirejs plugin could be useful to other plugin developers too.

Adding something like value._currentLocale = needed.slice(-1)[0] just before onLoad(value) (line 175) does the trick but I'm pretty sure one can find a better solution.

Hopefully my english wasn't too bad and you get the point of this "issue". Thanks.

@jrburke
Copy link
Member

jrburke commented Oct 23, 2012

Sorry for the delayed response -- I have not had to use the i18n plugin for a while, and I think there may be better solutions out there, perhaps like https://github.com/SlexAxton/messageformat.js.

So, is it that you want to get the locale that i18n has detected, around this line?
https://github.com/requirejs/i18n/blob/master/i18n.js#L123

If so, then I can see exposing a "getLocale()" function on the i18n module that would do that work (or used the cached value), then to get it, use i18n as a module and call that method. Is that enough?

Or if you want to know what would be the first thing in the needed array on a per-load call to i18n, maybe that should be extracted as a function that maybe takes a callback?

I'm a bit hesitant to add it to the value itself though, as it modifies the value as defined in the i18n modules.

@sebastien-p
Copy link
Author

I like the i18n plugin because how simple it is to use and because of its lightweight footprint.

Your first suggestion will pretty much always return (navigator.language || "root).toLowerCase()" which is not what I need.

For example, if navigator.language === "en-us" and require("nls/lang") === { root: { ... }, en: true }, the first matching part would be "en".

Or if you want to know what would be the first thing in the needed array on a per-load call to i18n, maybe that should be extracted as a function that maybe takes a callback?

I think this is what I need.

I'm a bit hesitant to add it to the value itself though, as it modifies the value as defined in the i18n modules.

I agree, this was just the easiest way to do it. I'm sure one can figure a better way though (callback-style or not, I don't know).

Thank you.

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

No branches or pull requests

2 participants