From b2cef0a825c6c8f7b9beb19438f61c83521f5e76 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Fri, 17 Jul 2020 12:35:29 +0200 Subject: [PATCH] add test setup --- README.md | 2 +- bin/cmd.js | 15 ++++++----- package.json | 8 +++--- test/gettext.js | 2 ++ test/locales/de.po | 6 +++++ test/log.js | 2 ++ test/test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 test/gettext.js create mode 100644 test/locales/de.po create mode 100644 test/log.js create mode 100644 test/test.js diff --git a/README.md b/README.md index 20ac37f..96c7bfe 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Localization workflow for Browserify -## How does this work +## How does this work? This is a Browserify-based workflow for localizing client side applications. It is built for use cases where you can and want to ship one bundle per locale. Strings are defined by calling a pseudo-global function (i.e. `__(string, args..)`) in the default language in your code (similar to GNU gettext or similar). Strings are stored in `.po` files. diff --git a/bin/cmd.js b/bin/cmd.js index 3572840..815664f 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -15,16 +15,18 @@ var args = arg({ '--default-locale': String, '--locale': [String], '--target': String, - '--match': String, '-h': '--help', '-d': '--default-locale', '-l': '--locale', - '-t': '--target', - '-m': '--match' + '-t': '--target' }) if (args['--help']) { - console.log(`Usage: extract-strings [options] + console.log(`Usage: extract-strings [options] glob-pattern + +glob-pattern defines in which files to look for strings. Usually this will +be "**/*.js" or similar. + Options: -l, --locale [LOCALE] Specify the locales to extract. Pass multiple locales by passing multiple flags. @@ -32,8 +34,6 @@ Options: Defaults to "en". -t, --target [DIRECTORY] Specify the target directory for saving .po files. Defaults to "./locales". - -m, --match [DIRECTORY] Specify a glob pattern for files to extract - strings from. Defaults to "**/*.js". -g, --global [IDENTIFIER] Specify the global identifier used as l10n function in code, Defaults to "__" -h, --help Display this help message. @@ -45,7 +45,6 @@ args = Object.assign({ '--default-locale': 'en', '--locale': [], '--target': './locales/', - '--match': '**/*.js', '--global': '__' }, args) @@ -63,7 +62,7 @@ if (eligible.length === 0) { Promise.all(eligible.map(function (locale) { return extractStrings( path.join(process.cwd(), args['--target'], locale + '.po'), - args['--match'], + args._[0], args['--global'] ) })) diff --git a/package.json b/package.json index fb6366b..8c11f65 100644 --- a/package.json +++ b/package.json @@ -5,13 +5,13 @@ "main": "index.js", "scripts": { "pretest": "standard", - "test": "echo \"Error: no test specified\" && exit 1", + "test": "tape test/test.js", "preversion": "npm test", "version": "changes --commits", "postversion": "git push --follow-tags && npm publish --access public" }, "bin": { - "extract-strings": "./bin/cli.js" + "extract-strings": "bin/cli.js" }, "repository": { "type": "git", @@ -30,8 +30,10 @@ "homepage": "https://github.com/offen/l10nify#readme", "devDependencies": { "@studio/changes": "^2.0.1", + "browserify": "^16.5.1", "standard": "^14.3.4", - "tape": "^5.0.1" + "tape": "^5.0.1", + "vm": "^0.1.0" }, "dependencies": { "arg": "^4.1.3", diff --git a/test/gettext.js b/test/gettext.js new file mode 100644 index 0000000..2985d64 --- /dev/null +++ b/test/gettext.js @@ -0,0 +1,2 @@ +/* global gettext */ +console.log(gettext('Happy birthday, %s!', 'Linus')) diff --git a/test/locales/de.po b/test/locales/de.po new file mode 100644 index 0000000..ae89b65 --- /dev/null +++ b/test/locales/de.po @@ -0,0 +1,6 @@ +msgid "" +msgstr "" + +# test/log.js:1 +msgid "Happy birthday %s!" +msgstr "Alles Gute, %s!" diff --git a/test/log.js b/test/log.js new file mode 100644 index 0000000..e571a2f --- /dev/null +++ b/test/log.js @@ -0,0 +1,2 @@ +/* global __ */ +console.log(__('Happy birthday %s!', 'Guillermo')) diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..bfeba9a --- /dev/null +++ b/test/test.js @@ -0,0 +1,62 @@ +var path = require('path') +var tape = require('tape') +var browserify = require('browserify') +var vm = require('vm') + +tape.test('returns the default language when no options are passed', function (t) { + bundle('log.js', null, function (err, src) { + if (err) { + t.fail(err) + } + + vm.runInNewContext(src, { + console: { log: log } + }) + + function log (value) { + t.equal(value, 'Happy birthday Guillermo!', 'passes') + t.end() + } + }) +}) + +tape.test('returns the given language', function (t) { + bundle('log.js', { locale: 'de', source: './test/locales' }, function (err, src) { + if (err) { + t.fail(err) + } + + vm.runInNewContext(src, { + console: { log: log } + }) + + function log (value) { + t.equal(value, 'Alles Gute, Guillermo!', 'passes') + t.end() + } + }) +}) + +tape.test('uses the given global identifier', function (t) { + bundle('gettext.js', { global: 'gettext' }, function (err, src) { + if (err) { + t.fail(err) + } + + vm.runInNewContext(src, { + console: { log: log } + }) + + function log (value) { + t.equal(value, 'Happy birthday, Linus!', 'passes') + t.end() + } + }) +}) + +function bundle (file, opts, callback) { + var b = browserify() + b.add(path.join(__dirname, file)) + b.transform(path.resolve(__dirname, '..'), opts || {}) + b.bundle(callback) +}