diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php')
-rw-r--r-- | vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php b/vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php new file mode 100644 index 00000000..387c964d --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/MessageSelector.php | |||
@@ -0,0 +1,82 @@ | |||
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 | namespace Symfony\Component\Translation; | ||
13 | |||
14 | /** | ||
15 | * MessageSelector. | ||
16 | * | ||
17 | * @author Fabien Potencier <fabien@symfony.com> | ||
18 | * | ||
19 | * @api | ||
20 | */ | ||
21 | class MessageSelector | ||
22 | { | ||
23 | /** | ||
24 | * Given a message with different plural translations separated by a | ||
25 | * pipe (|), this method returns the correct portion of the message based | ||
26 | * on the given number, locale and the pluralization rules in the message | ||
27 | * itself. | ||
28 | * | ||
29 | * The message supports two different types of pluralization rules: | ||
30 | * | ||
31 | * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples | ||
32 | * indexed: There is one apple|There are %count% apples | ||
33 | * | ||
34 | * The indexed solution can also contain labels (e.g. one: There is one apple). | ||
35 | * This is purely for making the translations more clear - it does not | ||
36 | * affect the functionality. | ||
37 | * | ||
38 | * The two methods can also be mixed: | ||
39 | * {0} There are no apples|one: There is one apple|more: There are %count% apples | ||
40 | * | ||
41 | * @param string $message The message being translated | ||
42 | * @param integer $number The number of items represented for the message | ||
43 | * @param string $locale The locale to use for choosing | ||
44 | * | ||
45 | * @return string | ||
46 | * | ||
47 | * @throws \InvalidArgumentException | ||
48 | * | ||
49 | * @api | ||
50 | */ | ||
51 | public function choose($message, $number, $locale) | ||
52 | { | ||
53 | $parts = explode('|', $message); | ||
54 | $explicitRules = array(); | ||
55 | $standardRules = array(); | ||
56 | foreach ($parts as $part) { | ||
57 | $part = trim($part); | ||
58 | |||
59 | if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/x', $part, $matches)) { | ||
60 | $explicitRules[$matches['interval']] = $matches['message']; | ||
61 | } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) { | ||
62 | $standardRules[] = $matches[1]; | ||
63 | } else { | ||
64 | $standardRules[] = $part; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | // try to match an explicit rule, then fallback to the standard ones | ||
69 | foreach ($explicitRules as $interval => $m) { | ||
70 | if (Interval::test($number, $interval)) { | ||
71 | return $m; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | $position = PluralizationRules::get($number, $locale); | ||
76 | if (!isset($standardRules[$position])) { | ||
77 | throw new \InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale)); | ||
78 | } | ||
79 | |||
80 | return $standardRules[$position]; | ||
81 | } | ||
82 | } | ||