about summary refs log tree commit diff
path: root/config/webpack
diff options
context:
space:
mode:
authorKoala Yeung <koalay@gmail.com>2017-05-06 10:05:38 +0800
committerEugen Rochko <eugen@zeonfederated.com>2017-05-06 04:05:38 +0200
commitcf0b7532097e7a59d59ea71a89ba39f8a4ab62ad (patch)
treefc968082bb8db90136e4d5c8be0777bf44f40b3f /config/webpack
parentddc34feb5875fd247fc7331b7710406d6de4a143 (diff)
Streamline javascript translation by improving translationRunner (#2808)
* package.json: Add "build:*" targets

* Improve react-intl-translations-manager workflow.
  * Added "build:production" to build production bundle.
  * Added "build:development" to build development bundle.

* Fix json translation files

* Run `yarn manage:translations` to fix translation files.
* Fix `pl.json` for syntax error.

* translationRunner: auto detect existing languages

* Auto detect existing rfc5646 language tag in *.json filenames
  in `app/javascript/mastodon/locale` folder. No need to manually
  define every new language in the languages array here.

* translationRunner: add more functionality

* Allow script user to specify language code to check.
* Added available language check.
* Added --force flag to force creation of unexists language.
* Added --help flag and help messages.

* gitignore: ignore npm-debug.log

* Fix webpack error if NODE_ENV is not defined

Default to use 'development' in config/webpack/configuration.js
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/configuration.js4
-rw-r--r--config/webpack/translationRunner.js106
2 files changed, 80 insertions, 30 deletions
diff --git a/config/webpack/configuration.js b/config/webpack/configuration.js
index 61c5b821f..e2f8d2d8b 100644
--- a/config/webpack/configuration.js
+++ b/config/webpack/configuration.js
@@ -7,8 +7,8 @@ const { readFileSync } = require('fs')
 
 const configPath = resolve('config', 'webpack')
 const loadersDir = join(__dirname, 'loaders')
-const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))[env.NODE_ENV]
-const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))[env.NODE_ENV]
+const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))[env.NODE_ENV || 'development']
+const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))[env.NODE_ENV || 'development']
 
 // Compute public path based on environment and CDN_HOST in production
 const ifHasCDN = env.CDN_HOST !== undefined && env.NODE_ENV === 'production'
diff --git a/config/webpack/translationRunner.js b/config/webpack/translationRunner.js
index c636170b9..937c2edd0 100644
--- a/config/webpack/translationRunner.js
+++ b/config/webpack/translationRunner.js
@@ -1,34 +1,84 @@
+/*eslint no-console: "off"*/
 const manageTranslations = require('react-intl-translations-manager').default;
+const argv = require('minimist')(process.argv.slice(2));
+const fs = require('fs');
+
+const translationsDirectory = 'app/javascript/mastodon/locales';
+const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/;
+const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/;
+const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => {
+  if (fn.match(localeFn)) {
+    acc.push(fn.replace(localeFn, '$1'));
+  }
+  return acc;
+}, []);
+
+// print help message
+if (argv.help !== undefined) {
+  console.log(
+`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
+
+Manage javascript translation files in mastodon. Generates and update
+translations in translationsDirectory: ${translationsDirectory}
+
+OPTIONS
+  --help    show this message
+  --force   force using the provided languages. create files if not exists.
+            default: false
+
+LANGUAGES
+The RFC5646 language tag for the language you want to test or fix. If you want
+to input multiple languages, separate them with space.
+
+Available languages:
+${availableLanguages}
+`);
+  process.exit(0);
+}
+
+// determine the languages list
+const languages = (argv._.length === 0) ? availableLanguages : argv._;
+
+// check if the languages provided are RFC5626 compliant
+(function() {
+  let invalidLanguages = languages.reduce((acc, language) => {
+    if (!language.match(reRFC5646)) {
+      acc.push(language);
+    }
+    return acc;
+  }, []);
+  if (invalidLanguages.length > 0) {
+    console.log(`Error:`);
+    for (let language of invalidLanguages) {
+      console.error(`* Not RFC5626 name: ${language}`);
+    }
+    console.log(`\nUse yarn "manage:translations -- --help" for usage information\n`);
+    process.exit(1);
+  }
+})();
+
+// make sure the language exists. Unless force to create locale file.
+if (argv.force !== true) {
+  let invalidLanguages = languages.reduce((acc, language) => {
+    if (availableLanguages.indexOf(language) < 0) {
+      acc.push(language);
+    }
+    return acc;
+  }, []);
+  if (invalidLanguages.length > 0) {
+    console.log(`Error:`);
+    for (let language of invalidLanguages) {
+      console.error(`* Language not available: ${language}`);
+    }
+    console.log(`\nIf you want to force creating the language(s) above, please add the --force option.\n`);
+    process.exit(1);
+  }
+}
 
 manageTranslations({
   messagesDirectory: 'build/messages',
-  translationsDirectory: 'app/javascript/mastodon/locales/',
+  translationsDirectory,
   detectDuplicateIds: false,
   singleMessagesFile: true,
-  languages: [
-    'ar',
-    'en',
-    'de',
-    'es',
-    'fa',
-    'hr',
-    'hu',
-    'io',
-    'it',
-    'fr',
-    'nl',
-    'no',
-    'oc',
-    'pt',
-    'pt-BR',
-    'uk',
-    'fi',
-    'eo',
-    'ru',
-    'ja',
-    'zh-HK',
-    'zh-CN',
-    'bg',
-    'id',
-  ],
-})
+  languages,
+});