
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
****>> STILL UNDER CONSTRUCTION <<****
Localization is a pain, specially when dealing with gender specific translations and pluralization, other libraries have tackled this with complex json
/xml
structures. This library avoids that by working similar to existing templating libraries, allowing translation to be self contained and inline. Using ES6 templating string makes it even easier.
/** Node / Server Setup **/
var __ = require('locaez')
/** AMD / RequireJS Setup **/
// Todo
/** Standalone / Browser Setup **/
// Library is automatically added to global/window scope
__('I have {{integer n zero(bottles) one(bottle) other(bottles)}} of beer', {n: 10})
// I have 10 bottles of beer
__('{{gender g male(He) female(She)}} has {{integer n zero(like) one(like) other(likes)}}', {g: 'female', n: 103})
// She has 103 likes
The translations are stored in a JSON
file which is loaded in and stored by it's language, the language to decided either by UserAgent or by user if loaded manually.
{
"config": {
"lang": "en",
"debug": false
},
"-1783117148": "This has been translated (Test Locale Loading) {{g gender male(male) female(female)}}"
}
The number stored for each translation is a unique HashCode to that string, the preference for a hashCode instead of original string is for compression and readability of contents.
Along with the with the translation is the config for that specific lang that overrides all aspects of translation.
The HashCode is generated using the function below (there's a Gulp task below too that trawls your source code and generates a translation file with this function)
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
Should library allow hashCode matching to be optional? And let developer decide to use
STRING CONST
instead, contact me if you believe so.
/** fen.js (loaded by localez) **/
locale = {
'-1390162012': 'Morning',
'-870584423': 'Good Afternoon',
'-2022316621': 'What a wonderful day!',
'-1392943720': 'You are {{gender target male({{n amount zero(sexy man) other(sexy men)}}) female({{n amount zero(sexy lady) other(sexy ladies)}})}}.',
'-1672370112': 'There\'s {{integer bottles other(bottles) one(bottle)}} of beer on the wall.'
}
/** fr.js (loaded by localez) **/
locale = {
'-1390162012': 'Matin',
'-870584423': 'Bon après-midi',
'-2022316621': 'Quel wonderfuly jour !',
'1837336777': '{{gender target male({{n amount zero(vous êtes %i sexy homme) other(vous êtes %i hommes sexy)}}) female({{n amount zero(vous êtes %i dame sexy) other(vous êtes %i dames sexy)}})}}.',
'-1672370112': 'There\'s {{integer bottles other(bottles) one(bottle)}} of beer on the wall.'
}
/** In Source Code **/
__('Morning')
// Matin
/** You can pass gender alternatives and pluralizers **/
__('There\'s {{i bottles other( bottles) one( bottle)}} of beer on the wall.', {bottles: 99})
// vous êtes deux dames sexy.
__('You are {{g target male({{n amount zero(sexy man) other(sexy men)}}) female({{n amount zero(sexy lady) other(sexy ladies)}})}}.', {amount: 2, target: 'female'})
// vous êtes deux dames sexy.
// Notices in the fr.json the sentences are all inside the expression for gender using number placement.
/** Node / Server Setup **/
var path = require('path');
__.load('fr', path.resolve(__dirname + '/locales/fr.js'));
/** AMD / RequireJS Setup **/
// Todo
/** Standalone / Browser Setup **/
localez.load('fr')
// or say src
localez.load('fr', 'http://url.com/fr.json')
Parse a string and inject appropriate variables if required.
__(STRING, [OBJECT])
The library works by first retrieving exact match of string from the stored locale (which is loaded on launch), it then parses that string for Types
which has Options
Type Long | Type Shorthand | Type Options |
---|---|---|
gender | g | Can be what ever you want, it just needs to match up to variable passed in: male or female |
integer | i | zero , one , few , many , other |
number | n | zero , one , few , many , other |
In any type the second argument is the variable used for options, the variable is gained from the passed in object.
Anything inside an option is also parsed for locale expressions
__('It\'s a {{g profile male(Man\'s) female(Women\'s)}} world.', {profile: 'male'})
// It's a Mans world.
__('It\'s a {{g profile male(Man\'s) female(Women\'s)}} world.', {profile: 'female'})
// It's a Women's world.
__('There\'s {{i bottles other(bottles) one(bottle)}} of beer on the wall.', {bottles: 99})
// There's 99 bottles of beer on the wall.
__('{{i bottles other(There\'s %i bottles) one(There was only %i bottle)}} of beer on the wall.', {bottles: 1})
// There was only 1 bottle of beer on the wall.
n.b. The library lets you decide if you want a gap between numbers using the %i
to indicate where to place number, if non found it will be injected before string with space.
__('There\'s {{n bottles other( bottles) one( bottle)}} of beer on the wall.', {bottles: 99})
// There's ninety-nine bottles of beer on the wall.
Inside the locale that's loaded you can add a config that will changed how the string is parsed.
config: {
debug: false,
}
Config Options | Defaults | |
---|---|---|
debug | If debug is enabled or not, default is false | |
debugConsoleStyle | ||
warn | background: #990f0f; color: #ffc7c7 | Styling for warning messages in console |
error | background: #990f0f; color: #ffc7c7 | Styling for error messages in console |
openTag | {{ | string to match on for opening an expression |
closeTag | }} | string to match on for closing an expression |
matchers | ||
type | /^(\s+)?\w+/i | Regex to extract the type inside an expression |
variable | /^(\s+)?[\w.]+/i | Regex to extract the variable in an expression |
markers | These are the markers for types | |
gender | ['gender', 'g'] | Determines if the expression is a gender type |
integer | ['integer', 'i'] | Determines if the expression is a integer type |
number | ['number', 'n'] | Determines if the expression is a number type |
numberEnum | Override the enums returned by the numbers function | |
zero | zero | |
one | one | |
two | two | |
few | few | |
many | many | |
other | other | |
numbers | function(number, gender) | A function to decide which enum the value of the variable is, used to pick the option inside an expression |
numberToString | function(number, gender) | A function to convert an integer into number text (1 -> one) |
Below is a Gulp task that will trawl the source code directory for any translation references and spit them out to ./locales/output.js
Download from: localez/gulptask.js
FAQs
Localize from single string for genders, numbers, and number to text.
We found that localez demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.