| 555 | | Adding translations to JavaScript poses some new problems. The main problem |
|---|
| 556 | | is, your JavaScript code doesn't have access to a readily available ``gettext`` |
|---|
| 557 | | implementation. The second problem is, your JavaScript code doesn't have access |
|---|
| 558 | | to .po or .mo files - they need to be delivered by the server. Additionally you |
|---|
| 559 | | want to keep those translation catalogs as small as possible. Django provides |
|---|
| 560 | | an integrated solution for all these problems. |
|---|
| 561 | | |
|---|
| 562 | | The ``javascript_catalog`` view function |
|---|
| 563 | | ---------------------------------------- |
|---|
| 564 | | |
|---|
| 565 | | This is a generic view that will send out a JavaScript code library with functions |
|---|
| 566 | | that mimick the ``gettext`` interface and an array with translation strings. Those |
|---|
| 567 | | translation strings are taken from the application, project or django core, just |
|---|
| 568 | | as you specifiy in either the info_dict or the URL. |
|---|
| | 555 | Adding translations to JavaScript poses some problems: |
|---|
| | 556 | |
|---|
| | 557 | * JavaScript code doesn't have access to a ``gettext`` implementation. |
|---|
| | 558 | |
|---|
| | 559 | * JavaScript code doesn't have access to .po or .mo files; they need to be |
|---|
| | 560 | delivered by the server. |
|---|
| | 561 | |
|---|
| | 562 | * The translation catalogs for JavaScript should be kept as small as |
|---|
| | 563 | possible. |
|---|
| | 564 | |
|---|
| | 565 | Django provides an integrated solution for these problems: It passes the |
|---|
| | 566 | translations into JavaScript, so you can call ``gettext``, etc., from within |
|---|
| | 567 | JavaScript. |
|---|
| | 568 | |
|---|
| | 569 | The ``javascript_catalog`` view |
|---|
| | 570 | ------------------------------- |
|---|
| | 571 | |
|---|
| | 572 | The main solution to these problems is the ``javascript_catalog`` view, which |
|---|
| | 573 | sends out a JavaScript code library with functions that mimick the ``gettext`` |
|---|
| | 574 | interface, plus an array of translation strings. Those translation strings are |
|---|
| | 575 | taken from the application, project or Django core, according to what you |
|---|
| | 576 | specify in either the {{{info_dict}}} or the URL. |
|---|
| 580 | | The package specifications are the same as with ``INSTALLED_APPS``. You can |
|---|
| 581 | | specify multiple packages - in that case all those catalogs are merged into |
|---|
| 582 | | one catalog. This is usefull if you have JavaScript that uses strings from different |
|---|
| 583 | | applications. |
|---|
| 584 | | |
|---|
| 585 | | Additionally you can make the view dynamic by putting the packages into the URL |
|---|
| 586 | | specification:: |
|---|
| | 588 | Each string in ``packages`` should be in Python dotted-package syntax (the |
|---|
| | 589 | same format as the strings in ``INSTALLED_APPS``) and should refer to a package |
|---|
| | 590 | that contains a ``locale`` directory. If you specify multiple packages, all |
|---|
| | 591 | those catalogs aremerged into one catalog. This is useful if you have |
|---|
| | 592 | JavaScript that uses strings from different applications. |
|---|
| | 593 | |
|---|
| | 594 | You can make the view dynamic by putting the packages into the URL pattern:: |
|---|
| 592 | | This way you can specify the packages as a list of package names delimited by '+' |
|---|
| 593 | | signs in the URL. This is especially useful if your pages use code from different |
|---|
| 594 | | apps and this changes often and you don't want to pull in one big catalog file. |
|---|
| 595 | | Packages are limited to either ``django.conf`` or any package from the ``INSTALLED_APPS`` |
|---|
| 596 | | setting. |
|---|
| | 600 | With this, you specify the packages as a list of package names delimited by '+' |
|---|
| | 601 | signs in the URL. This is especially useful if your pages use code from |
|---|
| | 602 | different apps and this changes often and you don't want to pull in one big |
|---|
| | 603 | catalog file. As a security measure, these values can only be either |
|---|
| | 604 | ``django.conf`` or any package from the ``INSTALLED_APPS`` setting. |
|---|
| 624 | | The interpolation syntax is borrowed from Python. You shouldn't go over the top with |
|---|
| 625 | | string interpolation, though: this is still JavaScript, so the code will have to do |
|---|
| 626 | | repeated regular expression substituions. This isn't as fast as string interpolation |
|---|
| 627 | | in Python, so keep it to those cases where you really need it (for example in conjunction with |
|---|
| 628 | | ngettext to produce proper pluralizations). |
|---|
| | 631 | The interpolation syntax is borrowed from Python. You shouldn't go over the top |
|---|
| | 632 | with string interpolation, though: this is still JavaScript, so the code will |
|---|
| | 633 | have to do repeated regular-expression substitutions. This isn't as fast as |
|---|
| | 634 | string interpolation in Python, so keep it to those cases where you really |
|---|
| | 635 | need it (for example, in conjunction with ``ngettext`` to produce proper |
|---|
| | 636 | pluralizations). |
|---|