
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
i18n-abide
Advanced tools
This module abides by the user's language preferences and makes it available throughout the app.
This module abides by the Mozilla L10n way of doing things.
The module abides.
This module is under development, but frozen parts of it power the Mozilla Persona service in 40+ languages.
Mozilla Hacks blog has a three part introduction.
npm install
has got your back
You should install Gnu gettext to get msginit, xgettext, and other tools.
What is a string wrangler? A person or an automated build process that will merge and delete strings between releases.
npm install i18n-abide
In your app where you setup express or connect:
var i18n = require('i18n-abide');
app.use(i18n.abide({
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
translation_directory: 'i18n'
}));
This block sets up the middleware and views with gettext support. We declare support for English, German, Spanish, and two debug locales (more on this later).
In your routes, you can use the gettext function in .js
files.
exports.homepage = function(req, resp) {
resp.render('home', {title: req.gettext("Hey, careful, man, there's a beverage here!")});
};
You can set locale in the scope of per-request instead of letting i18n-abide
decide the locale for you. The following example shows how to set the locale of the request to zh_TW
(Traditional Chinese):
exports.homepage = function(req, resp) {
req.setLocale('zh_TW');
resp.render('home', {title: "my title"});
};
In your layout files, you can add
<!DOCTYPE html>
<html lang="<%= lang %>" dir="<%= lang_dir %>">
<head>
<meta charset="utf-8">
...
In your templates files, you can use the gettext function in .ejs
files:
<p><%= gettext("This will not stand, ya know, this aggression will not stand, man.") %></p>
i18n-abide also provides a format
function for string interpolation.
These are both server side translations and client side translations. Server side works out of the box and is the most common use case.
If you also want to do client-side translations,
i18n-abide provides lib/gettext.js
and you can do the same in .js
and .ejs
files.
The i18n-abide
module uses the accept-language
HTTP header to determine which language to use. It is also possible to have language tags parsed on the URL with an option at setup:
app.use(i18n.abide({
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
translation_directory: 'i18n',
locale_on_url: true
}));
Here the locale_on_url
option has been set to true
, which causes i18n-abide
to perform an extra step with every request: each time a request comes to the server, the URL is examined for an optional language tag. Some examples include:
If a URL includes a language tag for one of the supported languages specified at setup, the request's accept-language
header is overridden by the URL's language. The URL is then rewritten to no longer include this language tag, such that routes further down in the middleware chain are unaffected.
Given the URLs listed above, the following would happen in the i18n-abide
middleware with locale_on_url
set to true
:
en-US
, url is http://app.com/de
, url is http://app.com/The locale_on_url
option is off by default.
The i18n-abide
module can use either PO/POT files, which get transformed to JSON via provided command line tools, or PLIST (i.e., XML) files, which require no transformation prior to use.
By default PO/POT files are used. This can be changed during setup:
app.use(i18n.abide({
supported_languages: ['en-US', 'de', 'es', 'db-LB', 'it-CH'],
default_lang: 'en-US',
debug_lang: 'it-CH',
translation_directory: 'i18n',
translation_type: 'plist'
}));
This will cause i18n-abide
to look for files named messages.plist
in locale directories beneath your translation_directory
root. For example, the es
language listed above should be located at i18n/es/messages.plist
.
If using the default of PO/POT files instead of plist, you must transform your strings such that i18n-abide
can work with them.
$ mkdir -p locale/templates/LC_MESSAGES
$ ./node_modules/.bin/extract-pot --locale locale .
If you look in locale/templates/LC_MESSAGES/messages.pot
you will see your strings have been extracted.
Edit this file and make sure charset
is set to UTF-8
.
If there are certain files or directories you want to exclude, use --exclude
one or more times. Example:
$ extract-pot --locale locale . --exclude tests --exclude examples
Example messages.pot:
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: ./routes.js:81
msgid "Hey, careful, man, there's a beverage here!"
msgstr ""
#: views/404.ejs:5
msgid "This will not stand, ya know, this aggression will not stand, man."
msgstr ""
To create po
files in bulk, do:
$ for l in en_US de es db_LB; do
mkdir -p locale/${l}/LC_MESSAGES/
msginit --input=./locale/templates/LC_MESSAGES/messages.pot \
--output-file=./locale/${l}/LC_MESSAGES/messages.po \
-l ${l}
done
If you look at locale/en_US/LC_MESSAGES/messages.po
, it will be very similar to your template messages.pot file.
This creates .po
files which you can give to localizers to translate your copy.
Let's put the i18n-abide tools in our path:
$ export PATH=$PATH:node_modules/i18n-abide/bin
And run a string merge:
$ merge_po.sh ./locale
A merge takes strings from our .pot
files and pushes them into our .po
files. If you have podebug
installed, it also automatically translates db-LB
.
db-LB
is a special
debug locale. To trigger it, set your Browser or Operating System language to Italian (Switzerland) which is it-CH
. This fake locale db-LB
will be triggered, it is David Bowie speak for the region of Labyrinth. Oh, hell ya a Dude / Bowie Mashup.
That just happened.
Example: locale/db_LB/LC_MESSAGES/messages.po
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: routes.js:81
msgid "Hey, careful, man, there's a beverage here!"
msgstr "Hǝʎ´ ɔɐɹǝɟnʅ´ ɯɐu´ ʇɥǝɹǝ,s ɐ qǝʌǝɹɐƃǝ ɥǝɹǝ¡"
#: views/404.ejs:5
msgid "This will not stand, ya know, this aggression will not stand, man."
msgstr "⊥ɥıs ʍıʅʅ uoʇ sʇɐup´ ʎɐ ʞuoʍ´ ʇɥıs ɐƃƃɹǝssıou ʍıʅʅ uoʇ sʇɐup´ ɯɐu·"
And we will compile .po
files into .mo
files.
$ compile_mo.sh locale/
Now, start up your Node server and visit a page you've wrapped strings in Gettext...
See docs/USAGE.md for full details.
FAQs
Express/connect module for Node i18n and l10n support
The npm package i18n-abide receives a total of 1,093 weekly downloads. As such, i18n-abide popularity was classified as popular.
We found that i18n-abide demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers 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.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.