doc/book/en/development/devweb/internationalization.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 08 Feb 2010 21:21:31 +0100
changeset 4507 57927ad07e63
parent 4442 7bc0e4ed4109
child 4751 1a9d2c3c7f0c
permissions -rw-r--r--
no need for a specific vid argument here
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
     3
.. _internationalization:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2539
diff changeset
     5
Internationalization
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
---------------------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2535
diff changeset
     8
Cubicweb fully supports the internalization of its content and interface.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
Cubicweb's interface internationalization is based on the translation project `GNU gettext`_.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
.. _`GNU gettext`: http://www.gnu.org/software/gettext/
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
Cubicweb' internalization involves two steps:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
* in your Python code and cubicweb-tal templates : mark translatable strings
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
    18
* in your instance : handle the translation catalog
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
String internationalization
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
~~~~~~~~~~~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
3992
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    23
User defined string
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    24
```````````````````
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    25
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
In the Python code and cubicweb-tal templates translatable strings can be
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
marked in one of the following ways :
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
 * by using the *built-in* function `_` ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
     class PrimaryView(EntityView):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
         """the full view of an non final entity"""
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
         id = 'primary'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
         title = _('primary')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
  OR
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    38
 * by using the equivalent request's method ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
     class NoResultView(EmptyRsetView):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
         """default view when no result has been found"""
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
         id = 'noresult'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
         def call(self, **kwargs):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
             self.w(u'<div class="searchMessage"><strong>%s</strong></div>\n'
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4255
diff changeset
    46
                 % self._cw._('No result matching query'))
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
The goal of the *built-in* function `_` is only **to mark the
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
translatable strings**, it will only return the string to translate
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    50
itself, but not its translation (it's actually another name for the
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    51
`unicode` builtin).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    53
In the other hand the request's method `self._cw._` is meant to retrieve the
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    54
proper translation of translation strings in the requested language.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    56
Finally you can also use the `__` attribute of request object to get a
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
translation for a string *which should not itself added to the catalog*,
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
usually in case where the actual msgid is created by string interpolation ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4255
diff changeset
    60
  self._cw.__('This %s' % etype)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4255
diff changeset
    62
In this example ._cw.__` is used instead of ._cw._` so we don't have 'This %s' in
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
messages catalogs.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
Translations in cubicweb-tal template can also be done with TAL tags
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
`i18n:content` and `i18n:replace`.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
2535
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2476
diff changeset
    70
If you need to add messages on top of those that can be found in the source,
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2476
diff changeset
    71
you can create a file named `i18n/static-messages.pot`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
3992
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    73
Generated string
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    74
````````````````
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    75
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    76
We do not need to mark the translation strings of entities/relations used by a
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    77
particular instance's schema as they are generated automatically. String for
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    78
various actions are also generated.
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    79
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    80
For exemple the following schema ::
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    81
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    82
  Class EntityA(EntityType):
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    83
      relationa2b = SubjectRelation('EntityB')
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    84
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    85
  class EntityB(EntityType):
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    86
      pass
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    87
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    88
May generate the following message ::
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    89
4255
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
    90
  add Execution has_export File subject
3992
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    91
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    92
This message will be used in views of ``EntityA`` for creation of a new
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    93
``EntityB`` with a preset relation ``relation_a2b`` between the current
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    94
``EntityA`` and the new ``EntityB``. The opposite message ::
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    95
4255
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
    96
  add Execution has_export File object
3992
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
    97
4255
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
    98
Is used for similar creation of an ``EntityA`` from a view of ``EntityB``. The
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
    99
title of they respective creation form will be ::
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
   100
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
   101
  creating EntityB (EntityA %(linkto)s relation_a2b EntityB)
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
   102
9d9400cb3f8c update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3992
diff changeset
   103
  creating EntityA (EntityA relation_a2b %(linkto)s EntityA)
3992
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
   104
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
   105
In the translated string you can use ``%(linkto)s`` for reference to the source
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
   106
``entity``.
4c98f0e28429 update i18n doc
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3581
diff changeset
   107
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
Handle the translation catalog
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   111
Once the internationalization is done in your code, you need to populate and
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   112
update the translation catalog. Cubicweb provides the following commands for this
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   113
purpose:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
1898
39b37f90a8a4 [cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   116
* `i18ncubicweb` updates Cubicweb framework's translation
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   117
  catalogs. Unless you actually work on the framework itself, you
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   118
  don't need to use this command.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
1898
39b37f90a8a4 [cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   120
* `i18ncube` updates the translation catalogs of *one particular
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   121
  cube* (or of all cubes). After this command is
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
  executed you must update the translation files *.po* in the "i18n"
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
  directory of your template. This command will of course not remove
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
  existing translations still in use.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   126
* `i18ninstance` recompiles the translation catalogs of *one particular
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
  instance* (or of all instances) after the translation catalogs of
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   128
  its cubes have been updated. This command is automatically
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
  called every time you create or update your instance. The compiled
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
  catalogs (*.mo*) are stored in the i18n/<lang>/LC_MESSAGES of
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   131
  instance where `lang` is the language identifier ('en' or 'fr'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
  for exemple).
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   135
Example
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
```````
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   137
You have added and/or modified some translation strings in your cube
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   138
(after creating a new view or modifying the cube's schema for exemple).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
To update the translation catalogs you need to do:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   141
1. `cubicweb-ctl i18ncube <cube>`
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   142
2. Edit the <cube>/i18n/xxx.po  files and add missing translations (empty `msgstr`)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
3. `hg ci -m "updated i18n catalogs"`
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1898
diff changeset
   144
4. `cubicweb-ctl i18ninstance <myinstance>`
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   145