Coder Social home page Coder Social logo

jarn.jsi18n's Introduction

Introduction

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.

jarn.jsi18n's People

Contributors

vangheem avatar ggozad avatar kysr avatar rockfruit avatar

Stargazers

Rafal Kasa avatar Frank avatar Andreas Zeidler avatar

Watchers

David Bain avatar Dylan Jay avatar David Glick avatar Tim Knapp avatar TsungWei Hu avatar Jesse Snyder avatar jianai avatar Rob Porter avatar Vincent Fretin avatar Sean Chen avatar Rodrigo Ferreira de Souza avatar Shane Graber avatar Mauro Amico avatar Leonardo J. Caballero G. avatar Esa-Matti Suuronen avatar Robert Niederreiter avatar Lukas Zdych avatar Thiago Curvelo avatar Cleber J Santos avatar Tom Gross avatar Thomas Desvenain avatar Gildardo Bautista avatar Peter Holzer avatar Rudá Porto Filgueiras avatar Armin Stross-Radschinski avatar Cris Ewing avatar Davi Medeiros avatar Philip Bauer avatar Rijk Stofberg avatar  avatar Fulvio Casali avatar David Siedband avatar Eric BREHAULT avatar Zoltan Szabo avatar Takashi NAGAI avatar Manabu TERADA avatar  avatar Joao S. O. Bueno avatar Jean-Philippe Camguilhem  avatar  avatar Davide Moro avatar Benoît Suttor avatar Danny Butaye avatar Rizwan avatar andreasma avatar  avatar Georg Gogo. BERNHARD avatar 4teamwork Jenkins avatar Jiang dongjin avatar Roché Compaan avatar T. Kim Nguyen avatar Thomas Clement Mogensen avatar James Cloos avatar  avatar Hans-Peter Locher avatar Ralph Jacobs avatar Roman Kozlovskyi avatar Chris Calloway avatar Hugo Rodriguez avatar Juan Carlos Coruña avatar André Nogueira avatar Maik Röder avatar Khairil Yusof avatar Espen Moe-Nilssen avatar  avatar Laurent Lasudry avatar tiazma avatar Harald Friessnegger avatar Vito Falco avatar Hanno Schulz avatar Gil Forcada Codinachs avatar André NUYENS avatar  avatar DHietpas avatar Bogdan Girman avatar Stéphane Moussiaux avatar  avatar Max Jakob avatar  avatar Gauthier Bastien avatar Stefaner Benjamin avatar Luca avatar Lucas Aquino avatar Alice Tseng avatar  avatar Ian Anderson avatar Federica D'Elia avatar Adam avatar Jason Craig avatar Julien Stegle avatar Jess Henderson avatar Rafahela avatar Christian-Xavier Prosperini avatar Julien Marinescu avatar Simon Delcourt avatar Carlos Malave avatar Bogdan Ciobanu avatar  avatar Jeff Terstriep avatar Adriana Ramírez Vigueras avatar

Forkers

braytonosg xispa

jarn.jsi18n's Issues

jarn.jsi18n uses the first matched catalog

Hello,

I am under the impression, that jarn.jsi18n uses the first catalog it comes across for a particular domain and language. This is troublesome for my use case, where I am going to update an existing domain/language by just few entries. After checking the localStorage, I have noticed that jarn.jsi18n is purely using my small catalog and ignores the original one.

This behaviour is contrary to the usual Plone translation machinery, that takes every matching catalog into consideration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.