PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/components/translation/introduction.rst

https://github.com/tyx/symfony-docs
ReStructuredText | 210 lines | 152 code | 58 blank | 0 comment | 0 complexity | 5346e65bd2474584a8cfb8c6907fd24f MD5 | raw file
  1. .. index::
  2. single: Translation
  3. single: Components; Translation
  4. The Translation Component
  5. =========================
  6. The Translation component provides tools to internationalize your
  7. application.
  8. Installation
  9. ------------
  10. You can install the component in 2 different ways:
  11. * :doc:`Install it via Composer </components/using_components>` (``symfony/translation`` on `Packagist`_);
  12. * Use the official Git repository (https://github.com/symfony/Translation).
  13. Constructing the Translator
  14. ---------------------------
  15. The main access point of the Translation component is
  16. :class:`Symfony\\Component\\Translation\\Translator`. Before you can use it,
  17. you need to configure it and load the messages to translate (called *message
  18. catalogs*).
  19. Configuration
  20. ~~~~~~~~~~~~~
  21. The constructor of the ``Translator`` class needs one argument: The locale.
  22. .. code-block:: php
  23. use Symfony\Component\Translation\Translator;
  24. use Symfony\Component\Translation\MessageSelector;
  25. $translator = new Translator('fr_FR', new MessageSelector());
  26. .. note::
  27. The locale set here is the default locale to use. You can override this
  28. locale when translating strings.
  29. .. note::
  30. The term *locale* refers roughly to the user's language and country. It
  31. can be any string that your application uses to manage translations and
  32. other format differences (e.g. currency format). The `ISO 639-1`_
  33. *language* code, an underscore (``_``), then the `ISO 3166-1 alpha-2`_
  34. *country* code (e.g. ``fr_FR`` for French/France) is recommended.
  35. .. _component-translator-message-catalogs:
  36. Loading Message Catalogs
  37. ~~~~~~~~~~~~~~~~~~~~~~~~
  38. The messages are stored in message catalogs inside the ``Translator``
  39. class. A message catalog is like a dictionary of translations for a specific
  40. locale.
  41. The Translation component uses Loader classes to load catalogs. You can load
  42. multiple resources for the same locale, which will then be combined into one
  43. catalog.
  44. .. versionadded:: 2.4
  45. The ``JsonFileLoader`` was introduced in Symfony 2.4.
  46. The component comes with some default Loaders and you can create your own
  47. Loader too. The default loaders are:
  48. * :class:`Symfony\\Component\\Translation\\Loader\\ArrayLoader` - to load
  49. catalogs from PHP arrays.
  50. * :class:`Symfony\\Component\\Translation\\Loader\\CsvFileLoader` - to load
  51. catalogs from CSV files.
  52. * :class:`Symfony\\Component\\Translation\\Loader\\IcuDatFileLoader` - to load
  53. catalogs form resource bundles.
  54. * :class:`Symfony\\Component\\Translation\\Loader\\IcuResFileLoader` - to load
  55. catalogs form resource bundles.
  56. * :class:`Symfony\\Component\\Translation\\Loader\\IniFileLoader` - to load
  57. catalogs form ini files.
  58. * :class:`Symfony\\Component\\Translation\\Loader\\MoFileLoader` - to load
  59. catalogs form gettext files.
  60. * :class:`Symfony\\Component\\Translation\\Loader\\PhpFileLoader` - to load
  61. catalogs from PHP files.
  62. * :class:`Symfony\\Component\\Translation\\Loader\\PoFileLoader` - to load
  63. catalogs form gettext files.
  64. * :class:`Symfony\\Component\\Translation\\Loader\\QtFileLoader` - to load
  65. catalogs form QT XML files.
  66. * :class:`Symfony\\Component\\Translation\\Loader\\XliffFileLoader` - to load
  67. catalogs from Xliff files.
  68. * :class:`Symfony\\Component\\Translation\\Loader\\JsonFileLoader` - to load
  69. catalogs from JSON files.
  70. * :class:`Symfony\\Component\\Translation\\Loader\\YamlFileLoader` - to load
  71. catalogs from Yaml files (requires the :doc:`Yaml component</components/yaml/introduction>`).
  72. All file loaders require the :doc:`Config component </components/config/index>`.
  73. At first, you should add one or more loaders to the ``Translator``::
  74. // ...
  75. $translator->addLoader('array', new ArrayLoader());
  76. The first argument is the name to which you can refer the loader in the
  77. translator and the second argument is an instance of the loader itself. After
  78. this, you can add your resources using the correct loader.
  79. Loading Messages with the ``ArrayLoader``
  80. .........................................
  81. Loading messages can be done by calling
  82. :method:`Symfony\\Component\\Translation\\Translator::addResource`. The first
  83. argument is the loader name (this was the first argument of the ``addLoader``
  84. method), the second is the resource and the third argument is the locale::
  85. // ...
  86. $translator->addResource('array', array(
  87. 'Hello World!' => 'Bonjour',
  88. ), 'fr_FR');
  89. Loading Messages with the File Loaders
  90. ......................................
  91. If you use one of the file loaders, you should also use the ``addResource``
  92. method. The only difference is that you should put the file name to the resource
  93. file as the second argument, instead of an array::
  94. // ...
  95. $translator->addLoader('yaml', new YamlFileLoader());
  96. $translator->addResource('yaml', 'path/to/messages.fr.yml', 'fr_FR');
  97. The Translation Process
  98. -----------------------
  99. To actually translate the message, the Translator uses a simple process:
  100. * A catalog of translated messages is loaded from translation resources defined
  101. for the ``locale`` (e.g. ``fr_FR``). Messages from the
  102. :ref:`components-fallback-locales` are also loaded and added to the
  103. catalog, if they don't already exist. The end result is a large "dictionary"
  104. of translations;
  105. * If the message is located in the catalog, the translation is returned. If
  106. not, the translator returns the original message.
  107. You start this process by calling
  108. :method:`Symfony\\Component\\Translation\\Translator::trans` or
  109. :method:`Symfony\\Component\\Translation\\Translator::transChoice`. Then, the
  110. Translator looks for the exact string inside the appropriate message catalog
  111. and returns it (if it exists).
  112. .. _components-fallback-locales:
  113. Fallback Locales
  114. ~~~~~~~~~~~~~~~~
  115. If the message is not located in the catalog of the specific locale, the
  116. translator will look into the catalog of one or more fallback locales. For
  117. example, assume you're trying to translate into the ``fr_FR`` locale:
  118. 1. First, the translator looks for the translation in the ``fr_FR`` locale;
  119. 2. If it wasn't found, the translator looks for the translation in the ``fr``
  120. locale;
  121. 3. If the translation still isn't found, the translator uses the one or more
  122. fallback locales set explicitly on the translator.
  123. For (3), the fallback locales can be set by calling
  124. :method:`Symfony\\Component\\Translation\\Translator::setFallbackLocale`::
  125. // ...
  126. $translator->setFallbackLocale(array('en'));
  127. .. _using-message-domains:
  128. Using Message Domains
  129. ---------------------
  130. As you've seen, message files are organized into the different locales that
  131. they translate. The message files can also be organized further into "domains".
  132. The domain is specified in the fourth argument of the ``addResource()``
  133. method. The default domain is ``messages``. For example, suppose that, for
  134. organization, translations were split into three different domains:
  135. ``messages``, ``admin`` and ``navigation``. The French translation would be
  136. loaded like this::
  137. // ...
  138. $translator->addLoader('xliff', new XliffLoader());
  139. $translator->addResource('xliff', 'messages.fr.xliff', 'fr_FR');
  140. $translator->addResource('xliff', 'admin.fr.xliff', 'fr_FR', 'admin');
  141. $translator->addResource('xliff', 'navigation.fr.xliff', 'fr_FR', 'navigation');
  142. When translating strings that are not in the default domain (``messages``),
  143. you must specify the domain as the third argument of ``trans()``::
  144. $translator->trans('Symfony2 is great', array(), 'admin');
  145. Symfony2 will now look for the message in the ``admin`` domain of the
  146. specified locale.
  147. Usage
  148. -----
  149. Read how to use the Translation component in :doc:`/components/translation/usage`.
  150. .. _Packagist: https://packagist.org/packages/symfony/translation
  151. .. _`ISO 3166-1 alpha-2`: http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
  152. .. _`ISO 639-1`: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes