Add moment locale mapping (#6614)
This commit is contained in:
Родитель
e5f26c2bb3
Коммит
be8cb983a3
|
@ -14,7 +14,7 @@ const glob = require('glob');
|
|||
const shelljs = require('shelljs');
|
||||
const chalk = require('chalk');
|
||||
const toSource = require('tosource');
|
||||
const oneLine = require('common-tags').oneLine;
|
||||
const { oneLine } = require('common-tags');
|
||||
|
||||
const appName = config.get('appName');
|
||||
|
||||
|
@ -27,6 +27,24 @@ const localeDir = path.join(__dirname, '../locale');
|
|||
const poFiles = glob.sync(`${localeDir}/**/${appName}.po`);
|
||||
const dest = path.join(__dirname, '../src/locale/');
|
||||
|
||||
const getLocaleModulePath = (locale, { _config = config } = {}) => {
|
||||
const momentLangMap = _config.get('momentLangMap');
|
||||
|
||||
const momentLocale = locale.replace('_', '-').toLowerCase();
|
||||
const mappedLocale = momentLangMap[momentLocale];
|
||||
|
||||
let localeModulePath = `moment/locale/${momentLocale}`;
|
||||
|
||||
if (mappedLocale) {
|
||||
localeModulePath = `moment/locale/${mappedLocale}`;
|
||||
|
||||
console.info(oneLine`${locale} is mapped to ${mappedLocale} in
|
||||
configuration, therefore ${mappedLocale} will be used.`);
|
||||
}
|
||||
|
||||
return localeModulePath;
|
||||
};
|
||||
|
||||
poFiles.forEach((pofile) => {
|
||||
const dir = path.dirname(pofile);
|
||||
const subdir = path.dirname(dir);
|
||||
|
@ -49,8 +67,8 @@ poFiles.forEach((pofile) => {
|
|||
if (appName === 'amo') {
|
||||
var defineLocale = null;
|
||||
try {
|
||||
const momentLocale = locale.replace('_', '-').toLowerCase();
|
||||
const localeModulePath = `moment/locale/${momentLocale}`;
|
||||
const localeModulePath = getLocaleModulePath(locale);
|
||||
|
||||
// Check for the locale first; if it doesn't exist we don't have
|
||||
// a moment locale that matches.
|
||||
fs.accessSync(`./node_modules/${localeModulePath}.js`);
|
||||
|
@ -81,3 +99,7 @@ poFiles.forEach((pofile) => {
|
|||
fs.writeFileSync(
|
||||
localeOutputFilePath, `module.exports = ${toSource(localeObject)}`);
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
getLocaleModulePath,
|
||||
};
|
||||
|
|
|
@ -240,6 +240,17 @@ module.exports = {
|
|||
},
|
||||
rtlLangs: ['ar', 'fa', 'he', 'ur'],
|
||||
defaultLang: 'en-US',
|
||||
// Some missing moment locales can be mapped to existing ones. Note: moment
|
||||
// locales are lowercase and do not use an underscore.
|
||||
// See: https://github.com/mozilla/addons-frontend/issues/1515
|
||||
momentLangMap: {
|
||||
'bn-bd': 'bn',
|
||||
'fy-nl': 'fy',
|
||||
'nb-no': 'nb',
|
||||
'nn-no': 'nn',
|
||||
'pt-pt': 'pt',
|
||||
'sv-se': 'sv',
|
||||
},
|
||||
|
||||
po2jsonFuzzyOutput: false,
|
||||
|
||||
|
|
|
@ -14,6 +14,8 @@ module.exports = {
|
|||
'^tests/(.*)$': '<rootDir>/tests/$1',
|
||||
// Replaces the following formats with an empty module.
|
||||
'^.+\\.(scss|css|svg|woff|woff2|mp4|webm)$': '<rootDir>/tests/emptyModule',
|
||||
// Alias bin for bin scripts.
|
||||
'^bin/(.*)$': '<rootDir>/bin/$1',
|
||||
},
|
||||
reporters: [
|
||||
'<rootDir>/tests/jest-reporters/fingers-crossed.js',
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import { getLocaleModulePath } from 'bin/build-locales';
|
||||
import { getFakeConfig } from 'tests/unit/helpers';
|
||||
|
||||
describe(__filename, () => {
|
||||
describe('getLocaleModulePath', () => {
|
||||
it('returns the locale module path of a supported locale', () => {
|
||||
expect(getLocaleModulePath('fr')).toEqual('moment/locale/fr');
|
||||
});
|
||||
|
||||
it('returns the locale module path of a mapped locale when locale is not supported by moment', () => {
|
||||
const locale = 'fr-fr';
|
||||
const _config = getFakeConfig({
|
||||
momentLangMap: {
|
||||
[locale]: 'fr',
|
||||
},
|
||||
});
|
||||
|
||||
expect(getLocaleModulePath(locale, { _config })).toEqual(
|
||||
'moment/locale/fr',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -1,4 +1,5 @@
|
|||
/* eslint-disable global-require */
|
||||
import config from 'config';
|
||||
|
||||
describe(__filename, () => {
|
||||
beforeEach(() => {
|
||||
|
@ -27,4 +28,18 @@ describe(__filename, () => {
|
|||
conf = require('config');
|
||||
expect(parseInt(conf.get('serverPort'), 10)).toEqual(5000);
|
||||
});
|
||||
|
||||
describe('momentLangMap', () => {
|
||||
it('should not map locales provided by upstream', () => {
|
||||
const momentLangMap = config.get('momentLangMap');
|
||||
|
||||
Object.keys(momentLangMap).forEach((locale) => {
|
||||
// If it does not throw, it means the file likely exists.
|
||||
expect(() => {
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
require(`moment/locale/${locale}.js`);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче