Skip to content

Commit fb1fa0d

Browse files
committed
minor #15310 [Translation] Introduce Translation Providers (welcoMattic)
This PR was submitted for the 5.4 branch but it was squashed and merged into the 5.3 branch instead. Discussion ---------- [Translation] Introduce Translation Providers Docs for symfony/symfony#38475, symfony/symfony#40926, symfony/symfony#40927, and symfony/symfony#40947 Ready for first review, but I'm not sure that I've written documentation in the right and all required places. ATM, Translation Providers Bridges packages doesn't exists, so Flex recipes are not created yet. Commits ------- 943a63f [Translation] Introduce Translation Providers
2 parents 24fdd84 + 943a63f commit fb1fa0d

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

reference/configuration/framework.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ Configuration
283283
* `formatter`_
284284
* `logging`_
285285
* :ref:`paths <reference-translator-paths>`
286+
* :ref:`providers <reference-translator-providers>`
286287

287288
* `trusted_headers`_
288289
* `trusted_hosts`_
@@ -2455,6 +2456,19 @@ default_path
24552456
This option allows to define the path where the application translations files
24562457
are stored.
24572458

2459+
.. _reference-translator-providers:
2460+
2461+
providers
2462+
.........
2463+
2464+
**type**: ``array`` **default**: ``[]``
2465+
2466+
This option allows you to enable translation providers to push and pull your translations to third party providers.
2467+
2468+
.. seealso::
2469+
2470+
For more information about how to configure providers, see :ref:`translation-providers`.
2471+
24582472
property_access
24592473
~~~~~~~~~~~~~~~
24602474

translation.rst

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,158 @@ if you're generating translations with specialized programs or teams.
591591
:class:`Symfony\\Component\\Translation\\Loader\\LoaderInterface` interface.
592592
See the :ref:`dic-tags-translation-loader` tag for more information.
593593

594+
.. _translation-providers:
595+
596+
Translation Providers
597+
---------------------
598+
599+
.. versionadded:: 5.3
600+
601+
The "Translation Providers" feature was introduced in Symfony 5.3 as an
602+
:doc:`experimental feature </contributing/code/experimental>`.
603+
604+
The translation component can push and pull translations to third party providers (e.g. Crowdin or PoEditor).
605+
606+
Installing and configuring a 3rd Party Provider
607+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
608+
609+
Instead of editing your translation files as code, you can push them to one of the supported third-party providers.
610+
This will make the translations edition easier for you or your translators.
611+
612+
==================== ===========================================================
613+
Service Install with
614+
==================== ===========================================================
615+
Crowdin ``composer require symfony/crowdin-translation-provider``
616+
Loco (localise.biz) ``composer require symfony/loco-translation-provider``
617+
Lokalise ``composer require symfony/lokalise-translation-provider``
618+
PoEditor ``composer require symfony/po-editor-translation-provider``
619+
==================== ===========================================================
620+
621+
Each library includes a :ref:`Symfony Flex recipe <symfony-flex>` that will add
622+
a configuration example to your ``.env`` file. For example, suppose you want to
623+
use Loco. First, install it:
624+
625+
.. code-block:: terminal
626+
627+
$ composer require symfony/loco-translation-provider
628+
629+
You'll now have a new line in your ``.env`` file that you can uncomment:
630+
631+
.. code-block:: env
632+
633+
# .env
634+
LOCO_DSN=loco://API_KEY@default
635+
636+
The ``LOCO_DSN`` isn't a *real* address: it's a convenient format that
637+
offloads most of the configuration work to translation. The ``loco`` scheme
638+
activates the Loco provider that you just installed, which knows all about
639+
how to push and pull translations via Loco. The *only* part you need to change is the
640+
``API_KEY`` placeholder.
641+
642+
This table shows the full list of available DSN formats for each third party provider:
643+
644+
===================== =================================================================
645+
Provider DSN
646+
===================== =================================================================
647+
Crowdin crowdin://PROJECT_ID:API_TOKEN@ORGANIZATION_DOMAIN.default
648+
Loco (localise.biz) loco://API_KEY@default
649+
Lokalise lokalise://PROJECT_ID:API_KEY@default
650+
PoEditor poeditor://PROJECT_ID:API_KEY@default
651+
===================== =================================================================
652+
653+
To enable a translation provider, add the correct DSN in your ``.env`` file and
654+
configure the ``providers``:
655+
656+
.. configuration-block::
657+
658+
.. code-block:: yaml
659+
660+
# config/packages/translation.yaml
661+
framework:
662+
translator:
663+
providers:
664+
loco:
665+
dsn: '%env(LOCO_DSN)%'
666+
domains: ['messages']
667+
locales: ['en', 'fr']
668+
669+
.. code-block:: xml
670+
671+
<!-- config/packages/translation.xml -->
672+
<?xml version="1.0" encoding="UTF-8" ?>
673+
<container xmlns="http://symfony.com/schema/dic/services"
674+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
675+
xmlns:framework="http://symfony.com/schema/dic/symfony"
676+
xsi:schemaLocation="http://symfony.com/schema/dic/services
677+
https://symfony.com/schema/dic/services/services-1.0.xsd
678+
http://symfony.com/schema/dic/symfony
679+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
680+
681+
<framework:config>
682+
<framework:translator>
683+
<framework:provider name="loco" dsn="%env(LOCO_DSN)%">
684+
<framework:domain>messages</framework:domain>
685+
<!-- ... -->
686+
<framework:locale>en</framework:locale>
687+
<framework:locale>fr</framework:locale>
688+
<!-- ... -->
689+
</framework:provider>
690+
</framework:translator>
691+
</framework:config>
692+
</container>
693+
694+
.. code-block:: php
695+
696+
# config/packages/translation.php
697+
$container->loadFromExtension('framework', [
698+
'translator' => [
699+
'providers' => [
700+
'loco' => [
701+
'dsn' => '%env(LOCO_DSN)%',
702+
'domains' => ['messages'],
703+
'locales' => ['en', 'fr'],
704+
],
705+
],
706+
],
707+
]);
708+
709+
Push and pull translations
710+
~~~~~~~~~~~~~~~~~~~~~~~~~~
711+
712+
To push your existing translations to your configured third party provider, you have to use the `translation:push` command:
713+
714+
.. code-block:: terminal
715+
716+
# push all local translations to the Loco provider for the locales and domains configured in config/packages/translation.yaml file
717+
# it will update existing translations already on the provider.
718+
$ php bin/console translation:push loco --force
719+
720+
# push new local translations to the Loco provider for the French locale and the validators domain.
721+
# it will **not** update existing translations already on the provider.
722+
$ php bin/console translation:push loco --locales fr --domain validators
723+
724+
# push new local translations and delete provider's translations that not exists anymore in local files for the French locale and the validators domain.
725+
# it will **not** update existing translations already on the provider.
726+
$ php bin/console translation:push loco --delete-missing --locales fr --domain validators
727+
728+
# check out the command help to see its options (format, domains, locales, etc.)
729+
$ php bin/console translation:push --help
730+
731+
To pull translations from a provider in your local files, you have to use the `translation:pull` command:
732+
733+
.. code-block:: terminal
734+
735+
# pull all provider's translations to local files for the locales and domains configured in config/packages/translation.yaml file
736+
# it will overwrite completely your local files.
737+
$ php bin/console translation:pull loco --force
738+
739+
# pull new translations from the Loco provider to local files for the French locale and the validators domain.
740+
# it will **not** overwrite your local files, only add new translations.
741+
$ php bin/console translation:pull loco --locales fr --domain validators
742+
743+
# check out the command help to see its options (format, domains, locales, intl-icu, etc.)
744+
$ php bin/console translation:pull --help
745+
594746
Handling the User's Locale
595747
--------------------------
596748

0 commit comments

Comments
 (0)