Skip to content

Commit

Permalink
add test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jul 17, 2020
1 parent 743e5aa commit b2cef0a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 7 additions & 8 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ 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.
-d, --default-locale [LOCALE] Specify the default locale that is used in code.
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.
Expand All @@ -45,7 +45,6 @@ args = Object.assign({
'--default-locale': 'en',
'--locale': [],
'--target': './locales/',
'--match': '**/*.js',
'--global': '__'
}, args)

Expand All @@ -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']
)
}))
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions test/gettext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* global gettext */
console.log(gettext('Happy birthday, %s!', 'Linus'))
6 changes: 6 additions & 0 deletions test/locales/de.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
msgid ""
msgstr ""

# test/log.js:1
msgid "Happy birthday %s!"
msgstr "Alles Gute, %s!"
2 changes: 2 additions & 0 deletions test/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* global __ */
console.log(__('Happy birthday %s!', 'Guillermo'))
62 changes: 62 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b2cef0a

Please sign in to comment.