Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
dd270386
Commit
dd270386
authored
Aug 04, 2015
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed translation override
parent
6d3cc41b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
1 deletion
+70
-1
doc/providers/translation.rst
doc/providers/translation.rst
+16
-0
src/Silex/Provider/TranslationServiceProvider.php
src/Silex/Provider/TranslationServiceProvider.php
+9
-0
src/Silex/Provider/ValidatorServiceProvider.php
src/Silex/Provider/ValidatorServiceProvider.php
+7
-1
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
+38
-0
No files found.
doc/providers/translation.rst
View file @
dd270386
...
@@ -93,6 +93,22 @@ The above example will result in following routes:
...
@@ -93,6 +93,22 @@ The above example will result in following routes:
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
Using Resources
---------------
When translations are stored in a file, you can load them as follows::
$app = new Application();
$app->register(new TranslationServiceProvider());
$app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should be a valid number.' => 'Cette valeur doit être un nombre.'), 'fr', 'validators'),
));
return $resources;
});
Traits
Traits
------
------
...
...
src/Silex/Provider/TranslationServiceProvider.php
View file @
dd270386
...
@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface
...
@@ -40,6 +40,11 @@ class TranslationServiceProvider implements ServiceProviderInterface
$translator
->
addLoader
(
'array'
,
new
ArrayLoader
());
$translator
->
addLoader
(
'array'
,
new
ArrayLoader
());
$translator
->
addLoader
(
'xliff'
,
new
XliffFileLoader
());
$translator
->
addLoader
(
'xliff'
,
new
XliffFileLoader
());
// Register default resources
foreach
(
$app
[
'translator.resources'
]
as
$resource
)
{
$translator
->
addResource
(
$resource
[
0
],
$resource
[
1
],
$resource
[
2
],
$resource
[
3
]);
}
foreach
(
$app
[
'translator.domains'
]
as
$domain
=>
$data
)
{
foreach
(
$app
[
'translator.domains'
]
as
$domain
=>
$data
)
{
foreach
(
$data
as
$locale
=>
$messages
)
{
foreach
(
$data
as
$locale
=>
$messages
)
{
$translator
->
addResource
(
'array'
,
$messages
,
$locale
,
$domain
);
$translator
->
addResource
(
'array'
,
$messages
,
$locale
,
$domain
);
...
@@ -49,6 +54,10 @@ class TranslationServiceProvider implements ServiceProviderInterface
...
@@ -49,6 +54,10 @@ class TranslationServiceProvider implements ServiceProviderInterface
return
$translator
;
return
$translator
;
});
});
$app
[
'translator.resources'
]
=
function
(
$app
)
{
return
array
();
};
$app
[
'translator.message_selector'
]
=
$app
->
share
(
function
()
{
$app
[
'translator.message_selector'
]
=
$app
->
share
(
function
()
{
return
new
MessageSelector
();
return
new
MessageSelector
();
});
});
...
...
src/Silex/Provider/ValidatorServiceProvider.php
View file @
dd270386
...
@@ -33,7 +33,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface
...
@@ -33,7 +33,13 @@ class ValidatorServiceProvider implements ServiceProviderInterface
$r
=
new
\ReflectionClass
(
'Symfony\Component\Validator\Validation'
);
$r
=
new
\ReflectionClass
(
'Symfony\Component\Validator\Validation'
);
$file
=
dirname
(
$r
->
getFilename
())
.
'/Resources/translations/validators.'
.
$app
[
'locale'
]
.
'.xlf'
;
$file
=
dirname
(
$r
->
getFilename
())
.
'/Resources/translations/validators.'
.
$app
[
'locale'
]
.
'.xlf'
;
if
(
file_exists
(
$file
))
{
if
(
file_exists
(
$file
))
{
$app
[
'translator'
]
->
addResource
(
'xliff'
,
$file
,
$app
[
'locale'
],
'validators'
);
$app
->
extend
(
'translator.resources'
,
function
(
$resources
,
$app
)
use
(
$file
)
{
$resources
=
array_merge
(
array
(
array
(
'xliff'
,
$file
,
$app
[
'locale'
],
'validators'
),
),
$resources
);
return
$resources
;
});
}
}
}
}
...
...
tests/Silex/Tests/Provider/ValidatorServiceProviderTest.php
View file @
dd270386
...
@@ -135,4 +135,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -135,4 +135,42 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
array
(
'email@sample.com'
,
true
,
0
,
0
),
array
(
'email@sample.com'
,
true
,
0
,
0
),
);
);
}
}
public
function
testAddResource
()
{
$app
=
new
Application
();
$app
[
'locale'
]
=
'fr'
;
$app
->
register
(
new
ValidatorServiceProvider
());
$app
->
register
(
new
TranslationServiceProvider
());
$app
[
'translator'
]
=
$app
->
share
(
$app
->
extend
(
'translator'
,
function
(
$translator
,
$app
)
{
$translator
->
addResource
(
'array'
,
array
(
'This value should not be blank.'
=>
'Pas vide'
),
'fr'
,
'validators'
);
return
$translator
;
}));
$app
[
'validator'
];
$this
->
assertEquals
(
'Pas vide'
,
$app
[
'translator'
]
->
trans
(
'This value should not be blank.'
,
array
(),
'validators'
,
'fr'
));
}
public
function
testAddResourceAlternate
()
{
$app
=
new
Application
();
$app
[
'locale'
]
=
'fr'
;
$app
->
register
(
new
ValidatorServiceProvider
());
$app
->
register
(
new
TranslationServiceProvider
());
$app
->
extend
(
'translator.resources'
,
function
(
$resources
,
$app
)
{
$resources
=
array_merge
(
$resources
,
array
(
array
(
'array'
,
array
(
'This value should not be blank.'
=>
'Pas vide'
),
'fr'
,
'validators'
),
));
return
$resources
;
});
$app
[
'validator'
];
$this
->
assertEquals
(
'Pas vide'
,
$app
[
'translator'
]
->
trans
(
'This value should not be blank.'
,
array
(),
'validators'
,
'fr'
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment