Gathering detailed insights and metrics for lokales
Gathering detailed insights and metrics for lokales
Gathering detailed insights and metrics for lokales
Gathering detailed insights and metrics for lokales
npm install lokales
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
34 Commits
2 Watching
1 Branches
2 Contributors
Updated on 06 Oct 2018
TypeScript (82.63%)
JavaScript (17.37%)
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
0%
5
Compared to previous week
Last month
-55.8%
19
Compared to previous month
Last year
-19.8%
636
Compared to previous year
1
Barebones i18n localization written in TypeScript. Similar to y18n but with a few improvements, handles errors better preventing corrupt or empty locale files. Other than that about the same.
Fixed graceful exit, ensures write before exit. Fix issue where directory isn't created, refactored simplified methods. Add method "sync" to synchronize all locales from a primary locale. This ensures your primary locale (probably "en") and it's keys exist in all other locales known on your system.
Initialize using ES5 or ES6/TypeScript Imports.
1import { Lokales } from 'lokales'; 2const lokales = new Lokales({ /* your options */ });
OR ES5
1const Lokales = require('lokales').Lokales; 2const lokales = new Lokales({ /* your options */ });
Singular
Singular example passing key and format values.
1lokales.__('Hello my name is %s.', 'Joe'); 2// Result > Hello my name is Joe.
Plural
Plural example passing singular key, plural key, count and format values.
1lokales.__n('I have %d cat', 'I have %d cats', 2, 'Joe');
2// Result > I have 2 cats.
Literal
Template literal example.
1const color = 'blue'; 2lokales.__`My favorite color is ${color}.`; 3// Result > My favorite color is blue.
Options can also be set using the lokales.setOption(key, value) method.
Key | Description |
directory | The directory where locales are stored. |
locale | The active locale to be used. |
update | When true localization file is updated when key is unknown. |
onUpdate | Custom callback called on locale file updated. |
onError | Custom callback called on error. |
Simple API for plural or singular localization along with get and set for options.
Method | Arguments | Description |
__ | val: string, ...args: any[] | Singular localization. |
__n | singular: string, plural: string, count: number, ...args: any[] | Plural localization. |
setOption | key: LokalesOptionKeys | ILokalesOptions, val: any | Sets an option by key/value or options object. |
getOption | key: string | Gets an existing option. |
keyExists | key: string, locale?: string, directory?: string | Checks if key exists in locale. |
In some cases it may be useful to call a translate API after your primary localization file is updated. For example upon updating the en.json locale you may with to update perhaps the es, fr, ru or it locales. Here's an example using Google Translate.
IMPORTANT
A quick note about the below example. One key piece missing below for the sake of brevity is some sort of method to serialialize and deserialize the string to be translated. This is because the translate API won't typically know what to do with %s or %d. You can simply iterate the string and replace these tokens with tokens your translate API will ignore. The restructure the data after translation.
1import { Lokales } from 'lokales'; 2import { translate } from '@google-cloud'; 3import { writeFile } from 'fs'; 4import { join } from 'path'; 5 6const lokales = new Lokales({ onUpdate: onUpdate }); 7const trans = translate({ 8 key: 'YOUR_API_KEY' 9}); 10 11/** 12 * On Update called when process queue updated locale. 13 * 14 * Updated Object Contains: 15 * singular: string; // The singular key string. 16 * plural: string; // The plural key if provided or null. 17 * count: number; // The count when plural is used. 18 * args: []; // The original format arguments passed. 19 * options: {} // Snapshot of options. (see options above for details) 20 */ 21function onUpdate(err, updated, instance) { 22 23 if (err) 24 throw err; // do something with error. 25 26 const value = updated.singular // you might check for plural here but for simplicity. 27 const options = updated.options; 28 const lang = 'es'; // the lang we want to translate to. 29 const translated: any = {}; // object to store translated values. 30 31 trans.translate(value, lang, (err, res) => { 32 33 if (err) 34 throw err; 35 36 if (!Array.isArray(res)) // ensure our result is an array. 37 res = [res]; 38 39 res.forEach((s, i) => { 40 translated[s] = s; // see above important note. 41 }); 42 43 const savePath = path.join(options.directory, lang + '.json'); 44 const serialized = JSON.stringify(translated, null, 2); 45 46 fs.writeFile(savePath, serialized, (err) => { 47 // do something with/if error. 48 // or 49 // log success etc. 50 }); 51 52 }); 53 54}
See https://origin1tech.github.io/lokales/
See LICENSE.md
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
45 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More