1 import isArray from '../utils/is-array';
2 import isUndefined from '../utils/is-undefined';
3 import compareArrays from '../utils/compare-arrays';
4 import { deprecateSimple } from '../utils/deprecate';
5 import { mergeConfigs } from './set';
6 import { Locale } from './constructor';
7 import keys from '../utils/keys';
9 // internal storage for locale config files
13 function normalizeLocale(key) {
14 return key ? key.toLowerCase().replace('_', '-') : key;
17 // pick the locale from the array
18 // try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
19 // substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
20 function chooseLocale(names) {
21 var i = 0, j, next, locale, split;
23 while (i < names.length) {
24 split = normalizeLocale(names[i]).split('-');
26 next = normalizeLocale(names[i + 1]);
27 next = next ? next.split('-') : null;
29 locale = loadLocale(split.slice(0, j).join('-'));
33 if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
34 //the next array item is better than a shallower substring of this one
44 function loadLocale(name) {
46 // TODO: Find a better way to register and load all the locales in Node
47 if (!locales[name] && (typeof module !== 'undefined') &&
48 module && module.exports) {
50 oldLocale = globalLocale._abbr;
51 require('./locale/' + name);
52 // because defineLocale currently also sets the global locale, we
53 // want to undo that for lazy loaded locales
54 getSetGlobalLocale(oldLocale);
60 // This function will load locale and then set the global locale. If
61 // no arguments are passed in, it will simply return the current global
63 export function getSetGlobalLocale (key, values) {
66 if (isUndefined(values)) {
67 data = getLocale(key);
70 data = defineLocale(key, values);
74 // moment.duration._locale = moment._locale = data;
79 return globalLocale._abbr;
82 export function defineLocale (name, config) {
83 if (config !== null) {
85 if (locales[name] != null) {
86 deprecateSimple('defineLocaleOverride',
87 'use moment.updateLocale(localeName, config) to change ' +
88 'an existing locale. moment.defineLocale(localeName, ' +
89 'config) should only be used for creating a new locale');
90 config = mergeConfigs(locales[name]._config, config);
91 } else if (config.parentLocale != null) {
92 if (locales[config.parentLocale] != null) {
93 config = mergeConfigs(locales[config.parentLocale]._config, config);
95 // treat as if there is no base config
96 deprecateSimple('parentLocaleUndefined',
97 'specified parentLocale is not defined yet');
100 locales[name] = new Locale(config);
102 // backwards compat for now: also set the locale
103 getSetGlobalLocale(name);
105 return locales[name];
107 // useful for testing
108 delete locales[name];
113 export function updateLocale(name, config) {
114 if (config != null) {
116 if (locales[name] != null) {
117 config = mergeConfigs(locales[name]._config, config);
119 locale = new Locale(config);
120 locale.parentLocale = locales[name];
121 locales[name] = locale;
123 // backwards compat for now: also set the locale
124 getSetGlobalLocale(name);
126 // pass null for config to unupdate, useful for tests
127 if (locales[name] != null) {
128 if (locales[name].parentLocale != null) {
129 locales[name] = locales[name].parentLocale;
130 } else if (locales[name] != null) {
131 delete locales[name];
135 return locales[name];
138 // returns locale data
139 export function getLocale (key) {
142 if (key && key._locale && key._locale._abbr) {
143 key = key._locale._abbr;
151 //short-circuit everything else
152 locale = loadLocale(key);
159 return chooseLocale(key);
162 export function listLocales() {
163 return keys(locales);