Skip to main content

While internationalization in Zope/Plone is very mature there is no generally accepted way of doing i18n in JavaScript. Typically packages that contain Javascript code expose i18n by having hard-coded dictionaries or templates that dynamically create these dictionaries. While this is fine for small i18n catalogs, it easily gets tedious and is hard to update.

This package provides the essentials for leveraging the i18n infrastructure in Plone inside your JavaScript code:

  • Allows you to load arbitrary gettext catalogs from Plone.

  • Provides you with Message Factories very similar to the ones used in your python code.

  • Makes use of the local storage available in modern browsers to avoid reloading message catalogs.

Usage

Your package should depend on jarn.jsi18n and include it in its meta-data dependencies.

In order to instantiate a MessageFactory and use it you will need to load the i18n catalog. Typically, this will be done in a way similar to:

$(document).ready(function () {
    jarn.i18n.loadCatalog('plone', 'el');
    _ = jarn.i18n.MessageFactory('plone')
});

The second parameter in loadCatalog specifying the language is optional, when omitted the lang attribute in the html tag is used.

Now that we have a message factory we can use it to get translated strings:

> _('Contributor');
Συντελεστής

or with keyword parameters:

> _('Groups are: ${names}', {names: 'Jarnians'})
"Οι ομάδες είναι: Jarnians "

You can if you wish load multiple catalogs (or languages for the same catalog) and instantiate their respective factories, for instance:

> jarn.i18n.loadCatalog('plone', 'es');
> _es = jarn.i18n.MessageFactory('plone', 'es');
> _es('Contributor');
"Contribuyente"

Caching

If the client browser supports local storage it will be used to store the catalogs that you load. This makes it possible to avoid making an ajax request every time in order to load the full catalog. The stored catalog is by default valid for 24 hours. You can set the time-to-live by calling jarn.i18n.setTTL(millis) passing in milliseconds for how long the local storage cache should remain valid. Note that if local storage is supported, and the cache has not expired, the browser will NOT reload a catalog even if it changes in the filesystem.

Changelog

1.1 - 2014-07-10

  • Fix for Internet Explorer 8, defining Date.now() in jsi18n.js: IE8 does not know Date.now() by default [kysr]

1.0 - 2013-01-25

  • courtesy release so people don’t get version 0.11 over 0.3. Also, about time for a 1.0…

0.3 - 2012-12-17

  • Do not cache null catalog contents [cambell-bika]

  • simpler MessageFactory [mmariani]

0.2 - 2011-12-6

  • A few bug fixes. [ggozad]

  • Test coverage with jasmine.js [ggozad]

0.11 - 2011-11-01

  • Fix typos.

0.1 - 2011-11-01

  • First release.

Release files for jarn.jsi18n 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jarn.jsi18n 1.1
File Size Uploaded
jarn.jsi18n-1.1.zip 41.8 kB Details

Release files / jarn.jsi18n-1.1.zip

Download URL jarn.jsi18n-1.1.zip
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ed7dd76c17082e50702648767ad7d0b07c84ebda7d9f90a3ce465cfe3477ff18
BLAKE2b-256 checksum
How to use checksums
8b4ab04db1ecd6bf2f866022387a41e02d72a4678c14f2301502770726ce5730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file

1.0

1 release file

0.3

1 release file

0.2

1 release file

0.1.1

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page