![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Polyglot is an internationalization library for express. It's template-agnostic, based on JSON files, and less than 200 lines of code. Compatible with express 3+.
Install with npm install polyglot
:
var i18n = require('polyglot')
app = express()
app.use(express.cookieParser())
app.use(express.cookieSession())
app.use(i18n())
# register template locals
app.locals(i18n.locals)
Check the example app.
app.use(i18n({
debug : false // enable debug messages
, default : 'en' // default language
, path : '/lang' // path for .json language files
}))
Translation files are .json
files containing the translated strings. The default directory is /lang
. To add a new language, just create an empty .json file with the language code as it's name (i.e. de.json
).
See https://github.com/ricardobeat/node-polyglot/blob/master/example/lang/pt.json
String definitions are automatically added to all available languages by adding the updateStrings
middleware to your express config:
app.configure('development', function(){
app.use(i18n.updateStrings)
})
All the following examples are based on handlebars templates.
Registering app.locals(i18n.locals)
is simply a shortcut for:
app.locals({
__ : i18n.translate
, _n : i18n.plural
, languages : i18n.languages
})
In addition to that, i18n()
registers a middleware which sets req.lang
and req.locale
containing the user's settings.
See the /example
folder for an implementation using Handlebars helpers.
Takes a string and returns a translation based on your current session preferences (req.session.lang
)`.
{{ __('hello') }}
// en: 'hello'
// pt: 'olá'
Takes [n, singular, plural]
or [n, zero, singular, plural]
arguments. Using i18n.translate
with the same arguments will use plural automatically.
{{ __(1, "%s cat", "%s cats") }}
// en: '1 cat'
// pt: '1 gato'
{{ __(0, "no cats", "%s cat", "%s cats") }}
// en: 'no cats'
// pt: 'nenhum gato'
To change the current language call i18n.setLanguage
, passing the user's session object and desired language code:
app.get('/lang/:lang', function(req, res){
i18n.setLanguage(req.session, req.params.lang)
res.redirect(req.headers.referer || '/')
})
Accessing http://yourapp/lang/de will set language to de
, if it is defined in the i18n.languages object.
Polyglot is written in coffeescript and distributed in js. Read the annotated source here.
Run tests using mocha
or npm test
. You need coffee-script
and mocha
installed globally on your machine.
FAQs
i18n module for express
The npm package polyglot receives a total of 0 weekly downloads. As such, polyglot popularity was classified as not popular.
We found that polyglot 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.