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

List loaded locales #2932

Closed
wants to merge 2 commits into from
Closed

List loaded locales #2932

wants to merge 2 commits into from

Conversation

jisaacks
Copy link
Contributor

Fixes #843

This adds the method moment.locales() to get an array of the locales that have been loaded (ie, bundled with moment.js or manually required in node.)

It does not list or require available (but unloaded) locales in node. I think that should be a separate feature if needed at all.

@mattjohnsonpint
Copy link
Contributor

LGTM. Staging for next release. Thanks!

@@ -152,6 +152,10 @@ test('defineLocale', function (assert) {
assert.equal(moment().locale('dude').locale(), 'dude', 'defineLocale defines a locale');
});

test('locales', function (assert) {
assert.equal(moment.locales().length > 0, true, 'locales returns an array of defined locales');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to see a smarter test here. Test for content, at least for English, and perhaps include one custom locale (like dude shown in the test above)

@ichernev
Copy link
Contributor

ichernev commented Mar 6, 2016

Merged in c0519b5

@ichernev ichernev closed this Mar 6, 2016
ichernev added a commit that referenced this pull request Mar 6, 2016
@Silviu-Marian
Copy link

Should this be in the docs too? I'm asking because I wasn't able to find any references to it

@fracz
Copy link

fracz commented Aug 25, 2017

The method is

let locales = moment.locales(); // ['en', 'ru', 'pl']

@gilles6
Copy link

gilles6 commented Sep 21, 2019

For me, it only returns ["en"], not the list of available locales. How to return such list ?

@mhornbacher
Copy link

@devondahon when you try yo change the locale with moment.locale() does it work?

It could be that your other locales are not importing properly....

@anhkhoi
Copy link

anhkhoi commented Nov 5, 2019

The locales() function just return ["en"]. It doesn't work for me. Any one know the reason?

@mhornbacher
Copy link

@anhkhoi how are you loading your secondary locales?

@StefansArya
Copy link

@mhornbacher
Copy link

@StefansArya nice script! Is something broken with it?

@StefansArya
Copy link

@mhornbacher I just tested on jsbin and working as expected. You can use any locale format like
ja-JP, ja_JP, or ja

It also has additional fallback, for the example ja-JP was not found on the list and it will try to strip the -JP and use ja instead.

The first argument on the callback will return ja if it was successfully loaded, and false if language was not found.

If you call loadLocale more than once with same locale name, it wouldn't not do any HTTP request. But will call moment.locale() directly and the given callback.

Also tested and working on IE11.

@tonix-tuft
Copy link

I think that the listing of all the supported/available locales (even if there were not loaded yet) should be included in the moment core itself, and not hardcoded in the client code, what do you think?

It would be nice to have a method e.g. like .allSupportedLocales():

import moment from "moment";

console.log(moment.allSupportedLocales()); // ["en", "it", "de", ..., "ja", ...]

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

Successfully merging this pull request may close these issues.

Get a list of available locales
10 participants