Django

Code

Changeset 1532

Show
Ignore:
Timestamp:
12/04/05 07:08:37 (3 years ago)
Author:
hugo
Message:

updated the i18n documentation with regard to JavaScript? translations

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/i18n.txt

    r1209 r1532  
    550550independently. 
    551551 
     552Translations and JavaScript 
     553=========================== 
     554 
     555Adding translations to JavaScript poses some new problems. The main problem 
     556is, your JavaScript code doesn't have access to a readily available ``gettext`` 
     557implementation. The second problem is, your JavaScript code doesn't have access 
     558to .po or .mo files - they need to be delivered by the server. Additionally you 
     559want to keep those translation catalogs as small as possible. Django provides 
     560an integrated solution for all these problems. 
     561 
     562The ``javascript_catalog`` view function 
     563---------------------------------------- 
     564 
     565This is a generic view that will send out a JavaScript code library with functions 
     566that mimick the ``gettext`` interface and an array with translation strings. Those 
     567translation strings are taken from the application, project or django core, just 
     568as you specifiy in either the info_dict or the URL. 
     569 
     570You hook it up like this:: 
     571 
     572    js_info_dict = { 
     573        'packages': ('your.app.package',), 
     574    } 
     575 
     576    urlpatterns = patterns('', 
     577        (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), 
     578    ) 
     579 
     580The package specifications are the same as with ``INSTALLED_APPS``. You can 
     581specify multiple packages - in that case all those catalogs are merged into 
     582one catalog. This is usefull if you have JavaScript that uses strings from different 
     583applications. 
     584 
     585Additionally you can make the view dynamic by putting the packages into the URL 
     586specification:: 
     587 
     588    urlpatterns = patterns('', 
     589        (r'^jsi18n/(?P<packages>\S+?)/$, 'django.views.i18n.javascript_catalog'), 
     590    ) 
     591 
     592This way you can specify the packages as a list of package names delimited by '+' 
     593signs in the URL. This is especially useful if your pages use code from different 
     594apps and this changes often and you don't want to pull in one big catalog file. 
     595Packages are limited to either ``django.conf`` or any package from the ``INSTALLED_APPS`` 
     596setting. 
     597 
     598Using the JavaScript translation catalog 
     599---------------------------------------- 
     600 
     601To make use of the catalog, you just have to pull in the dynamically generated 
     602script like this:: 
     603 
     604    <script type="text/javascript" src="../../../jsi18n/"></script> 
     605 
     606This is how the admin fetches the translation catalog from the server. When the 
     607catalog is loaded, your JavaScript code can use the standard ``gettext`` interface 
     608to access it:: 
     609 
     610    document.write(gettext('this is to be translated')); 
     611 
     612There even is a ``ngettext`` interface and a string interpolation function:: 
     613 
     614    d = { 
     615        count: 10 
     616    }; 
     617    s = interpolate(ngettext('this is %(count)s object', 'this are %(count)s objects', d.count), d); 
     618 
     619The ``interpolate`` function both supports positional interpolation and named interpolation. 
     620So the above could have been written as:: 
     621 
     622    s = interpolate(ngettext('this is %s object', 'this are %s objects', 11), 11); 
     623 
     624The interpolation syntax is borrowed from Python. You shouldn't go over the top with 
     625string interpolation, though: this is still JavaScript, so the code will have to do 
     626repeated regular expression substituions. This isn't as fast as string interpolation 
     627in Python, so keep it to those cases where you really need it (for example in conjunction with 
     628ngettext to produce proper pluralizations). 
     629 
     630Creating JavaScript translation catalogs 
     631---------------------------------------- 
     632 
     633You create and update the translation catalogs the same way as the other django 
     634translation catalogs with the make-messages.py tool. Only difference is, you have 
     635to provide a ``-d djangojs`` parameter like this:: 
     636 
     637    make-messages.py -d djangojs -l de 
     638 
     639This would create or update the translation catalog for JavaScript for German. 
     640After updating translation catalogs, just run ``compile-messages.py`` the same 
     641way as you do with normal django translation catalogs. 
     642 
    552643Specialities of Django translation 
    553644================================== 
     
    556647does translation: 
    557648 
    558     * The string domain is always ``django``. The string domain is used to 
     649    * The string domain is ``django`` or ``djangojs``. The string domain is used to 
    559650      differentiate between different programs that store their data in a 
    560       common message-file library (usually ``/usr/share/locale/``). In Django's 
    561       case, there are Django-specific locale libraries, so the domain itself 
    562       isn't used. We could store app message files with different names and put 
    563       them, say, in the project library, but we decided against this. With 
    564       message files in the application tree, apps can be distributed more 
    565       easily. 
     651      common message-file library (usually ``/usr/share/locale/``). The ``django`` 
     652      domain is used for python and template translation strings and is loaded into 
     653      the global translation catalogs. The ``djangojs`` domain is only used for 
     654      JavaScript translation catalogs to make sure that those are as small as 
     655      possible. 
    566656    * Django only uses ``gettext`` and ``gettext_noop``. That's because Django 
    567657      always uses ``DEFAULT_CHARSET`` strings internally. There isn't much use