@aurium/i18n
Advanced tools
Comparing version 0.0.2 to 0.0.4
47
i18n.js
"use strict"; | ||
const toString = ''.toString | ||
const __toString = ''.toString | ||
const toStr = (obj)=> __toString.call(obj) | ||
function stringBuilder(str, ...args) { | ||
str = toString.call(str) // Don't change the original string object. | ||
if (args.length === 1 && args[0] && typeof(args[0]) === 'object') { | ||
@@ -15,3 +15,3 @@ args = args[0] | ||
} | ||
return new L10nString(str) | ||
return mkL10nString(str) | ||
} | ||
@@ -21,9 +21,13 @@ | ||
toString() { | ||
return '' + this[0] | ||
} | ||
plural(num) { | ||
let str = num===1 ? this[0] : this[1] | ||
return new L10nString(toString.call(str).replace(/\{num\}/g, num)) | ||
return mkL10nString(str.replace(/\{num\}/g, num)) | ||
} | ||
build(...args) { | ||
return this.map((str)=> stringBuilder(toString.call(str), ...args) ) | ||
return this.map((str)=> stringBuilder(str, ...args) ) | ||
} | ||
@@ -33,9 +37,9 @@ | ||
let arr = this.filter((str)=> | ||
toString.call(str).split('\0').slice(1).includes(c) | ||
str.split('\0').slice(1).includes(c) | ||
) | ||
return arr.length == 0 | ||
? new L10nString(this[0]) | ||
? mkL10nString(this[0]) | ||
: arr.length == 1 | ||
? new L10nString(arr[0]) | ||
: arr.map((str)=> new L10nString(str)) | ||
? mkL10nString(arr[0]) | ||
: arr.map(mkL10nString) | ||
} | ||
@@ -47,8 +51,4 @@ | ||
toString() { | ||
toString.call(this).split('\0')[0] | ||
} | ||
get raw() { | ||
return toString.call(this) | ||
return toStr(this) | ||
} | ||
@@ -66,3 +66,3 @@ | ||
category(c) { | ||
return new L10nString(toString.call(this)) | ||
return new L10nString(toStr(this)) | ||
} | ||
@@ -72,5 +72,16 @@ | ||
function mkL10nString(str) { | ||
let l10n = new L10nString(str.split('\0')[0]) | ||
l10n.categories = str.categories ? [] : str.split('\0').slice(1) | ||
return l10n | ||
} | ||
function mkL10nArray(arr) { | ||
return new L10nArray(...arr) | ||
} | ||
function mkLangList(langList, defaultLang='en') { | ||
let newLangList = [] | ||
let navigator = (this||{}).hasOwnProperty('navigator') ? this.navigator : {} | ||
const win = (typeof window !== 'undefined') ? window : {} | ||
const navigator = win.hasOwnProperty('navigator') ? win.navigator : {} | ||
;(langList || (navigator && navigator.languages) || []) | ||
@@ -104,4 +115,4 @@ .map((l)=> l.toLocaleLowerCase().replace(/-/g, '_') ) | ||
let l10n = getL10n(prop, target, [...target.langs]) | ||
if (l10n.forEach) return new L10nArray(...l10n) | ||
else return new L10nString(l10n) | ||
if (l10n.forEach) return mkL10nArray(l10n) | ||
else return mkL10nString(l10n) | ||
} | ||
@@ -108,0 +119,0 @@ } |
{ | ||
"name": "@aurium/i18n", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"description": "Internationalization made simple", | ||
"main": "i18n.js", | ||
"bin": { | ||
"extract-keys": "bin/extract-keys" | ||
"extract-i18n-keys": "bin/extract-i18n-keys" | ||
}, | ||
"scripts": { | ||
"test": "node test/test.js" | ||
"test": "node test/test.js", | ||
"test:docker:build": "docker image build --network=host -f test/Dockerfile -t i18n-test:$npm_package_version .", | ||
"test:docker:run": "docker run --rm --volume $(pwd):/opt/src --network=host --name i18n-test i18n-test:$npm_package_version sh -c 'cp /opt/src/package.json . && echo Update source... && cp -vr $(ls -1 /opt/src | grep -v node_modules | sed 's#^#/opt/src/#') ./ && echo Run tests... && npm -s test'" | ||
}, | ||
@@ -26,3 +28,11 @@ "repository": { | ||
}, | ||
"homepage": "https://gitlab.com/nTopus/i18n#readme" | ||
"homepage": "https://gitlab.com/nTopus/i18n#readme", | ||
"devDependencies": { | ||
"puppeteer": "^2.0.0", | ||
"puppeteer-firefox": "^0.5.0" | ||
}, | ||
"dependencies": { | ||
"commander": "^5.0.0", | ||
"globby": "^11.0.0" | ||
} | ||
} |
Internationalization made Simple | ||
================================ | ||
The focus of this project is to make the i18n call to look simple, clean and obvious. | ||
Instaling | ||
--------- | ||
```bash | ||
npm install @aurium/i18n | ||
``` | ||
``` | ||
Usage | ||
----- | ||
```js | ||
var l10nData = { | ||
en: { | ||
some_string: 'Some string.', | ||
has_n_aples: ['There is one apple.', 'There are {num} apples.'] | ||
}, | ||
pt_br: { | ||
some_string: 'Algum texto.', | ||
has_n_aples: ['Existe uma maçã.', 'Existem {num} maçãs.'] | ||
} | ||
} | ||
const i18nBuilder = require('@aurium/i18n') | ||
var i18n = i18nBuilder(l10nData, ['pt-Br', 'es']) | ||
console.log('>> '+ i18n.some_string) | ||
console.log('>> '+ i18n.has_n_aples.plural(5)) | ||
``` | ||
Extracting i18n keys from source code | ||
------------------------------------- | ||
This package provides the `extract-i18n-keys` cli, that can find for keys like `i18n.some_string` and add this to l10n files. You can run this command to add each new key you just write to the l10n files, without change current ones. | ||
`extract-i18n-keys` can write js file modules with ocurrences information, or clean json files. You can also change your proxy var name from `i18n` to any other and use the `-p|--i18n-proxy-name` parameter to inform that. | ||
For more details, read `extract-i18n-keys --help`. | ||
Hacking and Contributing | ||
------------------------ | ||
Please, [read the contributing page](https://gitlab.com/nTopus/i18n/blob/master/CONTRIBUTING.md). |
@@ -26,2 +26,5 @@ "use strict"; | ||
console.log('>> '+ i18n.some_string) | ||
console.log('>> '+ i18n.some_string.toString()) | ||
console.log(i18n.some_string.toString()) | ||
console.log(i18n.some_string) | ||
//TODO: console.log('>> '+ i18n.some_string.lang('en')) | ||
@@ -43,2 +46,39 @@ console.log('>> '+ i18n.only_english) | ||
console.log('>> '+ i18n.someone_is_beaulty.category(person2.gender).build(person2)) | ||
console.log('>> '+ i18n.someone_is_beaulty.build(person2)) | ||
const firefox = require('puppeteer-firefox'); | ||
(async () => { | ||
console.log(11) | ||
const browser = await firefox.launch({args: ['--no-sandbox']}); | ||
console.log(22) | ||
const page = await browser.newPage(); | ||
console.log(33) | ||
await page.goto('./test.html'); | ||
console.log(44) | ||
await page.screenshot({path: '/tmp/example.png'}); | ||
console.log(55) | ||
await browser.close(); | ||
console.log(66) | ||
})().catch(err=> { | ||
console.error(err); | ||
process.exit(1) | ||
}); | ||
const chromium = require('puppeteer'); | ||
(async () => { | ||
console.log('c 1') | ||
const browser = await chromium.launch({args: ['--no-sandbox']}); | ||
console.log('c 2') | ||
const page = await browser.newPage(); | ||
console.log('c 3') | ||
await page.goto('./test.html'); | ||
console.log('c 4') | ||
await page.screenshot({path: '/tmp/example.png'}); | ||
console.log('c 5') | ||
await browser.close(); | ||
console.log('c 6') | ||
})().catch(err=> { | ||
console.error(err); | ||
process.exit(1) | ||
}); | ||
20032
9
177
49
2
2
+ Addedcommander@^5.0.0
+ Addedglobby@^11.0.0
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedcommander@5.1.0(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.3(transitive)
+ Addedfastq@1.19.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)