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

Commit62cb6a3

Browse files
committed
[Translation] Allow global parameters
1 parent7c53816 commit62cb6a3

14 files changed

+200
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
929929
->fixXmlConfig('fallback')
930930
->fixXmlConfig('path')
931931
->fixXmlConfig('provider')
932+
->fixXmlConfig('global')
932933
->children()
933934
->arrayNode('fallbacks')
934935
->info('Defaults to the value of "default_locale".')
@@ -983,6 +984,13 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
983984
->end()
984985
->defaultValue([])
985986
->end()
987+
->arrayNode('globals')
988+
->info('Global parameters.')
989+
->example(['pi' =>3.14])
990+
->normalizeKeys(false)
991+
->useAttributeAsKey('name')
992+
->prototype('scalar')->end()
993+
->end()
986994
->end()
987995
->end()
988996
->end()

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
useSymfony\Component\Translation\BridgeasTranslationBridge;
165165
useSymfony\Component\Translation\Command\XliffLintCommandasBaseXliffLintCommand;
166166
useSymfony\Component\Translation\Extractor\PhpAstExtractor;
167+
useSymfony\Component\Translation\GlobalsTranslator;
167168
useSymfony\Component\Translation\LocaleSwitcher;
168169
useSymfony\Component\Translation\PseudoLocalizationTranslator;
169170
useSymfony\Component\Translation\Translator;
@@ -1580,6 +1581,18 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
15801581
}
15811582
}
15821583

1584+
if (!empty($config['globals'])) {
1585+
$def =$container
1586+
->register('translator.globals', GlobalsTranslator::class)
1587+
->setDecoratedService('translator',null, -1)// Lower priority than "translator.data_collector"
1588+
->setArguments([
1589+
newReference('translator.globals.inner'),
1590+
]);
1591+
foreach ($config['globals']as$key =>$value) {
1592+
$def->addMethodCall('addGlobal', [$key,$value]);
1593+
}
1594+
}
1595+
15831596
if (!$config['providers']) {
15841597
return;
15851598
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,15 @@
230230
<xsd:elementname="path"type="xsd:string"minOccurs="0"maxOccurs="unbounded" />
231231
<xsd:elementname="pseudo-localization"type="pseudo_localization"minOccurs="0"maxOccurs="1" />
232232
<xsd:elementname="provider"type="translation_provider"minOccurs="0"maxOccurs="unbounded" />
233+
<xsd:elementname="global"type="translation_global"minOccurs="0"maxOccurs="unbounded" />
233234
</xsd:sequence>
234235
<xsd:attributename="enabled"type="xsd:boolean" />
235236
<xsd:attributename="fallback"type="xsd:string" />
236237
<xsd:attributename="logging"type="xsd:boolean" />
237238
<xsd:attributename="formatter"type="xsd:string" />
238239
<xsd:attributename="cache-dir"type="xsd:string" />
239240
<xsd:attributename="default-path"type="xsd:string" />
241+
<xsd:attributename="global"type="xsd:string" />
240242
</xsd:complexType>
241243

242244
<xsd:complexTypename="pseudo_localization">
@@ -259,6 +261,11 @@
259261
<xsd:attributename="dsn"type="xsd:string" />
260262
</xsd:complexType>
261263

264+
<xsd:complexTypename="translation_global"mixed="true">
265+
<xsd:attributename="name"type="xsd:string"use="required" />
266+
<xsd:attributename="value"type="xsd:string" />
267+
</xsd:complexType>
268+
262269
<xsd:complexTypename="validation">
263270
<xsd:choiceminOccurs="0"maxOccurs="unbounded">
264271
<xsd:elementname="static-method"type="xsd:string" />

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ protected static function getBundleDefaultConfig()
591591
'localizable_html_attributes' => [],
592592
],
593593
'providers' => [],
594+
'globals' => [],
594595
],
595596
'validation' => [
596597
'enabled' => !class_exists(FullStack::class),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'annotations' =>false,
5+
'http_method_override' =>false,
6+
'handle_all_throwables' =>true,
7+
'php_errors' => ['log' =>true],
8+
'translator' => [
9+
'globals' => [
10+
'%%app_name%%' =>'My application',
11+
'{app_version}' =>'1.2.3',
12+
],
13+
],
14+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'annotations' =>false,
5+
'http_method_override' =>false,
6+
'handle_all_throwables' =>true,
7+
'php_errors' => ['log' =>true],
8+
'translator' => [],
9+
]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<containerxmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:configsecret="s3cr3t"http-method-override="false"handle-all-throwables="true">
10+
<framework:annotationsenabled="false" />
11+
<framework:php-errorslog="true" />
12+
<framework:translatorenabled="true">
13+
<framework:globalname="%%app_name%%"value="My application" />
14+
<framework:globalname="{app_version}"value="1.2.3" />
15+
</framework:translator>
16+
</framework:config>
17+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<containerxmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:configsecret="s3cr3t"http-method-override="false"handle-all-throwables="true">
10+
<framework:annotationsenabled="false" />
11+
<framework:php-errorslog="true" />
12+
<framework:translatorenabled="true" />
13+
</framework:config>
14+
</container>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
framework:
2+
annotations:false
3+
http_method_override:false
4+
handle_all_throwables:true
5+
php_errors:
6+
log:true
7+
translator:
8+
globals:
9+
'%%app_name%%':'My application'
10+
'{app_version}':'1.2.3'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
framework:
2+
annotations:false
3+
http_method_override:false
4+
handle_all_throwables:true
5+
php_errors:
6+
log:true
7+
translator:
8+
globals:[]

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,22 @@ public function testTranslatorCacheDirDisabled()
11821182
$this->assertNull($options['cache_dir']);
11831183
}
11841184

1185+
publicfunctiontestTranslatorGlobals()
1186+
{
1187+
$container =$this->createContainerFromFile('translator_globals');
1188+
$calls =$container->getDefinition('translator.globals')->getMethodCalls();
1189+
$this->assertSame([
1190+
['addGlobal', ['%%app_name%%','My application']],
1191+
['addGlobal', ['{app_version}','1.2.3']],
1192+
],$calls);
1193+
}
1194+
1195+
publicfunctiontestTranslatorWithoutGlobals()
1196+
{
1197+
$container =$this->createContainerFromFile('translator_without_globals');
1198+
$this->assertFalse($container->hasDefinition('translator.globals'));
1199+
}
1200+
11851201
publicfunctiontestValidation()
11861202
{
11871203
$container =$this->createContainerFromFile('full');

‎src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Mark class`DataCollectorTranslator` as`final`
8+
* Add`GlobalsTranslator` to allow defining global parameters
89

910
7.0
1011
---
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Translation;
13+
14+
useSymfony\Contracts\Translation\TranslatorInterface;
15+
16+
finalclass GlobalsTranslatorimplements TranslatorInterface
17+
{
18+
privatearray$globals = [];
19+
20+
publicfunction__construct(
21+
privatereadonlyTranslatorInterface$translator,
22+
) {
23+
}
24+
25+
publicfunctiontrans(string$id,array$parameters = [],string$domain =null,string$locale =null):string
26+
{
27+
$parameters =array_replace($this->globals,$parameters);
28+
29+
return$this->translator->trans($id,$parameters,$domain,$locale);
30+
}
31+
32+
publicfunctiongetLocale():string
33+
{
34+
return$this->translator->getLocale();
35+
}
36+
37+
publicfunctionaddGlobal(string$name,mixed$value):void
38+
{
39+
$this->globals[$name] =$value;
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Translation\Tests;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Translation\GlobalsTranslator;
16+
useSymfony\Component\Translation\Loader\ArrayLoader;
17+
useSymfony\Component\Translation\Translator;
18+
19+
class GlobalTranslatorTestextends TestCase
20+
{
21+
publicfunctiontestTrans()
22+
{
23+
$translator =newTranslator('en');
24+
$translator->addLoader('array',newArrayLoader());
25+
$translator->addResource('array', ['test' =>'Welcome %name%!'],'en');
26+
27+
$globalTranslator =newGlobalsTranslator($translator);
28+
$globalTranslator->addGlobal('%name%','Global name');
29+
30+
$this->assertSame('Welcome Global name!',$globalTranslator->trans('test'));
31+
$this->assertSame('Welcome Name!',$globalTranslator->trans('test', ['%name%' =>'Name']));
32+
}
33+
34+
publicfunctiontestGetLocale()
35+
{
36+
$translator =newTranslator('en');
37+
$globalTranslator =newGlobalsTranslator($translator);
38+
39+
$this->assertSame('en',$globalTranslator->getLocale());
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp