Društvo LJUDMILA
Rozmanova ulica 12
1000 Ljubljana
Slovenia
Prostori: osmo/za

JavaScript Localization Library

(Preusmerjeno s strani Locale.js)

Disclaimer

This is work in progress. It is not complete yet.

Scenarios

Kiki must make his AJAX web applications work with several different human languages. He needs a JavaScript library to help him with this. The library provides functions which work with familiar semantics.


Download it here

Nongoals

Not a complete AI human language translator. Must be actually useful for writing JavaScript programs.

Overview

What we want

Small library in style of jQuery for localization/translation functions; Useful for AJAX web apps and such. Contained in a locale object. Message catalogs are also linked in this object.

Loading a library:

<script type="text/javascript" src="http://kulturnik.ljudmila.net/script/locale.js"></script>

Loading catalogs for slovene and french

<script type="text/javascript" src="http://kulturnik.ljudmila.net/web/form/event/l10n/sl.js"></script>
<script type="text/javascript" src="http://kulturnik.ljudmila.net/web/form/event/l10n/fr.js"></script>

Normally, you would want to load only catalogs for languages actually used.

Setting a language:

locale.lang('sl');

If language is not set, locale will try to determine it from browser settings. This is normally the setting of browser's GUI, so it might not match the language set in the web application session. For best results, web application should call locale.lang() function to set the language explicitly.

Gettext interface:

gettext(string)
gettext(string, string, number)

_ is an alias for gettext

_('yes')
_('result','results',n)

As gettext method on string:

a = 'Hello'; a.gettext();
b = 'yes'; b._();

Catalogs can be preloaded with page. Also possible is loading a catalog at runtime:

locale.catalog('http://mysite.org/catalog/en.js')

Setting a domain:

locale.domain('my_app');

Default domain is core;

Details

Catalogs

Catalogs can come from various sources, must be merged or something.

Message catalogs can be stored:

  1. in JavaScript variables
  2. in DOM elements (not yet)

In JavaScript:

locale.add_catalog('sl','my_app',{
  'result': ['rezultat','rezultata','rezultati','rezultatov']
  'yes': 'da',
  'no': 'ne'
 });

Open Issues

locale.sprintf() function

javascript desperately needs sprintf() like function.

locale.nsl_sprintf() function

javascript desperately needs localizable sprintf() like function.

Instead of:

sprintf(format,args...)

it would look like this:

nsl_sprintf(string_singular,string_plural,count,args,...)

which is actually longer than:

sprintf(_(string_singular,string_plural,count),args...)


locale.wrap function

Other

  1. Methods for auto-translating DOM elements??
  2. human_date() date formatting function
  3. Collect lists of wanted strings not in catalogs...