From 166d77dcd9a9932692942838dc743848be9b8d78 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 8 Feb 2015 13:11:14 +0100 Subject: [PATCH] Add `gulp sync-locales` To make the implementation easier, all translations must be in a single line and must not contain superfluos whitespace. All .dtd files were updated to reflect this. --- CONTRIBUTING.md | 40 ++++++++++++++++++++ extension/locale/de/options.dtd | 24 ++++++------ extension/locale/el-GR/options.dtd | 23 ++++++------ extension/locale/en-US/options.dtd | 23 ++++++------ extension/locale/hu/options.dtd | 24 ++++++------ extension/locale/id/options.dtd | 25 ++++++------- extension/locale/it/options.dtd | 23 ++++++------ extension/locale/ja/options.dtd | 23 ++++++------ extension/locale/nl/options.dtd | 25 ++++++------- extension/locale/pl/options.dtd | 25 ++++++------- extension/locale/ru/options.dtd | 25 ++++++------- extension/locale/sv-SE/options.dtd | 23 ++++++------ extension/locale/zh-CN/options.dtd | 25 ++++++------- extension/locale/zh-TW/options.dtd | 25 ++++++------- gulpfile.coffee | 60 +++++++++++++++++++++++++++++- package.json | 1 + 16 files changed, 251 insertions(+), 163 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 789b844..d557c35 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -103,6 +103,9 @@ Finally send a pull request to same branch as you based your topic branch on automatically install it. (No need to restart Firefox.) - `gulp clean` removes the `build/` directory. - `gulp lint` lints your code. +- `gulp sync-locale` syncs all locales against the en-US locale. To sync against + for example the sv-SE locale instead, pass `--sv-SE` as an option. See also + the “Syncing locales” section below. - Use the `--test` or `-t` option to include the unit test files. The output of the tests are `console.log`ed. See the browser console, or start Firefox from the command line to see it. @@ -114,6 +117,43 @@ unit tests.) [gulp]: https://github.com/gulpjs/gulp [Extension Auto-Installer]: https://addons.mozilla.org/firefox/addon/autoinstaller +### Syncing locales + +This is usually not done by translators, but by developers who change, add or +remove features that involves localized text. + +If you add, remove or reorder translations in a file, do so in _one_ of the +locales (one that is easy for you to test—but always write new translations in +English!). If you modified the en-US locale, run `gulp sync-locales` (or `gulp +sync-locales --en-US`—substitute “en-US” with a locale of choice if needed). +That rewrites all other locales so that: + +- Old translations are removed. +- New translations are added (in English). +- All translations appear in the same order. + +If you modify an existing translation in a file and want to update all other +locales to use the new wording: + +- If possible, edit all other locales by hand to save as much translated text as + possible. +- Otherwise: + 1. Before modifying existing translations, copy the file in question and add + the extension “.old” to the filename. For example, copy a + “vimfx.properties” file to “vimfx.properties.old”. + 2. Make your modifications (in for example “vimfx.properties”, leaving + “vimfx.properties.old” intact). + 3. Run `gulp sync-locales`. It does the same thing as before, except that if a + translation has changed compared to an “.old”-file, the newly changed + translation is used in all locales, replacing what was there before. + 4. Remove the “.old”-file. + +Note that `gulp sync-locales` requires every translation to be in a single line. +In other words, do not line-wrap translations. Also don’t bother adding comments +when translating locale files, since they’ll likely be removed by `gulp +sync-locales`. + + ### Making a release 1. Add a list of changes since the last version at the top of CHANGELOG.md. diff --git a/extension/locale/de/options.dtd b/extension/locale/de/options.dtd index 8b2ef1f..d35cd72 100644 --- a/extension/locale/de/options.dtd +++ b/extension/locale/de/options.dtd @@ -1,27 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/el-GR/options.dtd b/extension/locale/el-GR/options.dtd index b49aff5..fac03c8 100644 --- a/extension/locale/el-GR/options.dtd +++ b/extension/locale/el-GR/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + diff --git a/extension/locale/en-US/options.dtd b/extension/locale/en-US/options.dtd index 661a0b9..b1ef912 100644 --- a/extension/locale/en-US/options.dtd +++ b/extension/locale/en-US/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + diff --git a/extension/locale/hu/options.dtd b/extension/locale/hu/options.dtd index c722d1d..432cc76 100644 --- a/extension/locale/hu/options.dtd +++ b/extension/locale/hu/options.dtd @@ -1,27 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/id/options.dtd b/extension/locale/id/options.dtd index bb74eff..a240625 100644 --- a/extension/locale/id/options.dtd +++ b/extension/locale/id/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/it/options.dtd b/extension/locale/it/options.dtd index ebbfdfe..5f91b6b 100644 --- a/extension/locale/it/options.dtd +++ b/extension/locale/it/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + diff --git a/extension/locale/ja/options.dtd b/extension/locale/ja/options.dtd index 3a288ab..3f665fd 100644 --- a/extension/locale/ja/options.dtd +++ b/extension/locale/ja/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + diff --git a/extension/locale/nl/options.dtd b/extension/locale/nl/options.dtd index 1ff2de8..875aa68 100644 --- a/extension/locale/nl/options.dtd +++ b/extension/locale/nl/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/pl/options.dtd b/extension/locale/pl/options.dtd index e2bdd29..8555962 100644 --- a/extension/locale/pl/options.dtd +++ b/extension/locale/pl/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/ru/options.dtd b/extension/locale/ru/options.dtd index 36a6246..d25a885 100644 --- a/extension/locale/ru/options.dtd +++ b/extension/locale/ru/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/sv-SE/options.dtd b/extension/locale/sv-SE/options.dtd index 8a882b7..3188f32 100644 --- a/extension/locale/sv-SE/options.dtd +++ b/extension/locale/sv-SE/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + diff --git a/extension/locale/zh-CN/options.dtd b/extension/locale/zh-CN/options.dtd index b42c328..0568bf7 100644 --- a/extension/locale/zh-CN/options.dtd +++ b/extension/locale/zh-CN/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/extension/locale/zh-TW/options.dtd b/extension/locale/zh-TW/options.dtd index 643fa1b..9429709 100644 --- a/extension/locale/zh-TW/options.dtd +++ b/extension/locale/zh-TW/options.dtd @@ -1,28 +1,27 @@ - - + + - - + + - - + + - - + + - + - + - + - + diff --git a/gulpfile.coffee b/gulpfile.coffee index f39ea0c..d26af17 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -18,7 +18,8 @@ ### fs = require('fs') -{ join } = require('path') +path = require('path') +regexEscape = require('escape-string-regexp') gulp = require('gulp') coffee = require('gulp-coffee') coffeelint = require('gulp-coffeelint') @@ -41,6 +42,7 @@ TEST = 'extension/test' test = '--test' in process.argv or '-t' in process.argv ifTest = (value) -> if test then [value] else [] +{ join } = path read = (filepath) -> fs.readFileSync(filepath).toString() template = (data) -> mustache(data, {extension: ''}) @@ -166,3 +168,59 @@ gulp.task('release', -> ).pipe(git.commit(message)) git.tag("v#{ version }", message, (error) -> throw error if error) ) + +gulp.task('sync-locales', -> + baseLocale = 'en-US' + for arg in process.argv when arg[...2] == '--' + baseLocale = arg[2..] + for file in fs.readdirSync(join(LOCALE, baseLocale)) + templateString = switch path.extname(file) + when '.properties' then '%key=%value' + when '.dtd' then '' + syncLocale(file, baseLocale, templateString) if templateString +) + +syncLocale = (fileName, baseLocaleName, templateString) -> + regex = ///^ #{ + regexEscape(templateString) + .replace(/%key/, '([^\\s=]+)') + .replace(/%value/, '(.+)') + } $/// + basePath = join(LOCALE, baseLocaleName, fileName) + base = parseLocaleFile(read(basePath), regex) + oldBasePath = "#{basePath}.old" + if fs.existsSync(oldBasePath) + oldBase = parseLocaleFile(read(oldBasePath), regex) + for localeName in fs.readdirSync(LOCALE) when localeName != baseLocaleName + localePath = join(LOCALE, localeName, fileName) + locale = parseLocaleFile(read(localePath), regex) + newLocale = base.template.map((line) -> + if Array.isArray(line) + [ key ] = line + oldValue = oldBase?.keys[key] + value = + if (oldValue? and oldValue != base.keys[key]) or + key not of locale.keys + base.keys[key] + else + locale.keys[key] + return templateString.replace(/%key/, key).replace(/%value/, value) + else + return line + ) + fs.writeFileSync(localePath, newLocale.join(base.newline)) + return + +parseLocaleFile = (fileContents, regex) -> + keys = {} + lines = [] + [ newline ] = fileContents.match(/\r?\n/) + for line in fileContents.split(newline) + line = line.trim() + [ match, key, value ] = line.match(regex) ? [] + if match + keys[key] = value + lines.push([key]) + else + lines.push(line) + return {keys, template: lines, newline} diff --git a/package.json b/package.json index e8568b3..3743ce9 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "coffee-script": "~1.8.0", "coffeelint-forbidden-keywords": "0.0.4", "coffeelint-newline-at-eof": "^0.4.1", + "escape-string-regexp": "^1.0.2", "gulp": "^3.8.10", "gulp-coffee": "^2.2.0", "gulp-coffeelint": "^0.4.0", -- 2.39.3