|
| 1 | +.. index:: |
| 2 | + single: Translation |
| 3 | + single: Components; Translation |
| 4 | + |
| 5 | +The Translation Component |
| 6 | +========================= |
| 7 | + |
| 8 | + The Translation component provides tools to internationalize your |
| 9 | + application. |
| 10 | + |
| 11 | +Installation |
| 12 | +------------ |
| 13 | + |
| 14 | +You can install the component in many different ways: |
| 15 | + |
| 16 | +* Use the official Git repository (https://github.com/symfony/Translation); |
| 17 | +* :doc:`Install it via Composer</components/using_components>` (``symfony/translation`` on `Packagist`_). |
| 18 | + |
| 19 | +Usage |
| 20 | +----- |
| 21 | + |
| 22 | +The :class:`Symfony\\Component\\Translation\\Translator` class is the main |
| 23 | +entry point of the Translation component. |
| 24 | + |
| 25 | +.. code-block:: php |
| 26 | +
|
| 27 | + use Symfony\Component\Translation\Translator; |
| 28 | + use Symfony\Component\Translation\MessageSelector; |
| 29 | + use Symfony\Component\Translation\Loader\ArrayLoader; |
| 30 | +
|
| 31 | + $translator = new Translator('fr_FR', new MessageSelector()); |
| 32 | + $translator->addLoader('array', new ArrayLoader()); |
| 33 | + $translator->addResource('array', array( |
| 34 | + 'Hello World!' => 'Bonjour', |
| 35 | + ), 'fr_FR'); |
| 36 | + |
| 37 | + echo $translator->trans('Hello World!'); |
| 38 | +
|
| 39 | +Message Catalogues |
| 40 | +------------------ |
| 41 | + |
| 42 | +The messages are stored in message catalogues inside the ``Translator`` |
| 43 | +class. A Message Catalogue is like a dictionary of translations for a specific |
| 44 | +locale. |
| 45 | + |
| 46 | +Loading catalogues |
| 47 | +~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +The Translation component uses Loader classes to load catalogues. You can load |
| 50 | +multiple resources for the same locale, it will be combined into one |
| 51 | +catalogue. |
| 52 | + |
| 53 | +The component comes with some default Loaders and you can create your own |
| 54 | +Loader too. The default loaders are: |
| 55 | + |
| 56 | +* :class:`Symfony\\Component\\Translation\\Loader\\ArrayLoader` - to load |
| 57 | + catalogues from PHP arrays. |
| 58 | +* :class:`Symfony\\Component\\Translation\\Loader\\CsvFileLoader` - to load |
| 59 | + catalogues from Csv files. |
| 60 | +* :class:`Symfony\\Component\\Translation\\Loader\\PhpFileLoader` - to load |
| 61 | + catalogues from Php files. |
| 62 | +* :class:`Symfony\\Component\\Translation\\Loader\\XliffFileLoader` - to load |
| 63 | + catalogues from Xliff files. |
| 64 | +* :class:`Symfony\\Component\\Translation\\Loader\\YamlFileLoader` - to load |
| 65 | + catalogues from Yaml files (requires the :doc:`Yaml component</components/yaml>`). |
| 66 | + |
| 67 | +All loaders, except the ``ArrayLoader``, requires the |
| 68 | +:doc:`Config component</components/config/index>`. |
| 69 | + |
| 70 | +At first, you should add a loader to the ``Translator``:: |
| 71 | + |
| 72 | + // ... |
| 73 | + $translator->addLoader('array', new ArrayLoader()); |
| 74 | + |
| 75 | +The first argument is the key to which we can refer the loader in the translator |
| 76 | +and the second argument is an instance of the loader itself. After this, you |
| 77 | +can add your resources using the correct loader. |
| 78 | + |
| 79 | +Loading Messages with the ``ArrayLoader`` |
| 80 | +......................................... |
| 81 | + |
| 82 | +Loading messages can be done by calling |
| 83 | +:method:`Symfony\\Component\\Translation\\Translator::addResource`. The first |
| 84 | +argument is the loader name (the first argument of the ``addLoader`` |
| 85 | +method), the second is the resource and the third argument is the locale:: |
| 86 | + |
| 87 | + // ... |
| 88 | + $translator->addResource('array', array( |
| 89 | + 'Hello World!' => 'Bonjour', |
| 90 | + ), 'fr_FR'); |
| 91 | + |
| 92 | +Loading Messages with the File Loaders |
| 93 | +...................................... |
| 94 | + |
| 95 | +If you use one of the file loaders, you also use the ``addResource`` method. |
| 96 | +The only difference is that you put the file name as the second argument, |
| 97 | +instead of an array:: |
| 98 | + |
| 99 | + // ... |
| 100 | + $translator->addLoader('yaml', new YamlFileLoader()); |
| 101 | + $translator->addResource('yaml', 'path/to/messages.fr.yml', 'fr_FR'); |
| 102 | + |
| 103 | +Translate Strings |
| 104 | +----------------- |
| 105 | + |
| 106 | +After you have loaded your Message Catalogues, you can begin to translate your |
| 107 | +strings. This is done with the |
| 108 | +:method:`Symfony\\Component\\Translation\\Translator::trans` method:: |
| 109 | + |
| 110 | + // ... |
| 111 | + $translator->addResource('array', array( |
| 112 | + 'Hello World!' => 'Bonjour', |
| 113 | + ), 'fr_FR'); |
| 114 | + $translator->addResource('array', array( |
| 115 | + 'Hello World!' => 'Hello World', |
| 116 | + ), 'en_GB'); |
| 117 | + |
| 118 | + echo $translator->trans('Hello World!'); |
| 119 | + // >> 'Bonjour' |
| 120 | + |
| 121 | +By default, the ``trans`` method uses the locale that is set in the |
| 122 | +constructor of the ``Translator``. If you want to translate another locale, |
| 123 | +you can change that by setting the fourth argument to the locale:: |
| 124 | + |
| 125 | + // ... |
| 126 | + echo $translator->trans('Hello World!', array(), 'messages', 'en_GB'); |
| 127 | + // >> 'Hello World!' |
| 128 | + |
| 129 | +Learn More |
| 130 | +---------- |
| 131 | + |
| 132 | +The Translation component can do a lot more things. Read more about the usage |
| 133 | +of this component in :ref:`the Translation book article <basic-translation>`. |
| 134 | +That article is specific about the Translation component in the Symfony2 |
| 135 | +Framework, but most of the article is framework independent. |
| 136 | + |
| 137 | +.. _Packagist: https://packagist.org/packages/symfony/translation |
0 commit comments