Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit92db8a3

Browse files
committed
minor#8733 Updated the translation/* articles to Symfony 4 (javiereguiluz)
This PR was squashed before being merged into the 4.0 branch (closes#8733).Discussion----------Updated the translation/* articles to Symfony 4Commits-------eb93259 Fixes after Ryan's remarksaec8b0e Tweaks8fe3cf0 Updated the translation/* articles to Symfony 4
2 parents6c54c78 +eb93259 commit92db8a3

File tree

8 files changed

+145
-94
lines changed

8 files changed

+145
-94
lines changed

‎_images/translation/debug_1.png‎

-22.2 KB
Binary file not shown.

‎_images/translation/debug_2.png‎

-22 KB
Binary file not shown.

‎_images/translation/debug_3.png‎

-21.9 KB
Binary file not shown.

‎_images/translation/debug_4.png‎

-21.7 KB
Binary file not shown.

‎translation.rst‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,36 @@ to learn even more. Overall, the process has several steps:
4848

4949
.. _translation-configuration:
5050

51+
Installation
52+
------------
53+
54+
First, run this command to install the translator before using it:
55+
56+
..code-block::terminal
57+
58+
$ composer require translator
59+
5160
Configuration
5261
-------------
5362

54-
Translations are handled by a ``translator`` service that uses the user's
55-
localeto lookupandreturn translated messages. Before using it, enable the
56-
``translator`` in your configuration:
63+
The previous command creates an initial config file where you can define the
64+
defaultlocaleof the appandthe:ref:`fallback locales<translation-fallback>`
65+
that will be used if Symfony can't find some translation:
5766

5867
..configuration-block::
5968

6069
..code-block::yaml
6170
62-
#app/config/config.yml
71+
# config/packages/translation.yaml
6372
framework:
64-
translator:{ fallbacks: [en] }
73+
default_locale:'en'
74+
translator:
75+
fallbacks:['en']
76+
# ...
6577
6678
..code-block::xml
6779
68-
<!--app/config/config.xml-->
80+
<!-- config/packages/translation.xml-->
6981
<?xml version="1.0" encoding="UTF-8" ?>
7082
<containerxmlns="http://symfony.com/schema/dic/services"
7183
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -75,23 +87,23 @@ locale to lookup and return translated messages. Before using it, enable the
7587
http://symfony.com/schema/dic/symfony
7688
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7789
78-
<framework:config>
90+
<framework:configdefault-locale="en">
7991
<framework:translator>
8092
<framework:fallback>en</framework:fallback>
93+
<!-- ...-->
8194
</framework:translator>
8295
</framework:config>
8396
</container>
8497
8598
..code-block::php
8699
87-
//app/config/config.php
100+
// config/packages/translation.php
88101
$container->loadFromExtension('framework', array(
102+
'default_locale' => 'en',
89103
'translator' => array('fallbacks' => array('en')),
104+
// ...
90105
));
91106
92-
See:ref:`translation-fallback` for details on the ``fallbacks`` key
93-
and what Symfony does when it doesn't find a translation.
94-
95107
The locale used in translations is the one stored on the request. This is
96108
typically set via a ``_locale`` attribute on your routes (see:ref:`translation-locale-url`).
97109

@@ -108,10 +120,11 @@ for example, that you're translating a simple message from inside a controller::
108120

109121
// ...
110122
use Symfony\Component\HttpFoundation\Response;
123+
use Symfony\Component\Translation\Translator;
111124

112-
public functionindexAction()
125+
public functionindex(Translator $translator)
113126
{
114-
$translated = $this->get('translator')->trans('Symfony is great');
127+
$translated = $translator->trans('Symfony is great');
115128

116129
return new Response($translated);
117130
}
@@ -129,7 +142,7 @@ different formats, XLIFF being the recommended format:
129142

130143
..code-block::xml
131144
132-
<!-- messages.fr.xlf-->
145+
<!--translations/messages.fr.xlf-->
133146
<?xml version="1.0"?>
134147
<xliffversion="1.2"xmlns="urn:oasis:names:tc:xliff:document:1.2">
135148
<filesource-language="en"datatype="plaintext"original="file.ext">
@@ -144,12 +157,12 @@ different formats, XLIFF being the recommended format:
144157
145158
..code-block::yaml
146159
147-
# messages.fr.yml
160+
#translations/messages.fr.yml
148161
Symfony is great:J'aime Symfony
149162
150163
..code-block::php
151164
152-
// messages.fr.php
165+
//translations/messages.fr.php
153166
return array(
154167
'Symfony is great' => 'J\'aime Symfony',
155168
);
@@ -186,10 +199,11 @@ Message Placeholders
186199
Sometimes, a message containing a variable needs to be translated::
187200

188201
use Symfony\Component\HttpFoundation\Response;
202+
use Symfony\Component\Translation\Translator;
189203

190-
public functionindexAction($name)
204+
public functionindex(Translator $translator,$name)
191205
{
192-
$translated = $this->get('translator')->trans('Hello '.$name);
206+
$translated = $translator->trans('Hello '.$name);
193207

194208
return new Response($translated);
195209
}
@@ -336,14 +350,14 @@ Translation Resource/File Names and Locations
336350

337351
Symfony looks for message files (i.e. translations) in the following default locations:
338352

339-
* the ``app/Resources/translations`` directory;
353+
* the ``translations/`` directory;
340354

341-
* the ``app/Resources/<bundle name>/translations`` directory;
355+
* the ``src/Resources/<bundle name>/translations/`` directory;
342356

343357
* the ``Resources/translations/`` directory inside of any bundle.
344358

345359
The locations are listed here with the highest priority first. That is, you can
346-
override the translation messages of a bundle in any of the top2 directories.
360+
override the translation messages of a bundle in any of the toptwo directories.
347361

348362
The override mechanism works at a key level: only the overridden keys need
349363
to be listed in a higher priority message file. When a key is not found
@@ -359,14 +373,14 @@ must be named according to the following path: ``domain.locale.loader``:
359373
* **locale**: The locale that the translations are for (e.g. ``en_GB``, ``en``, etc);
360374

361375
* **loader**: How Symfony should load and parse the file (e.g. ``xlf``,
362-
``php``, ``yml``, etc).
376+
``php``, ``yaml``, etc).
363377

364378
The loader can be the name of any registered loader. By default, Symfony
365379
provides many loaders, including:
366380

367381
* ``xlf``: XLIFF file;
368382
* ``php``: PHP file;
369-
* ``yml``: YAML file.
383+
* ``yaml``: YAML file.
370384

371385
The choice of which loader to use is entirely up to you and is a matter of
372386
taste. The recommended option is to use ``xlf`` for translations.
@@ -381,15 +395,15 @@ For more options, see :ref:`component-translator-message-catalogs`.
381395

382396
..code-block::yaml
383397
384-
#app/config/config.yml
398+
# config/packages/translation.yaml
385399
framework:
386400
translator:
387401
paths:
388-
-'%kernel.project_dir%/translations'
402+
-'%kernel.project_dir%/custom/path/to/translations'
389403
390404
..code-block::xml
391405
392-
<!--app/config/config.xml-->
406+
<!-- config/packages/translation.xml-->
393407
<?xml version="1.0" encoding="UTF-8" ?>
394408
<containerxmlns="http://symfony.com/schema/dic/services"
395409
xmlns:framework="http://symfony.com/schema/dic/symfony"
@@ -402,18 +416,18 @@ For more options, see :ref:`component-translator-message-catalogs`.
402416
403417
<framework:config>
404418
<framework:translator>
405-
<framework:path>%kernel.project_dir%/translations</framework:path>
419+
<framework:path>%kernel.project_dir%/custom/path/to/translations</framework:path>
406420
</framework:translator>
407421
</framework:config>
408422
</container>
409423
410424
..code-block::php
411425
412-
//app/config/config.php
426+
// config/packages/translation.php
413427
$container->loadFromExtension('framework', array(
414428
'translator' => array(
415429
'paths' => array(
416-
'%kernel.project_dir%/translations',
430+
'%kernel.project_dir%/custom/path/to/translations',
417431
),
418432
),
419433
));
@@ -455,7 +469,7 @@ checks translation resources for several locales:
455469

456470
..note::
457471

458-
When Symfonydoesn't find a translation in the given locale, it will
472+
When Symfonycan't find a translation in the given locale, it will
459473
add the missing translation to the log file. For details,
460474
see:ref:`reference-framework-translator-logging`.
461475

@@ -504,9 +518,10 @@ Learn more
504518

505519
..toctree::
506520
:maxdepth:1
507-
:glob:
508521

509-
/translation/*
522+
translation/locale
523+
translation/debug
524+
translation/lint
510525

511526
.. _`i18n`:https://en.wikipedia.org/wiki/Internationalization_and_localization
512527
.. _`ISO 3166-1 alpha-2`:https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp