Django

Code

Changeset 6294

Show
Ignore:
Timestamp:
09/15/07 12:32:53 (1 year ago)
Author:
adrian
Message:

Fixed #5495 -- Made some organization / spelling / grammar changes to docs/i18n.txt. Thanks, Axis_of_Entropy

Files:

Legend:

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

    r6286 r6294  
    2828      to their language preferences. 
    2929 
    30 How to internationalize your app: in three steps 
    31 ------------------------------------------------ 
    32  
    33     1. Embed translation strings in your Python code and templates. 
    34     2. Get translations for those strings, in whichever languages you want to 
    35        support. 
    36     3. Activate the locale middleware in your Django settings. 
    37  
    38 .. admonition:: Behind the scenes 
    39  
    40     Django's translation machinery uses the standard ``gettext`` module that 
    41     comes with Python. 
    42  
    43 If you don't need internationalization 
    44 ====================================== 
     30If you don't need internationalization in your app 
     31================================================== 
    4532 
    4633Django's internationalization hooks are on by default, and that means there's a 
     
    5643.. _documentation for USE_I18N: ../settings/#use-i18n 
    5744 
    58 How to specify translation strings 
    59 ================================== 
     45If you do need internationalization: three steps 
     46================================================ 
     47 
     48    1. Embed translation strings in your Python code and templates. 
     49    2. Get translations for those strings, in whichever languages you want to 
     50       support. 
     51    3. Activate the locale middleware in your Django settings. 
     52 
     53.. admonition:: Behind the scenes 
     54 
     55    Django's translation machinery uses the standard ``gettext`` module that 
     56    comes with Python. 
     57 
     581. How to specify translation strings 
     59===================================== 
    6060 
    6161Translation strings specify "This text should be translated." These strings can 
     
    296296 
    297297Working with lazy translation objects 
    298 ===================================== 
     298------------------------------------- 
    299299 
    300300Using ``ugettext_lazy()`` and ``ungettext_lazy()`` to mark strings in models 
     
    306306 
    307307Joining strings: string_concat() 
    308 -------------------------------- 
     308~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    309309 
    310310Standard Python string joins (``''.join([...])``) will not work on lists 
     
    325325 
    326326The allow_lazy() decorator 
    327 -------------------------- 
     327~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    328328 
    329329Django offers many utility functions (particularly in ``django.utils``) that 
     
    360360end. 
    361361 
    362 How to create language files 
    363 ============================ 
     3622. How to create language files 
     363=============================== 
    364364 
    365365Once you've tagged your strings for later translation, you need to write (or 
     
    394394...where ``de`` is the language code for the message file you want to create. 
    395395The language code, in this case, is in locale format. For example, it's 
    396 ``pt_BR`` for Brazilian Portugese and ``de_AT`` for Austrian German. 
     396``pt_BR`` for Brazilian Portuguese and ``de_AT`` for Austrian German. 
    397397 
    398398The script should be run from one of three places: 
     
    491491    .. _Submitting and maintaining translations: ../contributing/ 
    492492 
    493 How Django discovers language preference 
    494 ======================================== 
     4933. How Django discovers language preference 
     494=========================================== 
    495495 
    496496Once you've prepared your translations -- or, if you just want to use the 
     
    547547 
    548548    * In each of these places, the language preference is expected to be in the 
    549       standard language format, as a string. For example, Brazilian Portugese 
     549      standard language format, as a string. For example, Brazilian Portuguese 
    550550      is ``pt-br``. 
    551551    * If a base language is available but the sublanguage specified is not, 
     
    630630.. _request object: ../request_response/#httprequest-objects 
    631631 
     632Using translations in your own projects 
     633======================================= 
     634 
     635Django looks for translations by following this algorithm: 
     636 
     637    * First, it looks for a ``locale`` directory in the application directory 
     638      of the view that's being called. If it finds a translation for the 
     639      selected language, the translation will be installed. 
     640    * Next, it looks for a ``locale`` directory in the project directory. If it 
     641      finds a translation, the translation will be installed. 
     642    * Finally, it checks the base translation in ``django/conf/locale``. 
     643 
     644This way, you can write applications that include their own translations, and 
     645you can override base translations in your project path. Or, you can just build 
     646a big project out of several apps and put all translations into one big project 
     647message file. The choice is yours. 
     648 
     649.. note:: 
     650 
     651    If you're using manually configured settings, as described in the 
     652    `settings documentation`_, the ``locale`` directory in the project 
     653    directory will not be examined, since Django loses the ability to work out 
     654    the location of the project directory. (Django normally uses the location 
     655    of the settings file to determine this, and a settings file doesn't exist 
     656    if you're manually configuring your settings.) 
     657 
     658.. _settings documentation: ../settings/#using-settings-without-the-django-settings-module-environment-variable 
     659 
     660All message file repositories are structured the same way. They are: 
     661 
     662    * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
     663    * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
     664    * All paths listed in ``LOCALE_PATHS`` in your settings file are 
     665      searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)`` 
     666    * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
     667 
     668To create message files, you use the same ``make-messages.py`` tool as with the 
     669Django message files. You only need to be in the right place -- in the directory 
     670where either the ``conf/locale`` (in case of the source tree) or the ``locale/`` 
     671(in case of app messages or project messages) directory are located. And you 
     672use the same ``compile-messages.py`` to produce the binary ``django.mo`` files that 
     673are used by ``gettext``. 
     674 
     675Application message files are a bit complicated to discover -- they need the 
     676``LocaleMiddleware``. If you don't use the middleware, only the Django message 
     677files and project message files will be processed. 
     678 
     679Finally, you should give some thought to the structure of your translation 
     680files. If your applications need to be delivered to other users and will 
     681be used in other projects, you might want to use app-specific translations. 
     682But using app-specific translations and project translations could produce 
     683weird problems with ``make-messages``: ``make-messages`` will traverse all 
     684directories below the current path and so might put message IDs into the 
     685project message file that are already in application message files. 
     686 
     687The easiest way out is to store applications that are not part of the project 
     688(and so carry their own translations) outside the project tree. That way, 
     689``make-messages`` on the project level will only translate strings that are 
     690connected to your explicit project and not strings that are distributed 
     691independently. 
     692 
    632693The ``set_language`` redirect view 
    633694================================== 
     
    652713    * Django looks for a ``next`` parameter in ``POST`` request. 
    653714    * If that doesn't exist, or is empty, Django tries the URL in the 
    654       ``Referer`` header. 
     715      ``Referrer`` header. 
    655716    * If that's empty -- say, if a user's browser suppresses that header -- 
    656717      then the user will be redirected to ``/`` (the site root) as a fallback. 
     
    668729    </form> 
    669730 
    670 Using translations in your own projects 
    671 ======================================= 
    672  
    673 Django looks for translations by following this algorithm: 
    674  
    675     * First, it looks for a ``locale`` directory in the application directory 
    676       of the view that's being called. If it finds a translation for the 
    677       selected language, the translation will be installed. 
    678     * Next, it looks for a ``locale`` directory in the project directory. If it 
    679       finds a translation, the translation will be installed. 
    680     * Finally, it checks the base translation in ``django/conf/locale``. 
    681  
    682 This way, you can write applications that include their own translations, and 
    683 you can override base translations in your project path. Or, you can just build 
    684 a big project out of several apps and put all translations into one big project 
    685 message file. The choice is yours. 
    686  
    687 .. note:: 
    688  
    689     If you're using manually configured settings, as described in the 
    690     `settings documentation`_, the ``locale`` directory in the project 
    691     directory will not be examined, since Django loses the ability to work out 
    692     the location of the project directory. (Django normally uses the location 
    693     of the settings file to determine this, and a settings file doesn't exist 
    694     if you're manually configuring your settings.) 
    695  
    696 .. _settings documentation: ../settings/#using-settings-without-the-django-settings-module-environment-variable 
    697  
    698 All message file repositories are structured the same way. They are: 
    699  
    700     * ``$APPPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
    701     * ``$PROJECTPATH/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
    702     * All paths listed in ``LOCALE_PATHS`` in your settings file are 
    703       searched in that order for ``<language>/LC_MESSAGES/django.(po|mo)`` 
    704     * ``$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)`` 
    705  
    706 To create message files, you use the same ``make-messages.py`` tool as with the 
    707 Django message files. You only need to be in the right place -- in the directory 
    708 where either the ``conf/locale`` (in case of the source tree) or the ``locale/`` 
    709 (in case of app messages or project messages) directory are located. And you 
    710 use the same ``compile-messages.py`` to produce the binary ``django.mo`` files that 
    711 are used by ``gettext``. 
    712  
    713 Application message files are a bit complicated to discover -- they need the 
    714 ``LocaleMiddleware``. If you don't use the middleware, only the Django message 
    715 files and project message files will be processed. 
    716  
    717 Finally, you should give some thought to the structure of your translation 
    718 files. If your applications need to be delivered to other users and will 
    719 be used in other projects, you might want to use app-specific translations. 
    720 But using app-specific translations and project translations could produce 
    721 weird problems with ``make-messages``: ``make-messages`` will traverse all 
    722 directories below the current path and so might put message IDs into the 
    723 project message file that are already in application message files. 
    724  
    725 The easiest way out is to store applications that are not part of the project 
    726 (and so carry their own translations) outside the project tree. That way, 
    727 ``make-messages`` on the project level will only translate strings that are 
    728 connected to your explicit project and not strings that are distributed 
    729 independently. 
    730  
    731731Translations and JavaScript 
    732732=========================== 
     
    828828way as you do with normal Django translation catalogs. 
    829829 
    830 Specialities of Django translation 
     830Specialties of Django translation 
    831831================================== 
    832832 
    833 If you know ``gettext``, you might note these specialities in the way Django 
     833If you know ``gettext``, you might note these specialties in the way Django 
    834834does translation: 
    835835 
    836     * The string domain is ``django`` or ``djangojs``. The string domain is 
     836    * The string domain is ``django`` or ``djangojs``. This string domain is 
    837837      used to differentiate between different programs that store their data 
    838838      in a common message-file library (usually ``/usr/share/locale/``). The 
     
    842842      that those are as small as possible. 
    843843    * Django doesn't use ``xgettext`` alone. It uses Python wrappers around 
    844       ``xgettext`` and ``msgfmt``. That's mostly for convenience. 
    845  
     844      ``xgettext`` and ``msgfmt``. This is mostly for convenience. 
     845