@@ -15,403 +15,403 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Language |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The language ID. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - public $id; |
|
| 18 | + /** |
|
| 19 | + * The language ID. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + public $id; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The language name. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - public $name; |
|
| 25 | + /** |
|
| 26 | + * The language name. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + public $name; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * If this language is deprecated: the gettext code of the new language. |
|
| 34 | - * |
|
| 35 | - * @var string|null |
|
| 36 | - */ |
|
| 37 | - public $supersededBy; |
|
| 32 | + /** |
|
| 33 | + * If this language is deprecated: the gettext code of the new language. |
|
| 34 | + * |
|
| 35 | + * @var string|null |
|
| 36 | + */ |
|
| 37 | + public $supersededBy; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The script name. |
|
| 41 | - * |
|
| 42 | - * @var string|null |
|
| 43 | - */ |
|
| 44 | - public $script; |
|
| 39 | + /** |
|
| 40 | + * The script name. |
|
| 41 | + * |
|
| 42 | + * @var string|null |
|
| 43 | + */ |
|
| 44 | + public $script; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The territory name. |
|
| 48 | - * |
|
| 49 | - * @var string|null |
|
| 50 | - */ |
|
| 51 | - public $territory; |
|
| 46 | + /** |
|
| 47 | + * The territory name. |
|
| 48 | + * |
|
| 49 | + * @var string|null |
|
| 50 | + */ |
|
| 51 | + public $territory; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The name of the base language. |
|
| 55 | - * |
|
| 56 | - * @var string|null |
|
| 57 | - */ |
|
| 58 | - public $baseLanguage; |
|
| 53 | + /** |
|
| 54 | + * The name of the base language. |
|
| 55 | + * |
|
| 56 | + * @var string|null |
|
| 57 | + */ |
|
| 58 | + public $baseLanguage; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * The list of categories. |
|
| 62 | - * |
|
| 63 | - * @var \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Category[] |
|
| 64 | - */ |
|
| 65 | - public $categories; |
|
| 60 | + /** |
|
| 61 | + * The list of categories. |
|
| 62 | + * |
|
| 63 | + * @var \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Category[] |
|
| 64 | + */ |
|
| 65 | + public $categories; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * The gettext formula to decide which category should be applied. |
|
| 69 | - * |
|
| 70 | - * @var string |
|
| 71 | - */ |
|
| 72 | - public $formula; |
|
| 67 | + /** |
|
| 68 | + * The gettext formula to decide which category should be applied. |
|
| 69 | + * |
|
| 70 | + * @var string |
|
| 71 | + */ |
|
| 72 | + public $formula; |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Initialize the instance and parse the language code. |
|
| 76 | - * |
|
| 77 | - * @param array $info The result of CldrData::getLanguageInfo() |
|
| 78 | - * |
|
| 79 | - * @throws \Exception throws an Exception if $fullId is not valid |
|
| 80 | - */ |
|
| 81 | - private function __construct($info) |
|
| 82 | - { |
|
| 83 | - $this->id = $info['id']; |
|
| 84 | - $this->name = $info['name']; |
|
| 85 | - $this->supersededBy = isset($info['supersededBy']) ? $info['supersededBy'] : null; |
|
| 86 | - $this->script = isset($info['script']) ? $info['script'] : null; |
|
| 87 | - $this->territory = isset($info['territory']) ? $info['territory'] : null; |
|
| 88 | - $this->baseLanguage = isset($info['baseLanguage']) ? $info['baseLanguage'] : null; |
|
| 89 | - // Let's build the category list |
|
| 90 | - $this->categories = array(); |
|
| 91 | - foreach ($info['categories'] as $cldrCategoryId => $cldrFormulaAndExamples) { |
|
| 92 | - $category = new Category($cldrCategoryId, $cldrFormulaAndExamples); |
|
| 93 | - foreach ($this->categories as $c) { |
|
| 94 | - if ($category->id === $c->id) { |
|
| 95 | - throw new Exception("The category '{$category->id}' is specified more than once"); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - $this->categories[] = $category; |
|
| 99 | - } |
|
| 100 | - if (empty($this->categories)) { |
|
| 101 | - throw new Exception("The language '{$info['id']}' does not have any plural category"); |
|
| 102 | - } |
|
| 103 | - // Let's sort the categories from 'zero' to 'other' |
|
| 104 | - usort($this->categories, function (Category $category1, Category $category2) { |
|
| 105 | - return array_search($category1->id, CldrData::$categories) - array_search($category2->id, CldrData::$categories); |
|
| 106 | - }); |
|
| 107 | - // The 'other' category should always be there |
|
| 108 | - if ($this->categories[count($this->categories) - 1]->id !== CldrData::OTHER_CATEGORY) { |
|
| 109 | - throw new Exception("The language '{$info['id']}' does not have the '" . CldrData::OTHER_CATEGORY . "' plural category"); |
|
| 110 | - } |
|
| 111 | - $this->checkAlwaysTrueCategories(); |
|
| 112 | - $this->checkAlwaysFalseCategories(); |
|
| 113 | - $this->checkAllCategoriesWithExamples(); |
|
| 114 | - $this->formula = $this->buildFormula(); |
|
| 115 | - } |
|
| 74 | + /** |
|
| 75 | + * Initialize the instance and parse the language code. |
|
| 76 | + * |
|
| 77 | + * @param array $info The result of CldrData::getLanguageInfo() |
|
| 78 | + * |
|
| 79 | + * @throws \Exception throws an Exception if $fullId is not valid |
|
| 80 | + */ |
|
| 81 | + private function __construct($info) |
|
| 82 | + { |
|
| 83 | + $this->id = $info['id']; |
|
| 84 | + $this->name = $info['name']; |
|
| 85 | + $this->supersededBy = isset($info['supersededBy']) ? $info['supersededBy'] : null; |
|
| 86 | + $this->script = isset($info['script']) ? $info['script'] : null; |
|
| 87 | + $this->territory = isset($info['territory']) ? $info['territory'] : null; |
|
| 88 | + $this->baseLanguage = isset($info['baseLanguage']) ? $info['baseLanguage'] : null; |
|
| 89 | + // Let's build the category list |
|
| 90 | + $this->categories = array(); |
|
| 91 | + foreach ($info['categories'] as $cldrCategoryId => $cldrFormulaAndExamples) { |
|
| 92 | + $category = new Category($cldrCategoryId, $cldrFormulaAndExamples); |
|
| 93 | + foreach ($this->categories as $c) { |
|
| 94 | + if ($category->id === $c->id) { |
|
| 95 | + throw new Exception("The category '{$category->id}' is specified more than once"); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + $this->categories[] = $category; |
|
| 99 | + } |
|
| 100 | + if (empty($this->categories)) { |
|
| 101 | + throw new Exception("The language '{$info['id']}' does not have any plural category"); |
|
| 102 | + } |
|
| 103 | + // Let's sort the categories from 'zero' to 'other' |
|
| 104 | + usort($this->categories, function (Category $category1, Category $category2) { |
|
| 105 | + return array_search($category1->id, CldrData::$categories) - array_search($category2->id, CldrData::$categories); |
|
| 106 | + }); |
|
| 107 | + // The 'other' category should always be there |
|
| 108 | + if ($this->categories[count($this->categories) - 1]->id !== CldrData::OTHER_CATEGORY) { |
|
| 109 | + throw new Exception("The language '{$info['id']}' does not have the '" . CldrData::OTHER_CATEGORY . "' plural category"); |
|
| 110 | + } |
|
| 111 | + $this->checkAlwaysTrueCategories(); |
|
| 112 | + $this->checkAlwaysFalseCategories(); |
|
| 113 | + $this->checkAllCategoriesWithExamples(); |
|
| 114 | + $this->formula = $this->buildFormula(); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Return a list of all languages available. |
|
| 119 | - * |
|
| 120 | - * @throws \Exception |
|
| 121 | - * |
|
| 122 | - * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language[] |
|
| 123 | - */ |
|
| 124 | - public static function getAll() |
|
| 125 | - { |
|
| 126 | - $result = array(); |
|
| 127 | - foreach (array_keys(CldrData::getLanguageNames()) as $cldrLanguageId) { |
|
| 128 | - $result[] = new self(CldrData::getLanguageInfo($cldrLanguageId)); |
|
| 129 | - } |
|
| 117 | + /** |
|
| 118 | + * Return a list of all languages available. |
|
| 119 | + * |
|
| 120 | + * @throws \Exception |
|
| 121 | + * |
|
| 122 | + * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language[] |
|
| 123 | + */ |
|
| 124 | + public static function getAll() |
|
| 125 | + { |
|
| 126 | + $result = array(); |
|
| 127 | + foreach (array_keys(CldrData::getLanguageNames()) as $cldrLanguageId) { |
|
| 128 | + $result[] = new self(CldrData::getLanguageInfo($cldrLanguageId)); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - return $result; |
|
| 132 | - } |
|
| 131 | + return $result; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Return a Language instance given the language id. |
|
| 136 | - * |
|
| 137 | - * @param string $id |
|
| 138 | - * |
|
| 139 | - * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language|null |
|
| 140 | - */ |
|
| 141 | - public static function getById($id) |
|
| 142 | - { |
|
| 143 | - $result = null; |
|
| 144 | - $info = CldrData::getLanguageInfo($id); |
|
| 145 | - if (isset($info)) { |
|
| 146 | - $result = new self($info); |
|
| 147 | - } |
|
| 134 | + /** |
|
| 135 | + * Return a Language instance given the language id. |
|
| 136 | + * |
|
| 137 | + * @param string $id |
|
| 138 | + * |
|
| 139 | + * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language|null |
|
| 140 | + */ |
|
| 141 | + public static function getById($id) |
|
| 142 | + { |
|
| 143 | + $result = null; |
|
| 144 | + $info = CldrData::getLanguageInfo($id); |
|
| 145 | + if (isset($info)) { |
|
| 146 | + $result = new self($info); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - return $result; |
|
| 150 | - } |
|
| 149 | + return $result; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Returns a clone of this instance with all the strings to US-ASCII. |
|
| 154 | - * |
|
| 155 | - * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language |
|
| 156 | - */ |
|
| 157 | - public function getUSAsciiClone() |
|
| 158 | - { |
|
| 159 | - $clone = clone $this; |
|
| 160 | - self::asciifier($clone->name); |
|
| 161 | - self::asciifier($clone->formula); |
|
| 162 | - $clone->categories = array(); |
|
| 163 | - foreach ($this->categories as $category) { |
|
| 164 | - $categoryClone = clone $category; |
|
| 165 | - self::asciifier($categoryClone->examples); |
|
| 166 | - $clone->categories[] = $categoryClone; |
|
| 167 | - } |
|
| 152 | + /** |
|
| 153 | + * Returns a clone of this instance with all the strings to US-ASCII. |
|
| 154 | + * |
|
| 155 | + * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language |
|
| 156 | + */ |
|
| 157 | + public function getUSAsciiClone() |
|
| 158 | + { |
|
| 159 | + $clone = clone $this; |
|
| 160 | + self::asciifier($clone->name); |
|
| 161 | + self::asciifier($clone->formula); |
|
| 162 | + $clone->categories = array(); |
|
| 163 | + foreach ($this->categories as $category) { |
|
| 164 | + $categoryClone = clone $category; |
|
| 165 | + self::asciifier($categoryClone->examples); |
|
| 166 | + $clone->categories[] = $categoryClone; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - return $clone; |
|
| 170 | - } |
|
| 169 | + return $clone; |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Build the formula starting from the currently defined categories. |
|
| 174 | - * |
|
| 175 | - * @param bool $withoutParenthesis TRUE to build a formula in standard gettext format, FALSE (default) to build a PHP-compatible formula |
|
| 176 | - * |
|
| 177 | - * @return string |
|
| 178 | - */ |
|
| 179 | - public function buildFormula($withoutParenthesis = false) |
|
| 180 | - { |
|
| 181 | - $numCategories = count($this->categories); |
|
| 182 | - switch ($numCategories) { |
|
| 183 | - case 1: |
|
| 184 | - // Just one category |
|
| 185 | - return '0'; |
|
| 186 | - case 2: |
|
| 187 | - return self::reduceFormula(self::reverseFormula($this->categories[0]->formula)); |
|
| 188 | - default: |
|
| 189 | - $formula = (string) ($numCategories - 1); |
|
| 190 | - for ($i = $numCategories - 2; $i >= 0; $i--) { |
|
| 191 | - $f = self::reduceFormula($this->categories[$i]->formula); |
|
| 192 | - if (!$withoutParenthesis && !preg_match('/^\([^()]+\)$/', $f)) { |
|
| 193 | - $f = "({$f})"; |
|
| 194 | - } |
|
| 195 | - $formula = "{$f} ? {$i} : {$formula}"; |
|
| 196 | - if (!$withoutParenthesis && $i > 0) { |
|
| 197 | - $formula = "({$formula})"; |
|
| 198 | - } |
|
| 199 | - } |
|
| 172 | + /** |
|
| 173 | + * Build the formula starting from the currently defined categories. |
|
| 174 | + * |
|
| 175 | + * @param bool $withoutParenthesis TRUE to build a formula in standard gettext format, FALSE (default) to build a PHP-compatible formula |
|
| 176 | + * |
|
| 177 | + * @return string |
|
| 178 | + */ |
|
| 179 | + public function buildFormula($withoutParenthesis = false) |
|
| 180 | + { |
|
| 181 | + $numCategories = count($this->categories); |
|
| 182 | + switch ($numCategories) { |
|
| 183 | + case 1: |
|
| 184 | + // Just one category |
|
| 185 | + return '0'; |
|
| 186 | + case 2: |
|
| 187 | + return self::reduceFormula(self::reverseFormula($this->categories[0]->formula)); |
|
| 188 | + default: |
|
| 189 | + $formula = (string) ($numCategories - 1); |
|
| 190 | + for ($i = $numCategories - 2; $i >= 0; $i--) { |
|
| 191 | + $f = self::reduceFormula($this->categories[$i]->formula); |
|
| 192 | + if (!$withoutParenthesis && !preg_match('/^\([^()]+\)$/', $f)) { |
|
| 193 | + $f = "({$f})"; |
|
| 194 | + } |
|
| 195 | + $formula = "{$f} ? {$i} : {$formula}"; |
|
| 196 | + if (!$withoutParenthesis && $i > 0) { |
|
| 197 | + $formula = "({$formula})"; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - return $formula; |
|
| 202 | - } |
|
| 203 | - } |
|
| 201 | + return $formula; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * Let's look for categories that will always occur. |
|
| 207 | - * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have just one case. |
|
| 208 | - * If we found that (single) category we reduce the categories to that one only. |
|
| 209 | - * |
|
| 210 | - * @throws \Exception |
|
| 211 | - */ |
|
| 212 | - private function checkAlwaysTrueCategories() |
|
| 213 | - { |
|
| 214 | - $alwaysTrueCategory = null; |
|
| 215 | - foreach ($this->categories as $category) { |
|
| 216 | - if ($category->formula === true) { |
|
| 217 | - if (!isset($category->examples)) { |
|
| 218 | - throw new Exception("The category '{$category->id}' should always occur, but it does not have examples (so for CLDR it will never occur for integers!)"); |
|
| 219 | - } |
|
| 220 | - $alwaysTrueCategory = $category; |
|
| 221 | - break; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - if (isset($alwaysTrueCategory)) { |
|
| 225 | - foreach ($this->categories as $category) { |
|
| 226 | - if (($category !== $alwaysTrueCategory) && isset($category->examples)) { |
|
| 227 | - throw new Exception("The category '{$category->id}' should never occur, but it has some examples (so for CLDR it will occur!)"); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - $alwaysTrueCategory->id = CldrData::OTHER_CATEGORY; |
|
| 231 | - $alwaysTrueCategory->formula = null; |
|
| 232 | - $this->categories = array($alwaysTrueCategory); |
|
| 233 | - } |
|
| 234 | - } |
|
| 205 | + /** |
|
| 206 | + * Let's look for categories that will always occur. |
|
| 207 | + * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have just one case. |
|
| 208 | + * If we found that (single) category we reduce the categories to that one only. |
|
| 209 | + * |
|
| 210 | + * @throws \Exception |
|
| 211 | + */ |
|
| 212 | + private function checkAlwaysTrueCategories() |
|
| 213 | + { |
|
| 214 | + $alwaysTrueCategory = null; |
|
| 215 | + foreach ($this->categories as $category) { |
|
| 216 | + if ($category->formula === true) { |
|
| 217 | + if (!isset($category->examples)) { |
|
| 218 | + throw new Exception("The category '{$category->id}' should always occur, but it does not have examples (so for CLDR it will never occur for integers!)"); |
|
| 219 | + } |
|
| 220 | + $alwaysTrueCategory = $category; |
|
| 221 | + break; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + if (isset($alwaysTrueCategory)) { |
|
| 225 | + foreach ($this->categories as $category) { |
|
| 226 | + if (($category !== $alwaysTrueCategory) && isset($category->examples)) { |
|
| 227 | + throw new Exception("The category '{$category->id}' should never occur, but it has some examples (so for CLDR it will occur!)"); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + $alwaysTrueCategory->id = CldrData::OTHER_CATEGORY; |
|
| 231 | + $alwaysTrueCategory->formula = null; |
|
| 232 | + $this->categories = array($alwaysTrueCategory); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Let's look for categories that will never occur. |
|
| 238 | - * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have some less cases. |
|
| 239 | - * If we found those categories we strip them out. |
|
| 240 | - * |
|
| 241 | - * @throws \Exception |
|
| 242 | - */ |
|
| 243 | - private function checkAlwaysFalseCategories() |
|
| 244 | - { |
|
| 245 | - $filtered = array(); |
|
| 246 | - foreach ($this->categories as $category) { |
|
| 247 | - if ($category->formula === false) { |
|
| 248 | - if (isset($category->examples)) { |
|
| 249 | - throw new Exception("The category '{$category->id}' should never occur, but it has examples (so for CLDR it may occur!)"); |
|
| 250 | - } |
|
| 251 | - } else { |
|
| 252 | - $filtered[] = $category; |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - $this->categories = $filtered; |
|
| 256 | - } |
|
| 236 | + /** |
|
| 237 | + * Let's look for categories that will never occur. |
|
| 238 | + * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have some less cases. |
|
| 239 | + * If we found those categories we strip them out. |
|
| 240 | + * |
|
| 241 | + * @throws \Exception |
|
| 242 | + */ |
|
| 243 | + private function checkAlwaysFalseCategories() |
|
| 244 | + { |
|
| 245 | + $filtered = array(); |
|
| 246 | + foreach ($this->categories as $category) { |
|
| 247 | + if ($category->formula === false) { |
|
| 248 | + if (isset($category->examples)) { |
|
| 249 | + throw new Exception("The category '{$category->id}' should never occur, but it has examples (so for CLDR it may occur!)"); |
|
| 250 | + } |
|
| 251 | + } else { |
|
| 252 | + $filtered[] = $category; |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + $this->categories = $filtered; |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * Let's look for categories that don't have examples. |
|
| 260 | - * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have some less cases. |
|
| 261 | - * If we found those categories, we check that they never occur and we strip them out. |
|
| 262 | - * |
|
| 263 | - * @throws \Exception |
|
| 264 | - */ |
|
| 265 | - private function checkAllCategoriesWithExamples() |
|
| 266 | - { |
|
| 267 | - $allCategoriesIds = array(); |
|
| 268 | - $goodCategories = array(); |
|
| 269 | - $badCategories = array(); |
|
| 270 | - $badCategoriesIds = array(); |
|
| 271 | - foreach ($this->categories as $category) { |
|
| 272 | - $allCategoriesIds[] = $category->id; |
|
| 273 | - if (isset($category->examples)) { |
|
| 274 | - $goodCategories[] = $category; |
|
| 275 | - } else { |
|
| 276 | - $badCategories[] = $category; |
|
| 277 | - $badCategoriesIds[] = $category->id; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - if (empty($badCategories)) { |
|
| 281 | - return; |
|
| 282 | - } |
|
| 283 | - $removeCategoriesWithoutExamples = false; |
|
| 284 | - switch (implode(',', $badCategoriesIds) . '@' . implode(',', $allCategoriesIds)) { |
|
| 285 | - case CldrData::OTHER_CATEGORY . '@one,few,many,' . CldrData::OTHER_CATEGORY: |
|
| 286 | - switch ($this->buildFormula()) { |
|
| 287 | - case '(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : ((n % 10 == 0 || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 11 && n % 100 <= 14) ? 2 : 3))': |
|
| 288 | - // Numbers ending with 0 => case 2 ('many') |
|
| 289 | - // Numbers ending with 1 but not with 11 => case 0 ('one') |
|
| 290 | - // Numbers ending with 11 => case 2 ('many') |
|
| 291 | - // Numbers ending with 2 but not with 12 => case 1 ('few') |
|
| 292 | - // Numbers ending with 12 => case 2 ('many') |
|
| 293 | - // Numbers ending with 3 but not with 13 => case 1 ('few') |
|
| 294 | - // Numbers ending with 13 => case 2 ('many') |
|
| 295 | - // Numbers ending with 4 but not with 14 => case 1 ('few') |
|
| 296 | - // Numbers ending with 14 => case 2 ('many') |
|
| 297 | - // Numbers ending with 5 => case 2 ('many') |
|
| 298 | - // Numbers ending with 6 => case 2 ('many') |
|
| 299 | - // Numbers ending with 7 => case 2 ('many') |
|
| 300 | - // Numbers ending with 8 => case 2 ('many') |
|
| 301 | - // Numbers ending with 9 => case 2 ('many') |
|
| 302 | - // => the 'other' case never occurs: use 'other' for 'many' |
|
| 303 | - $removeCategoriesWithoutExamples = true; |
|
| 304 | - break; |
|
| 305 | - case '(n == 1) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : ((n != 1 && (n % 10 == 0 || n % 10 == 1) || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 12 && n % 100 <= 14) ? 2 : 3))': |
|
| 306 | - // Numbers ending with 0 => case 2 ('many') |
|
| 307 | - // Numbers ending with 1 but not number 1 => case 2 ('many') |
|
| 308 | - // Number 1 => case 0 ('one') |
|
| 309 | - // Numbers ending with 2 but not with 12 => case 1 ('few') |
|
| 310 | - // Numbers ending with 12 => case 2 ('many') |
|
| 311 | - // Numbers ending with 3 but not with 13 => case 1 ('few') |
|
| 312 | - // Numbers ending with 13 => case 2 ('many') |
|
| 313 | - // Numbers ending with 4 but not with 14 => case 1 ('few') |
|
| 314 | - // Numbers ending with 14 => case 2 ('many') |
|
| 315 | - // Numbers ending with 5 => case 2 ('many') |
|
| 316 | - // Numbers ending with 6 => case 2 ('many') |
|
| 317 | - // Numbers ending with 7 => case 2 ('many') |
|
| 318 | - // Numbers ending with 8 => case 2 ('many') |
|
| 319 | - // Numbers ending with 9 => case 2 ('many') |
|
| 320 | - // => the 'other' case never occurs: use 'other' for 'many' |
|
| 321 | - $removeCategoriesWithoutExamples = true; |
|
| 322 | - break; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - if (!$removeCategoriesWithoutExamples) { |
|
| 326 | - throw new Exception("Unhandled case of plural categories without examples '" . implode(', ', $badCategoriesIds) . "' out of '" . implode(', ', $allCategoriesIds) . "'"); |
|
| 327 | - } |
|
| 328 | - if ($badCategories[count($badCategories) - 1]->id === CldrData::OTHER_CATEGORY) { |
|
| 329 | - // We're removing the 'other' cagory: let's change the last good category to 'other' |
|
| 330 | - $lastGood = $goodCategories[count($goodCategories) - 1]; |
|
| 331 | - $lastGood->id = CldrData::OTHER_CATEGORY; |
|
| 332 | - $lastGood->formula = null; |
|
| 333 | - } |
|
| 334 | - $this->categories = $goodCategories; |
|
| 335 | - } |
|
| 258 | + /** |
|
| 259 | + * Let's look for categories that don't have examples. |
|
| 260 | + * This because with decimals (CLDR) we may have more cases, with integers (gettext) we have some less cases. |
|
| 261 | + * If we found those categories, we check that they never occur and we strip them out. |
|
| 262 | + * |
|
| 263 | + * @throws \Exception |
|
| 264 | + */ |
|
| 265 | + private function checkAllCategoriesWithExamples() |
|
| 266 | + { |
|
| 267 | + $allCategoriesIds = array(); |
|
| 268 | + $goodCategories = array(); |
|
| 269 | + $badCategories = array(); |
|
| 270 | + $badCategoriesIds = array(); |
|
| 271 | + foreach ($this->categories as $category) { |
|
| 272 | + $allCategoriesIds[] = $category->id; |
|
| 273 | + if (isset($category->examples)) { |
|
| 274 | + $goodCategories[] = $category; |
|
| 275 | + } else { |
|
| 276 | + $badCategories[] = $category; |
|
| 277 | + $badCategoriesIds[] = $category->id; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + if (empty($badCategories)) { |
|
| 281 | + return; |
|
| 282 | + } |
|
| 283 | + $removeCategoriesWithoutExamples = false; |
|
| 284 | + switch (implode(',', $badCategoriesIds) . '@' . implode(',', $allCategoriesIds)) { |
|
| 285 | + case CldrData::OTHER_CATEGORY . '@one,few,many,' . CldrData::OTHER_CATEGORY: |
|
| 286 | + switch ($this->buildFormula()) { |
|
| 287 | + case '(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : ((n % 10 == 0 || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 11 && n % 100 <= 14) ? 2 : 3))': |
|
| 288 | + // Numbers ending with 0 => case 2 ('many') |
|
| 289 | + // Numbers ending with 1 but not with 11 => case 0 ('one') |
|
| 290 | + // Numbers ending with 11 => case 2 ('many') |
|
| 291 | + // Numbers ending with 2 but not with 12 => case 1 ('few') |
|
| 292 | + // Numbers ending with 12 => case 2 ('many') |
|
| 293 | + // Numbers ending with 3 but not with 13 => case 1 ('few') |
|
| 294 | + // Numbers ending with 13 => case 2 ('many') |
|
| 295 | + // Numbers ending with 4 but not with 14 => case 1 ('few') |
|
| 296 | + // Numbers ending with 14 => case 2 ('many') |
|
| 297 | + // Numbers ending with 5 => case 2 ('many') |
|
| 298 | + // Numbers ending with 6 => case 2 ('many') |
|
| 299 | + // Numbers ending with 7 => case 2 ('many') |
|
| 300 | + // Numbers ending with 8 => case 2 ('many') |
|
| 301 | + // Numbers ending with 9 => case 2 ('many') |
|
| 302 | + // => the 'other' case never occurs: use 'other' for 'many' |
|
| 303 | + $removeCategoriesWithoutExamples = true; |
|
| 304 | + break; |
|
| 305 | + case '(n == 1) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : ((n != 1 && (n % 10 == 0 || n % 10 == 1) || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 12 && n % 100 <= 14) ? 2 : 3))': |
|
| 306 | + // Numbers ending with 0 => case 2 ('many') |
|
| 307 | + // Numbers ending with 1 but not number 1 => case 2 ('many') |
|
| 308 | + // Number 1 => case 0 ('one') |
|
| 309 | + // Numbers ending with 2 but not with 12 => case 1 ('few') |
|
| 310 | + // Numbers ending with 12 => case 2 ('many') |
|
| 311 | + // Numbers ending with 3 but not with 13 => case 1 ('few') |
|
| 312 | + // Numbers ending with 13 => case 2 ('many') |
|
| 313 | + // Numbers ending with 4 but not with 14 => case 1 ('few') |
|
| 314 | + // Numbers ending with 14 => case 2 ('many') |
|
| 315 | + // Numbers ending with 5 => case 2 ('many') |
|
| 316 | + // Numbers ending with 6 => case 2 ('many') |
|
| 317 | + // Numbers ending with 7 => case 2 ('many') |
|
| 318 | + // Numbers ending with 8 => case 2 ('many') |
|
| 319 | + // Numbers ending with 9 => case 2 ('many') |
|
| 320 | + // => the 'other' case never occurs: use 'other' for 'many' |
|
| 321 | + $removeCategoriesWithoutExamples = true; |
|
| 322 | + break; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + if (!$removeCategoriesWithoutExamples) { |
|
| 326 | + throw new Exception("Unhandled case of plural categories without examples '" . implode(', ', $badCategoriesIds) . "' out of '" . implode(', ', $allCategoriesIds) . "'"); |
|
| 327 | + } |
|
| 328 | + if ($badCategories[count($badCategories) - 1]->id === CldrData::OTHER_CATEGORY) { |
|
| 329 | + // We're removing the 'other' cagory: let's change the last good category to 'other' |
|
| 330 | + $lastGood = $goodCategories[count($goodCategories) - 1]; |
|
| 331 | + $lastGood->id = CldrData::OTHER_CATEGORY; |
|
| 332 | + $lastGood->formula = null; |
|
| 333 | + } |
|
| 334 | + $this->categories = $goodCategories; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Reverse a formula. |
|
| 339 | - * |
|
| 340 | - * @param string $formula |
|
| 341 | - * |
|
| 342 | - * @throws \Exception |
|
| 343 | - * |
|
| 344 | - * @return string |
|
| 345 | - */ |
|
| 346 | - private static function reverseFormula($formula) |
|
| 347 | - { |
|
| 348 | - if (preg_match('/^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 349 | - return str_replace(' == ', ' != ', $formula); |
|
| 350 | - } |
|
| 351 | - if (preg_match('/^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 352 | - return str_replace(' != ', ' == ', $formula); |
|
| 353 | - } |
|
| 354 | - if (preg_match('/^\(?n == \d+ \|\| n == \d+\)?$/', $formula)) { |
|
| 355 | - return trim(str_replace(array(' == ', ' || '), array(' != ', ' && '), $formula), '()'); |
|
| 356 | - } |
|
| 357 | - $m = null; |
|
| 358 | - if (preg_match('/^(n(?: % \d+)?) == (\d+) && (n(?: % \d+)?) != (\d+)$/', $formula, $m)) { |
|
| 359 | - return "{$m[1]} != {$m[2]} || {$m[3]} == {$m[4]}"; |
|
| 360 | - } |
|
| 361 | - switch ($formula) { |
|
| 362 | - case '(n == 1 || n == 2 || n == 3) || n % 10 != 4 && n % 10 != 6 && n % 10 != 9': |
|
| 363 | - return 'n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)'; |
|
| 364 | - case '(n == 0 || n == 1) || n >= 11 && n <= 99': |
|
| 365 | - return 'n >= 2 && (n < 11 || n > 99)'; |
|
| 366 | - } |
|
| 367 | - throw new Exception("Unable to reverse the formula '{$formula}'"); |
|
| 368 | - } |
|
| 337 | + /** |
|
| 338 | + * Reverse a formula. |
|
| 339 | + * |
|
| 340 | + * @param string $formula |
|
| 341 | + * |
|
| 342 | + * @throws \Exception |
|
| 343 | + * |
|
| 344 | + * @return string |
|
| 345 | + */ |
|
| 346 | + private static function reverseFormula($formula) |
|
| 347 | + { |
|
| 348 | + if (preg_match('/^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 349 | + return str_replace(' == ', ' != ', $formula); |
|
| 350 | + } |
|
| 351 | + if (preg_match('/^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 352 | + return str_replace(' != ', ' == ', $formula); |
|
| 353 | + } |
|
| 354 | + if (preg_match('/^\(?n == \d+ \|\| n == \d+\)?$/', $formula)) { |
|
| 355 | + return trim(str_replace(array(' == ', ' || '), array(' != ', ' && '), $formula), '()'); |
|
| 356 | + } |
|
| 357 | + $m = null; |
|
| 358 | + if (preg_match('/^(n(?: % \d+)?) == (\d+) && (n(?: % \d+)?) != (\d+)$/', $formula, $m)) { |
|
| 359 | + return "{$m[1]} != {$m[2]} || {$m[3]} == {$m[4]}"; |
|
| 360 | + } |
|
| 361 | + switch ($formula) { |
|
| 362 | + case '(n == 1 || n == 2 || n == 3) || n % 10 != 4 && n % 10 != 6 && n % 10 != 9': |
|
| 363 | + return 'n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)'; |
|
| 364 | + case '(n == 0 || n == 1) || n >= 11 && n <= 99': |
|
| 365 | + return 'n >= 2 && (n < 11 || n > 99)'; |
|
| 366 | + } |
|
| 367 | + throw new Exception("Unable to reverse the formula '{$formula}'"); |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - /** |
|
| 371 | - * Reduce some excessively complex formulas. |
|
| 372 | - * |
|
| 373 | - * @param string $formula |
|
| 374 | - * |
|
| 375 | - * @return string |
|
| 376 | - */ |
|
| 377 | - private static function reduceFormula($formula) |
|
| 378 | - { |
|
| 379 | - $map = array( |
|
| 380 | - 'n != 0 && n != 1' => 'n > 1', |
|
| 381 | - '(n == 0 || n == 1) && n != 0' => 'n == 1', |
|
| 382 | - ); |
|
| 370 | + /** |
|
| 371 | + * Reduce some excessively complex formulas. |
|
| 372 | + * |
|
| 373 | + * @param string $formula |
|
| 374 | + * |
|
| 375 | + * @return string |
|
| 376 | + */ |
|
| 377 | + private static function reduceFormula($formula) |
|
| 378 | + { |
|
| 379 | + $map = array( |
|
| 380 | + 'n != 0 && n != 1' => 'n > 1', |
|
| 381 | + '(n == 0 || n == 1) && n != 0' => 'n == 1', |
|
| 382 | + ); |
|
| 383 | 383 | |
| 384 | - return isset($map[$formula]) ? $map[$formula] : $formula; |
|
| 385 | - } |
|
| 384 | + return isset($map[$formula]) ? $map[$formula] : $formula; |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | - /** |
|
| 388 | - * Take one variable and, if it's a string, we transliterate it to US-ASCII. |
|
| 389 | - * |
|
| 390 | - * @param mixed $value the variable to work on |
|
| 391 | - * |
|
| 392 | - * @throws \Exception |
|
| 393 | - */ |
|
| 394 | - private static function asciifier(&$value) |
|
| 395 | - { |
|
| 396 | - if (is_string($value) && $value !== '') { |
|
| 397 | - // Avoid converting from 'Ÿ' to '"Y', let's prefer 'Y' |
|
| 398 | - $value = strtr($value, array( |
|
| 399 | - 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', |
|
| 400 | - 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', |
|
| 401 | - 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', |
|
| 402 | - 'Ñ' => 'N', |
|
| 403 | - 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', |
|
| 404 | - 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', |
|
| 405 | - 'Ÿ' => 'Y', 'Ý' => 'Y', |
|
| 406 | - 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', |
|
| 407 | - 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', |
|
| 408 | - 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', |
|
| 409 | - 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', |
|
| 410 | - 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', |
|
| 411 | - 'ý' => 'y', 'ÿ' => 'y', |
|
| 412 | - '…' => '...', |
|
| 413 | - 'ʼ' => "'", '’' => "'", |
|
| 414 | - )); |
|
| 415 | - } |
|
| 416 | - } |
|
| 387 | + /** |
|
| 388 | + * Take one variable and, if it's a string, we transliterate it to US-ASCII. |
|
| 389 | + * |
|
| 390 | + * @param mixed $value the variable to work on |
|
| 391 | + * |
|
| 392 | + * @throws \Exception |
|
| 393 | + */ |
|
| 394 | + private static function asciifier(&$value) |
|
| 395 | + { |
|
| 396 | + if (is_string($value) && $value !== '') { |
|
| 397 | + // Avoid converting from 'Ÿ' to '"Y', let's prefer 'Y' |
|
| 398 | + $value = strtr($value, array( |
|
| 399 | + 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', |
|
| 400 | + 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', |
|
| 401 | + 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', |
|
| 402 | + 'Ñ' => 'N', |
|
| 403 | + 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', |
|
| 404 | + 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', |
|
| 405 | + 'Ÿ' => 'Y', 'Ý' => 'Y', |
|
| 406 | + 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', |
|
| 407 | + 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', |
|
| 408 | + 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', |
|
| 409 | + 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', |
|
| 410 | + 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', |
|
| 411 | + 'ý' => 'y', 'ÿ' => 'y', |
|
| 412 | + '…' => '...', |
|
| 413 | + 'ʼ' => "'", '’' => "'", |
|
| 414 | + )); |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | 417 | } |
@@ -78,35 +78,35 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @throws \Exception throws an Exception if $fullId is not valid |
| 80 | 80 | */ |
| 81 | - private function __construct($info) |
|
| 81 | + private function __construct( $info ) |
|
| 82 | 82 | { |
| 83 | - $this->id = $info['id']; |
|
| 84 | - $this->name = $info['name']; |
|
| 85 | - $this->supersededBy = isset($info['supersededBy']) ? $info['supersededBy'] : null; |
|
| 86 | - $this->script = isset($info['script']) ? $info['script'] : null; |
|
| 87 | - $this->territory = isset($info['territory']) ? $info['territory'] : null; |
|
| 88 | - $this->baseLanguage = isset($info['baseLanguage']) ? $info['baseLanguage'] : null; |
|
| 83 | + $this->id = $info[ 'id' ]; |
|
| 84 | + $this->name = $info[ 'name' ]; |
|
| 85 | + $this->supersededBy = isset( $info[ 'supersededBy' ] ) ? $info[ 'supersededBy' ] : null; |
|
| 86 | + $this->script = isset( $info[ 'script' ] ) ? $info[ 'script' ] : null; |
|
| 87 | + $this->territory = isset( $info[ 'territory' ] ) ? $info[ 'territory' ] : null; |
|
| 88 | + $this->baseLanguage = isset( $info[ 'baseLanguage' ] ) ? $info[ 'baseLanguage' ] : null; |
|
| 89 | 89 | // Let's build the category list |
| 90 | 90 | $this->categories = array(); |
| 91 | - foreach ($info['categories'] as $cldrCategoryId => $cldrFormulaAndExamples) { |
|
| 92 | - $category = new Category($cldrCategoryId, $cldrFormulaAndExamples); |
|
| 93 | - foreach ($this->categories as $c) { |
|
| 94 | - if ($category->id === $c->id) { |
|
| 95 | - throw new Exception("The category '{$category->id}' is specified more than once"); |
|
| 91 | + foreach ( $info[ 'categories' ] as $cldrCategoryId => $cldrFormulaAndExamples ) { |
|
| 92 | + $category = new Category( $cldrCategoryId, $cldrFormulaAndExamples ); |
|
| 93 | + foreach ( $this->categories as $c ) { |
|
| 94 | + if ( $category->id === $c->id ) { |
|
| 95 | + throw new Exception( "The category '{$category->id}' is specified more than once" ); |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | - $this->categories[] = $category; |
|
| 98 | + $this->categories[ ] = $category; |
|
| 99 | 99 | } |
| 100 | - if (empty($this->categories)) { |
|
| 101 | - throw new Exception("The language '{$info['id']}' does not have any plural category"); |
|
| 100 | + if ( empty( $this->categories ) ) { |
|
| 101 | + throw new Exception( "The language '{$info[ 'id' ]}' does not have any plural category" ); |
|
| 102 | 102 | } |
| 103 | 103 | // Let's sort the categories from 'zero' to 'other' |
| 104 | - usort($this->categories, function (Category $category1, Category $category2) { |
|
| 105 | - return array_search($category1->id, CldrData::$categories) - array_search($category2->id, CldrData::$categories); |
|
| 104 | + usort( $this->categories, function( Category $category1, Category $category2 ) { |
|
| 105 | + return array_search( $category1->id, CldrData::$categories ) - array_search( $category2->id, CldrData::$categories ); |
|
| 106 | 106 | }); |
| 107 | 107 | // The 'other' category should always be there |
| 108 | - if ($this->categories[count($this->categories) - 1]->id !== CldrData::OTHER_CATEGORY) { |
|
| 109 | - throw new Exception("The language '{$info['id']}' does not have the '" . CldrData::OTHER_CATEGORY . "' plural category"); |
|
| 108 | + if ( $this->categories[ count( $this->categories ) - 1 ]->id !== CldrData::OTHER_CATEGORY ) { |
|
| 109 | + throw new Exception( "The language '{$info[ 'id' ]}' does not have the '" . CldrData::OTHER_CATEGORY . "' plural category" ); |
|
| 110 | 110 | } |
| 111 | 111 | $this->checkAlwaysTrueCategories(); |
| 112 | 112 | $this->checkAlwaysFalseCategories(); |
@@ -124,8 +124,8 @@ discard block |
||
| 124 | 124 | public static function getAll() |
| 125 | 125 | { |
| 126 | 126 | $result = array(); |
| 127 | - foreach (array_keys(CldrData::getLanguageNames()) as $cldrLanguageId) { |
|
| 128 | - $result[] = new self(CldrData::getLanguageInfo($cldrLanguageId)); |
|
| 127 | + foreach ( array_keys( CldrData::getLanguageNames() ) as $cldrLanguageId ) { |
|
| 128 | + $result[ ] = new self( CldrData::getLanguageInfo( $cldrLanguageId ) ); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | return $result; |
@@ -138,12 +138,12 @@ discard block |
||
| 138 | 138 | * |
| 139 | 139 | * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language|null |
| 140 | 140 | */ |
| 141 | - public static function getById($id) |
|
| 141 | + public static function getById( $id ) |
|
| 142 | 142 | { |
| 143 | 143 | $result = null; |
| 144 | - $info = CldrData::getLanguageInfo($id); |
|
| 145 | - if (isset($info)) { |
|
| 146 | - $result = new self($info); |
|
| 144 | + $info = CldrData::getLanguageInfo( $id ); |
|
| 145 | + if ( isset( $info ) ) { |
|
| 146 | + $result = new self( $info ); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | return $result; |
@@ -157,13 +157,13 @@ discard block |
||
| 157 | 157 | public function getUSAsciiClone() |
| 158 | 158 | { |
| 159 | 159 | $clone = clone $this; |
| 160 | - self::asciifier($clone->name); |
|
| 161 | - self::asciifier($clone->formula); |
|
| 160 | + self::asciifier( $clone->name ); |
|
| 161 | + self::asciifier( $clone->formula ); |
|
| 162 | 162 | $clone->categories = array(); |
| 163 | - foreach ($this->categories as $category) { |
|
| 163 | + foreach ( $this->categories as $category ) { |
|
| 164 | 164 | $categoryClone = clone $category; |
| 165 | - self::asciifier($categoryClone->examples); |
|
| 166 | - $clone->categories[] = $categoryClone; |
|
| 165 | + self::asciifier( $categoryClone->examples ); |
|
| 166 | + $clone->categories[ ] = $categoryClone; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | return $clone; |
@@ -176,24 +176,24 @@ discard block |
||
| 176 | 176 | * |
| 177 | 177 | * @return string |
| 178 | 178 | */ |
| 179 | - public function buildFormula($withoutParenthesis = false) |
|
| 179 | + public function buildFormula( $withoutParenthesis = false ) |
|
| 180 | 180 | { |
| 181 | - $numCategories = count($this->categories); |
|
| 182 | - switch ($numCategories) { |
|
| 181 | + $numCategories = count( $this->categories ); |
|
| 182 | + switch ( $numCategories ) { |
|
| 183 | 183 | case 1: |
| 184 | 184 | // Just one category |
| 185 | 185 | return '0'; |
| 186 | 186 | case 2: |
| 187 | - return self::reduceFormula(self::reverseFormula($this->categories[0]->formula)); |
|
| 187 | + return self::reduceFormula( self::reverseFormula( $this->categories[ 0 ]->formula ) ); |
|
| 188 | 188 | default: |
| 189 | - $formula = (string) ($numCategories - 1); |
|
| 190 | - for ($i = $numCategories - 2; $i >= 0; $i--) { |
|
| 191 | - $f = self::reduceFormula($this->categories[$i]->formula); |
|
| 192 | - if (!$withoutParenthesis && !preg_match('/^\([^()]+\)$/', $f)) { |
|
| 189 | + $formula = (string)( $numCategories - 1 ); |
|
| 190 | + for ( $i = $numCategories - 2; $i >= 0; $i-- ) { |
|
| 191 | + $f = self::reduceFormula( $this->categories[ $i ]->formula ); |
|
| 192 | + if ( ! $withoutParenthesis && ! preg_match( '/^\([^()]+\)$/', $f ) ) { |
|
| 193 | 193 | $f = "({$f})"; |
| 194 | 194 | } |
| 195 | 195 | $formula = "{$f} ? {$i} : {$formula}"; |
| 196 | - if (!$withoutParenthesis && $i > 0) { |
|
| 196 | + if ( ! $withoutParenthesis && $i > 0 ) { |
|
| 197 | 197 | $formula = "({$formula})"; |
| 198 | 198 | } |
| 199 | 199 | } |
@@ -212,24 +212,24 @@ discard block |
||
| 212 | 212 | private function checkAlwaysTrueCategories() |
| 213 | 213 | { |
| 214 | 214 | $alwaysTrueCategory = null; |
| 215 | - foreach ($this->categories as $category) { |
|
| 216 | - if ($category->formula === true) { |
|
| 217 | - if (!isset($category->examples)) { |
|
| 218 | - throw new Exception("The category '{$category->id}' should always occur, but it does not have examples (so for CLDR it will never occur for integers!)"); |
|
| 215 | + foreach ( $this->categories as $category ) { |
|
| 216 | + if ( $category->formula === true ) { |
|
| 217 | + if ( ! isset( $category->examples ) ) { |
|
| 218 | + throw new Exception( "The category '{$category->id}' should always occur, but it does not have examples (so for CLDR it will never occur for integers!)" ); |
|
| 219 | 219 | } |
| 220 | 220 | $alwaysTrueCategory = $category; |
| 221 | 221 | break; |
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | - if (isset($alwaysTrueCategory)) { |
|
| 225 | - foreach ($this->categories as $category) { |
|
| 226 | - if (($category !== $alwaysTrueCategory) && isset($category->examples)) { |
|
| 227 | - throw new Exception("The category '{$category->id}' should never occur, but it has some examples (so for CLDR it will occur!)"); |
|
| 224 | + if ( isset( $alwaysTrueCategory ) ) { |
|
| 225 | + foreach ( $this->categories as $category ) { |
|
| 226 | + if ( ( $category !== $alwaysTrueCategory ) && isset( $category->examples ) ) { |
|
| 227 | + throw new Exception( "The category '{$category->id}' should never occur, but it has some examples (so for CLDR it will occur!)" ); |
|
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | $alwaysTrueCategory->id = CldrData::OTHER_CATEGORY; |
| 231 | 231 | $alwaysTrueCategory->formula = null; |
| 232 | - $this->categories = array($alwaysTrueCategory); |
|
| 232 | + $this->categories = array( $alwaysTrueCategory ); |
|
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | |
@@ -243,13 +243,13 @@ discard block |
||
| 243 | 243 | private function checkAlwaysFalseCategories() |
| 244 | 244 | { |
| 245 | 245 | $filtered = array(); |
| 246 | - foreach ($this->categories as $category) { |
|
| 247 | - if ($category->formula === false) { |
|
| 248 | - if (isset($category->examples)) { |
|
| 249 | - throw new Exception("The category '{$category->id}' should never occur, but it has examples (so for CLDR it may occur!)"); |
|
| 246 | + foreach ( $this->categories as $category ) { |
|
| 247 | + if ( $category->formula === false ) { |
|
| 248 | + if ( isset( $category->examples ) ) { |
|
| 249 | + throw new Exception( "The category '{$category->id}' should never occur, but it has examples (so for CLDR it may occur!)" ); |
|
| 250 | 250 | } |
| 251 | 251 | } else { |
| 252 | - $filtered[] = $category; |
|
| 252 | + $filtered[ ] = $category; |
|
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | $this->categories = $filtered; |
@@ -268,22 +268,22 @@ discard block |
||
| 268 | 268 | $goodCategories = array(); |
| 269 | 269 | $badCategories = array(); |
| 270 | 270 | $badCategoriesIds = array(); |
| 271 | - foreach ($this->categories as $category) { |
|
| 272 | - $allCategoriesIds[] = $category->id; |
|
| 273 | - if (isset($category->examples)) { |
|
| 274 | - $goodCategories[] = $category; |
|
| 271 | + foreach ( $this->categories as $category ) { |
|
| 272 | + $allCategoriesIds[ ] = $category->id; |
|
| 273 | + if ( isset( $category->examples ) ) { |
|
| 274 | + $goodCategories[ ] = $category; |
|
| 275 | 275 | } else { |
| 276 | - $badCategories[] = $category; |
|
| 277 | - $badCategoriesIds[] = $category->id; |
|
| 276 | + $badCategories[ ] = $category; |
|
| 277 | + $badCategoriesIds[ ] = $category->id; |
|
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | - if (empty($badCategories)) { |
|
| 280 | + if ( empty( $badCategories ) ) { |
|
| 281 | 281 | return; |
| 282 | 282 | } |
| 283 | 283 | $removeCategoriesWithoutExamples = false; |
| 284 | - switch (implode(',', $badCategoriesIds) . '@' . implode(',', $allCategoriesIds)) { |
|
| 284 | + switch ( implode( ',', $badCategoriesIds ) . '@' . implode( ',', $allCategoriesIds ) ) { |
|
| 285 | 285 | case CldrData::OTHER_CATEGORY . '@one,few,many,' . CldrData::OTHER_CATEGORY: |
| 286 | - switch ($this->buildFormula()) { |
|
| 286 | + switch ( $this->buildFormula() ) { |
|
| 287 | 287 | case '(n % 10 == 1 && n % 100 != 11) ? 0 : ((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : ((n % 10 == 0 || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 11 && n % 100 <= 14) ? 2 : 3))': |
| 288 | 288 | // Numbers ending with 0 => case 2 ('many') |
| 289 | 289 | // Numbers ending with 1 but not with 11 => case 0 ('one') |
@@ -322,12 +322,12 @@ discard block |
||
| 322 | 322 | break; |
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | - if (!$removeCategoriesWithoutExamples) { |
|
| 326 | - throw new Exception("Unhandled case of plural categories without examples '" . implode(', ', $badCategoriesIds) . "' out of '" . implode(', ', $allCategoriesIds) . "'"); |
|
| 325 | + if ( ! $removeCategoriesWithoutExamples ) { |
|
| 326 | + throw new Exception( "Unhandled case of plural categories without examples '" . implode( ', ', $badCategoriesIds ) . "' out of '" . implode( ', ', $allCategoriesIds ) . "'" ); |
|
| 327 | 327 | } |
| 328 | - if ($badCategories[count($badCategories) - 1]->id === CldrData::OTHER_CATEGORY) { |
|
| 328 | + if ( $badCategories[ count( $badCategories ) - 1 ]->id === CldrData::OTHER_CATEGORY ) { |
|
| 329 | 329 | // We're removing the 'other' cagory: let's change the last good category to 'other' |
| 330 | - $lastGood = $goodCategories[count($goodCategories) - 1]; |
|
| 330 | + $lastGood = $goodCategories[ count( $goodCategories ) - 1 ]; |
|
| 331 | 331 | $lastGood->id = CldrData::OTHER_CATEGORY; |
| 332 | 332 | $lastGood->formula = null; |
| 333 | 333 | } |
@@ -343,28 +343,28 @@ discard block |
||
| 343 | 343 | * |
| 344 | 344 | * @return string |
| 345 | 345 | */ |
| 346 | - private static function reverseFormula($formula) |
|
| 346 | + private static function reverseFormula( $formula ) |
|
| 347 | 347 | { |
| 348 | - if (preg_match('/^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 349 | - return str_replace(' == ', ' != ', $formula); |
|
| 348 | + if ( preg_match( '/^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$/', $formula ) ) { |
|
| 349 | + return str_replace( ' == ', ' != ', $formula ); |
|
| 350 | 350 | } |
| 351 | - if (preg_match('/^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
|
| 352 | - return str_replace(' != ', ' == ', $formula); |
|
| 351 | + if ( preg_match( '/^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$/', $formula ) ) { |
|
| 352 | + return str_replace( ' != ', ' == ', $formula ); |
|
| 353 | 353 | } |
| 354 | - if (preg_match('/^\(?n == \d+ \|\| n == \d+\)?$/', $formula)) { |
|
| 355 | - return trim(str_replace(array(' == ', ' || '), array(' != ', ' && '), $formula), '()'); |
|
| 354 | + if ( preg_match( '/^\(?n == \d+ \|\| n == \d+\)?$/', $formula ) ) { |
|
| 355 | + return trim( str_replace( array( ' == ', ' || ' ), array( ' != ', ' && ' ), $formula ), '()' ); |
|
| 356 | 356 | } |
| 357 | 357 | $m = null; |
| 358 | - if (preg_match('/^(n(?: % \d+)?) == (\d+) && (n(?: % \d+)?) != (\d+)$/', $formula, $m)) { |
|
| 359 | - return "{$m[1]} != {$m[2]} || {$m[3]} == {$m[4]}"; |
|
| 358 | + if ( preg_match( '/^(n(?: % \d+)?) == (\d+) && (n(?: % \d+)?) != (\d+)$/', $formula, $m ) ) { |
|
| 359 | + return "{$m[ 1 ]} != {$m[ 2 ]} || {$m[ 3 ]} == {$m[ 4 ]}"; |
|
| 360 | 360 | } |
| 361 | - switch ($formula) { |
|
| 361 | + switch ( $formula ) { |
|
| 362 | 362 | case '(n == 1 || n == 2 || n == 3) || n % 10 != 4 && n % 10 != 6 && n % 10 != 9': |
| 363 | 363 | return 'n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)'; |
| 364 | 364 | case '(n == 0 || n == 1) || n >= 11 && n <= 99': |
| 365 | 365 | return 'n >= 2 && (n < 11 || n > 99)'; |
| 366 | 366 | } |
| 367 | - throw new Exception("Unable to reverse the formula '{$formula}'"); |
|
| 367 | + throw new Exception( "Unable to reverse the formula '{$formula}'" ); |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
@@ -374,14 +374,14 @@ discard block |
||
| 374 | 374 | * |
| 375 | 375 | * @return string |
| 376 | 376 | */ |
| 377 | - private static function reduceFormula($formula) |
|
| 377 | + private static function reduceFormula( $formula ) |
|
| 378 | 378 | { |
| 379 | 379 | $map = array( |
| 380 | 380 | 'n != 0 && n != 1' => 'n > 1', |
| 381 | 381 | '(n == 0 || n == 1) && n != 0' => 'n == 1', |
| 382 | 382 | ); |
| 383 | 383 | |
| 384 | - return isset($map[$formula]) ? $map[$formula] : $formula; |
|
| 384 | + return isset( $map[ $formula ] ) ? $map[ $formula ] : $formula; |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | /** |
@@ -391,11 +391,11 @@ discard block |
||
| 391 | 391 | * |
| 392 | 392 | * @throws \Exception |
| 393 | 393 | */ |
| 394 | - private static function asciifier(&$value) |
|
| 394 | + private static function asciifier( &$value ) |
|
| 395 | 395 | { |
| 396 | - if (is_string($value) && $value !== '') { |
|
| 396 | + if ( is_string( $value ) && $value !== '' ) { |
|
| 397 | 397 | // Avoid converting from 'Ÿ' to '"Y', let's prefer 'Y' |
| 398 | - $value = strtr($value, array( |
|
| 398 | + $value = strtr( $value, array( |
|
| 399 | 399 | 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', |
| 400 | 400 | 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', |
| 401 | 401 | 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | 'ý' => 'y', 'ÿ' => 'y', |
| 412 | 412 | '…' => '...', |
| 413 | 413 | 'ʼ' => "'", '’' => "'", |
| 414 | - )); |
|
| 414 | + ) ); |
|
| 415 | 415 | } |
| 416 | 416 | } |
| 417 | 417 | } |
@@ -13,8 +13,7 @@ discard block |
||
| 13 | 13 | /** |
| 14 | 14 | * Main class to convert the plural rules of a language from CLDR to gettext. |
| 15 | 15 | */ |
| 16 | -class Language |
|
| 17 | -{ |
|
| 16 | +class Language { |
|
| 18 | 17 | /** |
| 19 | 18 | * The language ID. |
| 20 | 19 | * |
@@ -78,8 +77,7 @@ discard block |
||
| 78 | 77 | * |
| 79 | 78 | * @throws \Exception throws an Exception if $fullId is not valid |
| 80 | 79 | */ |
| 81 | - private function __construct($info) |
|
| 82 | - { |
|
| 80 | + private function __construct($info) { |
|
| 83 | 81 | $this->id = $info['id']; |
| 84 | 82 | $this->name = $info['name']; |
| 85 | 83 | $this->supersededBy = isset($info['supersededBy']) ? $info['supersededBy'] : null; |
@@ -121,8 +119,7 @@ discard block |
||
| 121 | 119 | * |
| 122 | 120 | * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language[] |
| 123 | 121 | */ |
| 124 | - public static function getAll() |
|
| 125 | - { |
|
| 122 | + public static function getAll() { |
|
| 126 | 123 | $result = array(); |
| 127 | 124 | foreach (array_keys(CldrData::getLanguageNames()) as $cldrLanguageId) { |
| 128 | 125 | $result[] = new self(CldrData::getLanguageInfo($cldrLanguageId)); |
@@ -138,8 +135,7 @@ discard block |
||
| 138 | 135 | * |
| 139 | 136 | * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language|null |
| 140 | 137 | */ |
| 141 | - public static function getById($id) |
|
| 142 | - { |
|
| 138 | + public static function getById($id) { |
|
| 143 | 139 | $result = null; |
| 144 | 140 | $info = CldrData::getLanguageInfo($id); |
| 145 | 141 | if (isset($info)) { |
@@ -154,8 +150,7 @@ discard block |
||
| 154 | 150 | * |
| 155 | 151 | * @return \GravityKit\GravityView\Foundation\ThirdParty\Gettext\Languages\Language |
| 156 | 152 | */ |
| 157 | - public function getUSAsciiClone() |
|
| 158 | - { |
|
| 153 | + public function getUSAsciiClone() { |
|
| 159 | 154 | $clone = clone $this; |
| 160 | 155 | self::asciifier($clone->name); |
| 161 | 156 | self::asciifier($clone->formula); |
@@ -176,8 +171,7 @@ discard block |
||
| 176 | 171 | * |
| 177 | 172 | * @return string |
| 178 | 173 | */ |
| 179 | - public function buildFormula($withoutParenthesis = false) |
|
| 180 | - { |
|
| 174 | + public function buildFormula($withoutParenthesis = false) { |
|
| 181 | 175 | $numCategories = count($this->categories); |
| 182 | 176 | switch ($numCategories) { |
| 183 | 177 | case 1: |
@@ -209,8 +203,7 @@ discard block |
||
| 209 | 203 | * |
| 210 | 204 | * @throws \Exception |
| 211 | 205 | */ |
| 212 | - private function checkAlwaysTrueCategories() |
|
| 213 | - { |
|
| 206 | + private function checkAlwaysTrueCategories() { |
|
| 214 | 207 | $alwaysTrueCategory = null; |
| 215 | 208 | foreach ($this->categories as $category) { |
| 216 | 209 | if ($category->formula === true) { |
@@ -240,8 +233,7 @@ discard block |
||
| 240 | 233 | * |
| 241 | 234 | * @throws \Exception |
| 242 | 235 | */ |
| 243 | - private function checkAlwaysFalseCategories() |
|
| 244 | - { |
|
| 236 | + private function checkAlwaysFalseCategories() { |
|
| 245 | 237 | $filtered = array(); |
| 246 | 238 | foreach ($this->categories as $category) { |
| 247 | 239 | if ($category->formula === false) { |
@@ -262,8 +254,7 @@ discard block |
||
| 262 | 254 | * |
| 263 | 255 | * @throws \Exception |
| 264 | 256 | */ |
| 265 | - private function checkAllCategoriesWithExamples() |
|
| 266 | - { |
|
| 257 | + private function checkAllCategoriesWithExamples() { |
|
| 267 | 258 | $allCategoriesIds = array(); |
| 268 | 259 | $goodCategories = array(); |
| 269 | 260 | $badCategories = array(); |
@@ -343,8 +334,7 @@ discard block |
||
| 343 | 334 | * |
| 344 | 335 | * @return string |
| 345 | 336 | */ |
| 346 | - private static function reverseFormula($formula) |
|
| 347 | - { |
|
| 337 | + private static function reverseFormula($formula) { |
|
| 348 | 338 | if (preg_match('/^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$/', $formula)) { |
| 349 | 339 | return str_replace(' == ', ' != ', $formula); |
| 350 | 340 | } |
@@ -374,8 +364,7 @@ discard block |
||
| 374 | 364 | * |
| 375 | 365 | * @return string |
| 376 | 366 | */ |
| 377 | - private static function reduceFormula($formula) |
|
| 378 | - { |
|
| 367 | + private static function reduceFormula($formula) { |
|
| 379 | 368 | $map = array( |
| 380 | 369 | 'n != 0 && n != 1' => 'n > 1', |
| 381 | 370 | '(n == 0 || n == 1) && n != 0' => 'n == 1', |
@@ -391,8 +380,7 @@ discard block |
||
| 391 | 380 | * |
| 392 | 381 | * @throws \Exception |
| 393 | 382 | */ |
| 394 | - private static function asciifier(&$value) |
|
| 395 | - { |
|
| 383 | + private static function asciifier(&$value) { |
|
| 396 | 384 | if (is_string($value) && $value !== '') { |
| 397 | 385 | // Avoid converting from 'Ÿ' to '"Y', let's prefer 'Y' |
| 398 | 386 | $value = strtr($value, array( |
@@ -7,13 +7,13 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | spl_autoload_register( |
| 10 | - function ($class) { |
|
| 11 | - if (strpos($class, 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages\\') !== 0) { |
|
| 12 | - return; |
|
| 13 | - } |
|
| 14 | - $file = __DIR__ . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen('GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages'))) . '.php'; |
|
| 15 | - if (is_file($file)) { |
|
| 16 | - require_once $file; |
|
| 17 | - } |
|
| 18 | - } |
|
| 10 | + function ($class) { |
|
| 11 | + if (strpos($class, 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages\\') !== 0) { |
|
| 12 | + return; |
|
| 13 | + } |
|
| 14 | + $file = __DIR__ . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen('GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages'))) . '.php'; |
|
| 15 | + if (is_file($file)) { |
|
| 16 | + require_once $file; |
|
| 17 | + } |
|
| 18 | + } |
|
| 19 | 19 | ); |
@@ -7,12 +7,12 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | spl_autoload_register( |
| 10 | - function ($class) { |
|
| 11 | - if (strpos($class, 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages\\') !== 0) { |
|
| 10 | + function( $class ) { |
|
| 11 | + if ( strpos( $class, 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages\\' ) !== 0 ) { |
|
| 12 | 12 | return; |
| 13 | 13 | } |
| 14 | - $file = __DIR__ . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen('GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages'))) . '.php'; |
|
| 15 | - if (is_file($file)) { |
|
| 14 | + $file = __DIR__ . str_replace( '\\', DIRECTORY_SEPARATOR, substr( $class, strlen( 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Languages' ) ) ) . '.php'; |
|
| 15 | + if ( is_file( $file ) ) { |
|
| 16 | 16 | require_once $file; |
| 17 | 17 | } |
| 18 | 18 | } |
@@ -105,401 +105,401 @@ |
||
| 105 | 105 | */ |
| 106 | 106 | class Translations extends ArrayObject |
| 107 | 107 | { |
| 108 | - const HEADER_LANGUAGE = 'Language'; |
|
| 109 | - const HEADER_PLURAL = 'Plural-Forms'; |
|
| 110 | - const HEADER_DOMAIN = 'X-Domain'; |
|
| 111 | - |
|
| 112 | - public static $options = [ |
|
| 113 | - 'defaultHeaders' => [ |
|
| 114 | - 'Project-Id-Version' => '', |
|
| 115 | - 'Report-Msgid-Bugs-To' => '', |
|
| 116 | - 'Last-Translator' => '', |
|
| 117 | - 'Language-Team' => '', |
|
| 118 | - 'MIME-Version' => '1.0', |
|
| 119 | - 'Content-Type' => 'text/plain; charset=UTF-8', |
|
| 120 | - 'Content-Transfer-Encoding' => '8bit', |
|
| 121 | - ], |
|
| 122 | - 'headersSorting' => false, |
|
| 123 | - 'defaultDateHeaders' => [ |
|
| 124 | - 'POT-Creation-Date', |
|
| 125 | - 'PO-Revision-Date', |
|
| 126 | - ], |
|
| 127 | - ]; |
|
| 128 | - |
|
| 129 | - protected $headers; |
|
| 130 | - |
|
| 131 | - protected $translationClass; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @see ArrayObject::__construct() |
|
| 135 | - */ |
|
| 136 | - public function __construct( |
|
| 137 | - $input = [], |
|
| 138 | - $flags = 0, |
|
| 139 | - $iterator_class = 'ArrayIterator', |
|
| 140 | - $translationClass = 'GravityKit\GravityView\Foundation\ThirdParty\Gettext\Translation' |
|
| 141 | - ) { |
|
| 142 | - $this->headers = static::$options['defaultHeaders']; |
|
| 143 | - |
|
| 144 | - foreach (static::$options['defaultDateHeaders'] as $header) { |
|
| 145 | - $this->headers[$header] = date('c'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $this->headers[self::HEADER_LANGUAGE] = ''; |
|
| 149 | - |
|
| 150 | - $this->translationClass = $translationClass; |
|
| 151 | - |
|
| 152 | - parent::__construct($input, $flags, $iterator_class); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Magic method to create new instances using extractors |
|
| 157 | - * For example: Translations::fromMoFile($filename, $options);. |
|
| 158 | - * |
|
| 159 | - * @return Translations |
|
| 160 | - */ |
|
| 161 | - public static function __callStatic($name, $arguments) |
|
| 162 | - { |
|
| 163 | - if (!preg_match('/^from(\w+)(File|String)$/i', $name, $matches)) { |
|
| 164 | - throw new BadMethodCallException("The method $name does not exists"); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return call_user_func_array([new static(), 'add'.ucfirst($name)], $arguments); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Magic method to import/export the translations to a specific format |
|
| 172 | - * For example: $translations->toMoFile($filename, $options); |
|
| 173 | - * For example: $translations->addFromMoFile($filename, $options);. |
|
| 174 | - * |
|
| 175 | - * @return self|bool |
|
| 176 | - */ |
|
| 177 | - public function __call($name, $arguments) |
|
| 178 | - { |
|
| 179 | - if (!preg_match('/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches)) { |
|
| 180 | - throw new BadMethodCallException("The method $name does not exists"); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($matches[1] === 'addFrom') { |
|
| 184 | - $extractor = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3]; |
|
| 185 | - $source = array_shift($arguments); |
|
| 186 | - $options = array_shift($arguments) ?: []; |
|
| 187 | - |
|
| 188 | - call_user_func($extractor, $source, $this, $options); |
|
| 189 | - |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $generator = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Generators\\'.$matches[2].'::to'.$matches[3]; |
|
| 194 | - |
|
| 195 | - array_unshift($arguments, $this); |
|
| 196 | - |
|
| 197 | - return call_user_func_array($generator, $arguments); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Magic method to clone each translation on clone the translations object. |
|
| 202 | - */ |
|
| 203 | - public function __clone() |
|
| 204 | - { |
|
| 205 | - $array = []; |
|
| 206 | - |
|
| 207 | - foreach ($this as $key => $translation) { |
|
| 208 | - $array[$key] = clone $translation; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $this->exchangeArray($array); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Control the new translations added. |
|
| 216 | - * |
|
| 217 | - * @param mixed $index |
|
| 218 | - * @param Translation $value |
|
| 219 | - * |
|
| 220 | - * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
|
| 221 | - * |
|
| 222 | - * @return Translation |
|
| 223 | - */ |
|
| 224 | - #[\ReturnTypeWillChange] |
|
| 225 | - public function offsetSet($index, $value) |
|
| 226 | - { |
|
| 227 | - if (!($value instanceof Translation)) { |
|
| 228 | - throw new InvalidArgumentException( |
|
| 229 | - 'Only instances of Gettext\\Translation must be added to a Gettext\\Translations' |
|
| 230 | - ); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $id = $value->getId(); |
|
| 234 | - |
|
| 235 | - if ($this->offsetExists($id)) { |
|
| 236 | - $this[$id]->mergeWith($value); |
|
| 237 | - |
|
| 238 | - return $this[$id]; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - parent::offsetSet($id, $value); |
|
| 242 | - |
|
| 243 | - return $value; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Set the plural definition. |
|
| 248 | - * |
|
| 249 | - * @param int $count |
|
| 250 | - * @param string $rule |
|
| 251 | - * |
|
| 252 | - * @return self |
|
| 253 | - */ |
|
| 254 | - public function setPluralForms($count, $rule) |
|
| 255 | - { |
|
| 256 | - if (preg_match('/[a-z]/i', str_replace('n', '', $rule))) { |
|
| 257 | - throw new \InvalidArgumentException('Invalid Plural form: ' . $rule); |
|
| 258 | - } |
|
| 259 | - $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};"); |
|
| 260 | - |
|
| 261 | - return $this; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Returns the parsed plural definition. |
|
| 266 | - * |
|
| 267 | - * @param null|array [count, rule] |
|
| 268 | - */ |
|
| 269 | - public function getPluralForms() |
|
| 270 | - { |
|
| 271 | - $header = $this->getHeader(self::HEADER_PLURAL); |
|
| 272 | - |
|
| 273 | - if (!empty($header) |
|
| 274 | - && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches) |
|
| 275 | - ) { |
|
| 276 | - return [intval($matches[1]), $matches[2]]; |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Set a new header. |
|
| 282 | - * |
|
| 283 | - * @param string $name |
|
| 284 | - * @param string $value |
|
| 285 | - * |
|
| 286 | - * @return self |
|
| 287 | - */ |
|
| 288 | - public function setHeader($name, $value) |
|
| 289 | - { |
|
| 290 | - $name = trim($name); |
|
| 291 | - $this->headers[$name] = trim($value); |
|
| 292 | - |
|
| 293 | - return $this; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Returns a header value. |
|
| 298 | - * |
|
| 299 | - * @param string $name |
|
| 300 | - * |
|
| 301 | - * @return null|string |
|
| 302 | - */ |
|
| 303 | - public function getHeader($name) |
|
| 304 | - { |
|
| 305 | - return isset($this->headers[$name]) ? $this->headers[$name] : null; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Returns all header for this translations (in alphabetic order). |
|
| 310 | - * |
|
| 311 | - * @return array |
|
| 312 | - */ |
|
| 313 | - public function getHeaders() |
|
| 314 | - { |
|
| 315 | - if (static::$options['headersSorting']) { |
|
| 316 | - ksort($this->headers); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return $this->headers; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Removes all headers. |
|
| 324 | - * |
|
| 325 | - * @return self |
|
| 326 | - */ |
|
| 327 | - public function deleteHeaders() |
|
| 328 | - { |
|
| 329 | - $this->headers = []; |
|
| 330 | - |
|
| 331 | - return $this; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Removes one header. |
|
| 336 | - * |
|
| 337 | - * @param string $name |
|
| 338 | - * |
|
| 339 | - * @return self |
|
| 340 | - */ |
|
| 341 | - public function deleteHeader($name) |
|
| 342 | - { |
|
| 343 | - unset($this->headers[$name]); |
|
| 344 | - |
|
| 345 | - return $this; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Returns the language value. |
|
| 350 | - * |
|
| 351 | - * @return string $language |
|
| 352 | - */ |
|
| 353 | - public function getLanguage() |
|
| 354 | - { |
|
| 355 | - return $this->getHeader(self::HEADER_LANGUAGE); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Sets the language and the plural forms. |
|
| 360 | - * |
|
| 361 | - * @param string $language |
|
| 362 | - * |
|
| 363 | - * @throws InvalidArgumentException if the language hasn't been recognized |
|
| 364 | - * |
|
| 365 | - * @return self |
|
| 366 | - */ |
|
| 367 | - public function setLanguage($language) |
|
| 368 | - { |
|
| 369 | - $this->setHeader(self::HEADER_LANGUAGE, trim($language)); |
|
| 370 | - |
|
| 371 | - if (($info = Language::getById($language))) { |
|
| 372 | - return $this->setPluralForms(count($info->categories), $info->formula); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language)); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Checks whether the language is empty or not. |
|
| 380 | - * |
|
| 381 | - * @return bool |
|
| 382 | - */ |
|
| 383 | - public function hasLanguage() |
|
| 384 | - { |
|
| 385 | - $language = $this->getLanguage(); |
|
| 386 | - |
|
| 387 | - return (is_string($language) && ($language !== '')) ? true : false; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Set a new domain for this translations. |
|
| 392 | - * |
|
| 393 | - * @param string $domain |
|
| 394 | - * |
|
| 395 | - * @return self |
|
| 396 | - */ |
|
| 397 | - public function setDomain($domain) |
|
| 398 | - { |
|
| 399 | - $this->setHeader(self::HEADER_DOMAIN, trim($domain)); |
|
| 400 | - |
|
| 401 | - return $this; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Returns the domain. |
|
| 406 | - * |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - public function getDomain() |
|
| 410 | - { |
|
| 411 | - return $this->getHeader(self::HEADER_DOMAIN); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Checks whether the domain is empty or not. |
|
| 416 | - * |
|
| 417 | - * @return bool |
|
| 418 | - */ |
|
| 419 | - public function hasDomain() |
|
| 420 | - { |
|
| 421 | - $domain = $this->getDomain(); |
|
| 422 | - |
|
| 423 | - return (is_string($domain) && ($domain !== '')) ? true : false; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Search for a specific translation. |
|
| 428 | - * |
|
| 429 | - * @param string|Translation $context The context of the translation or a translation instance |
|
| 430 | - * @param string $original The original string |
|
| 431 | - * @warning Translations with custom identifiers (e.g. XLIFF unit IDs) cannot be found using this function. |
|
| 432 | - * |
|
| 433 | - * @return Translation|false |
|
| 434 | - */ |
|
| 435 | - public function find($context, $original = '') |
|
| 436 | - { |
|
| 437 | - if ($context instanceof Translation) { |
|
| 438 | - $id = $context->getId(); |
|
| 439 | - } else { |
|
| 440 | - $id = Translation::generateId($context, $original); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - return $this->offsetExists($id) ? $this[$id] : false; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Count all elements translated |
|
| 448 | - * |
|
| 449 | - * @return integer |
|
| 450 | - */ |
|
| 451 | - public function countTranslated() |
|
| 452 | - { |
|
| 453 | - $c = 0; |
|
| 454 | - foreach ($this as $v) { |
|
| 455 | - if ($v->hasTranslation()) { |
|
| 456 | - $c++; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - return $c; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Creates and insert/merges a new translation. |
|
| 464 | - * |
|
| 465 | - * @param string $context The translation context |
|
| 466 | - * @param string $original The translation original string |
|
| 467 | - * @param string $plural The translation original plural string |
|
| 468 | - * |
|
| 469 | - * @return Translation The translation created |
|
| 470 | - */ |
|
| 471 | - public function insert($context, $original, $plural = '') |
|
| 472 | - { |
|
| 473 | - return $this->offsetSet(null, $this->createNewTranslation($context, $original, $plural)); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Merges this translations with other translations. |
|
| 478 | - * |
|
| 479 | - * @param Translations $translations The translations instance to merge with |
|
| 480 | - * @param int $options |
|
| 481 | - * |
|
| 482 | - * @return self |
|
| 483 | - */ |
|
| 484 | - public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
|
| 485 | - { |
|
| 486 | - Merge::mergeHeaders($translations, $this, $options); |
|
| 487 | - Merge::mergeTranslations($translations, $this, $options); |
|
| 488 | - |
|
| 489 | - return $this; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Create a new instance of a Translation object. |
|
| 494 | - * |
|
| 495 | - * @param string $context The context of the translation |
|
| 496 | - * @param string $original The original string |
|
| 497 | - * @param string $plural The original plural string |
|
| 498 | - * @return Translation New Translation instance |
|
| 499 | - */ |
|
| 500 | - public function createNewTranslation($context, $original, $plural = '') |
|
| 501 | - { |
|
| 502 | - $class = $this->translationClass; |
|
| 503 | - return $class::create($context, $original, $plural); |
|
| 504 | - } |
|
| 108 | + const HEADER_LANGUAGE = 'Language'; |
|
| 109 | + const HEADER_PLURAL = 'Plural-Forms'; |
|
| 110 | + const HEADER_DOMAIN = 'X-Domain'; |
|
| 111 | + |
|
| 112 | + public static $options = [ |
|
| 113 | + 'defaultHeaders' => [ |
|
| 114 | + 'Project-Id-Version' => '', |
|
| 115 | + 'Report-Msgid-Bugs-To' => '', |
|
| 116 | + 'Last-Translator' => '', |
|
| 117 | + 'Language-Team' => '', |
|
| 118 | + 'MIME-Version' => '1.0', |
|
| 119 | + 'Content-Type' => 'text/plain; charset=UTF-8', |
|
| 120 | + 'Content-Transfer-Encoding' => '8bit', |
|
| 121 | + ], |
|
| 122 | + 'headersSorting' => false, |
|
| 123 | + 'defaultDateHeaders' => [ |
|
| 124 | + 'POT-Creation-Date', |
|
| 125 | + 'PO-Revision-Date', |
|
| 126 | + ], |
|
| 127 | + ]; |
|
| 128 | + |
|
| 129 | + protected $headers; |
|
| 130 | + |
|
| 131 | + protected $translationClass; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @see ArrayObject::__construct() |
|
| 135 | + */ |
|
| 136 | + public function __construct( |
|
| 137 | + $input = [], |
|
| 138 | + $flags = 0, |
|
| 139 | + $iterator_class = 'ArrayIterator', |
|
| 140 | + $translationClass = 'GravityKit\GravityView\Foundation\ThirdParty\Gettext\Translation' |
|
| 141 | + ) { |
|
| 142 | + $this->headers = static::$options['defaultHeaders']; |
|
| 143 | + |
|
| 144 | + foreach (static::$options['defaultDateHeaders'] as $header) { |
|
| 145 | + $this->headers[$header] = date('c'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $this->headers[self::HEADER_LANGUAGE] = ''; |
|
| 149 | + |
|
| 150 | + $this->translationClass = $translationClass; |
|
| 151 | + |
|
| 152 | + parent::__construct($input, $flags, $iterator_class); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Magic method to create new instances using extractors |
|
| 157 | + * For example: Translations::fromMoFile($filename, $options);. |
|
| 158 | + * |
|
| 159 | + * @return Translations |
|
| 160 | + */ |
|
| 161 | + public static function __callStatic($name, $arguments) |
|
| 162 | + { |
|
| 163 | + if (!preg_match('/^from(\w+)(File|String)$/i', $name, $matches)) { |
|
| 164 | + throw new BadMethodCallException("The method $name does not exists"); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return call_user_func_array([new static(), 'add'.ucfirst($name)], $arguments); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Magic method to import/export the translations to a specific format |
|
| 172 | + * For example: $translations->toMoFile($filename, $options); |
|
| 173 | + * For example: $translations->addFromMoFile($filename, $options);. |
|
| 174 | + * |
|
| 175 | + * @return self|bool |
|
| 176 | + */ |
|
| 177 | + public function __call($name, $arguments) |
|
| 178 | + { |
|
| 179 | + if (!preg_match('/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches)) { |
|
| 180 | + throw new BadMethodCallException("The method $name does not exists"); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($matches[1] === 'addFrom') { |
|
| 184 | + $extractor = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3]; |
|
| 185 | + $source = array_shift($arguments); |
|
| 186 | + $options = array_shift($arguments) ?: []; |
|
| 187 | + |
|
| 188 | + call_user_func($extractor, $source, $this, $options); |
|
| 189 | + |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $generator = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Generators\\'.$matches[2].'::to'.$matches[3]; |
|
| 194 | + |
|
| 195 | + array_unshift($arguments, $this); |
|
| 196 | + |
|
| 197 | + return call_user_func_array($generator, $arguments); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Magic method to clone each translation on clone the translations object. |
|
| 202 | + */ |
|
| 203 | + public function __clone() |
|
| 204 | + { |
|
| 205 | + $array = []; |
|
| 206 | + |
|
| 207 | + foreach ($this as $key => $translation) { |
|
| 208 | + $array[$key] = clone $translation; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $this->exchangeArray($array); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Control the new translations added. |
|
| 216 | + * |
|
| 217 | + * @param mixed $index |
|
| 218 | + * @param Translation $value |
|
| 219 | + * |
|
| 220 | + * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
|
| 221 | + * |
|
| 222 | + * @return Translation |
|
| 223 | + */ |
|
| 224 | + #[\ReturnTypeWillChange] |
|
| 225 | + public function offsetSet($index, $value) |
|
| 226 | + { |
|
| 227 | + if (!($value instanceof Translation)) { |
|
| 228 | + throw new InvalidArgumentException( |
|
| 229 | + 'Only instances of Gettext\\Translation must be added to a Gettext\\Translations' |
|
| 230 | + ); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $id = $value->getId(); |
|
| 234 | + |
|
| 235 | + if ($this->offsetExists($id)) { |
|
| 236 | + $this[$id]->mergeWith($value); |
|
| 237 | + |
|
| 238 | + return $this[$id]; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + parent::offsetSet($id, $value); |
|
| 242 | + |
|
| 243 | + return $value; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Set the plural definition. |
|
| 248 | + * |
|
| 249 | + * @param int $count |
|
| 250 | + * @param string $rule |
|
| 251 | + * |
|
| 252 | + * @return self |
|
| 253 | + */ |
|
| 254 | + public function setPluralForms($count, $rule) |
|
| 255 | + { |
|
| 256 | + if (preg_match('/[a-z]/i', str_replace('n', '', $rule))) { |
|
| 257 | + throw new \InvalidArgumentException('Invalid Plural form: ' . $rule); |
|
| 258 | + } |
|
| 259 | + $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};"); |
|
| 260 | + |
|
| 261 | + return $this; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Returns the parsed plural definition. |
|
| 266 | + * |
|
| 267 | + * @param null|array [count, rule] |
|
| 268 | + */ |
|
| 269 | + public function getPluralForms() |
|
| 270 | + { |
|
| 271 | + $header = $this->getHeader(self::HEADER_PLURAL); |
|
| 272 | + |
|
| 273 | + if (!empty($header) |
|
| 274 | + && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches) |
|
| 275 | + ) { |
|
| 276 | + return [intval($matches[1]), $matches[2]]; |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Set a new header. |
|
| 282 | + * |
|
| 283 | + * @param string $name |
|
| 284 | + * @param string $value |
|
| 285 | + * |
|
| 286 | + * @return self |
|
| 287 | + */ |
|
| 288 | + public function setHeader($name, $value) |
|
| 289 | + { |
|
| 290 | + $name = trim($name); |
|
| 291 | + $this->headers[$name] = trim($value); |
|
| 292 | + |
|
| 293 | + return $this; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Returns a header value. |
|
| 298 | + * |
|
| 299 | + * @param string $name |
|
| 300 | + * |
|
| 301 | + * @return null|string |
|
| 302 | + */ |
|
| 303 | + public function getHeader($name) |
|
| 304 | + { |
|
| 305 | + return isset($this->headers[$name]) ? $this->headers[$name] : null; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Returns all header for this translations (in alphabetic order). |
|
| 310 | + * |
|
| 311 | + * @return array |
|
| 312 | + */ |
|
| 313 | + public function getHeaders() |
|
| 314 | + { |
|
| 315 | + if (static::$options['headersSorting']) { |
|
| 316 | + ksort($this->headers); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return $this->headers; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Removes all headers. |
|
| 324 | + * |
|
| 325 | + * @return self |
|
| 326 | + */ |
|
| 327 | + public function deleteHeaders() |
|
| 328 | + { |
|
| 329 | + $this->headers = []; |
|
| 330 | + |
|
| 331 | + return $this; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Removes one header. |
|
| 336 | + * |
|
| 337 | + * @param string $name |
|
| 338 | + * |
|
| 339 | + * @return self |
|
| 340 | + */ |
|
| 341 | + public function deleteHeader($name) |
|
| 342 | + { |
|
| 343 | + unset($this->headers[$name]); |
|
| 344 | + |
|
| 345 | + return $this; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Returns the language value. |
|
| 350 | + * |
|
| 351 | + * @return string $language |
|
| 352 | + */ |
|
| 353 | + public function getLanguage() |
|
| 354 | + { |
|
| 355 | + return $this->getHeader(self::HEADER_LANGUAGE); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Sets the language and the plural forms. |
|
| 360 | + * |
|
| 361 | + * @param string $language |
|
| 362 | + * |
|
| 363 | + * @throws InvalidArgumentException if the language hasn't been recognized |
|
| 364 | + * |
|
| 365 | + * @return self |
|
| 366 | + */ |
|
| 367 | + public function setLanguage($language) |
|
| 368 | + { |
|
| 369 | + $this->setHeader(self::HEADER_LANGUAGE, trim($language)); |
|
| 370 | + |
|
| 371 | + if (($info = Language::getById($language))) { |
|
| 372 | + return $this->setPluralForms(count($info->categories), $info->formula); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language)); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Checks whether the language is empty or not. |
|
| 380 | + * |
|
| 381 | + * @return bool |
|
| 382 | + */ |
|
| 383 | + public function hasLanguage() |
|
| 384 | + { |
|
| 385 | + $language = $this->getLanguage(); |
|
| 386 | + |
|
| 387 | + return (is_string($language) && ($language !== '')) ? true : false; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Set a new domain for this translations. |
|
| 392 | + * |
|
| 393 | + * @param string $domain |
|
| 394 | + * |
|
| 395 | + * @return self |
|
| 396 | + */ |
|
| 397 | + public function setDomain($domain) |
|
| 398 | + { |
|
| 399 | + $this->setHeader(self::HEADER_DOMAIN, trim($domain)); |
|
| 400 | + |
|
| 401 | + return $this; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Returns the domain. |
|
| 406 | + * |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + public function getDomain() |
|
| 410 | + { |
|
| 411 | + return $this->getHeader(self::HEADER_DOMAIN); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Checks whether the domain is empty or not. |
|
| 416 | + * |
|
| 417 | + * @return bool |
|
| 418 | + */ |
|
| 419 | + public function hasDomain() |
|
| 420 | + { |
|
| 421 | + $domain = $this->getDomain(); |
|
| 422 | + |
|
| 423 | + return (is_string($domain) && ($domain !== '')) ? true : false; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Search for a specific translation. |
|
| 428 | + * |
|
| 429 | + * @param string|Translation $context The context of the translation or a translation instance |
|
| 430 | + * @param string $original The original string |
|
| 431 | + * @warning Translations with custom identifiers (e.g. XLIFF unit IDs) cannot be found using this function. |
|
| 432 | + * |
|
| 433 | + * @return Translation|false |
|
| 434 | + */ |
|
| 435 | + public function find($context, $original = '') |
|
| 436 | + { |
|
| 437 | + if ($context instanceof Translation) { |
|
| 438 | + $id = $context->getId(); |
|
| 439 | + } else { |
|
| 440 | + $id = Translation::generateId($context, $original); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + return $this->offsetExists($id) ? $this[$id] : false; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Count all elements translated |
|
| 448 | + * |
|
| 449 | + * @return integer |
|
| 450 | + */ |
|
| 451 | + public function countTranslated() |
|
| 452 | + { |
|
| 453 | + $c = 0; |
|
| 454 | + foreach ($this as $v) { |
|
| 455 | + if ($v->hasTranslation()) { |
|
| 456 | + $c++; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + return $c; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Creates and insert/merges a new translation. |
|
| 464 | + * |
|
| 465 | + * @param string $context The translation context |
|
| 466 | + * @param string $original The translation original string |
|
| 467 | + * @param string $plural The translation original plural string |
|
| 468 | + * |
|
| 469 | + * @return Translation The translation created |
|
| 470 | + */ |
|
| 471 | + public function insert($context, $original, $plural = '') |
|
| 472 | + { |
|
| 473 | + return $this->offsetSet(null, $this->createNewTranslation($context, $original, $plural)); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Merges this translations with other translations. |
|
| 478 | + * |
|
| 479 | + * @param Translations $translations The translations instance to merge with |
|
| 480 | + * @param int $options |
|
| 481 | + * |
|
| 482 | + * @return self |
|
| 483 | + */ |
|
| 484 | + public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
|
| 485 | + { |
|
| 486 | + Merge::mergeHeaders($translations, $this, $options); |
|
| 487 | + Merge::mergeTranslations($translations, $this, $options); |
|
| 488 | + |
|
| 489 | + return $this; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Create a new instance of a Translation object. |
|
| 494 | + * |
|
| 495 | + * @param string $context The context of the translation |
|
| 496 | + * @param string $original The original string |
|
| 497 | + * @param string $plural The original plural string |
|
| 498 | + * @return Translation New Translation instance |
|
| 499 | + */ |
|
| 500 | + public function createNewTranslation($context, $original, $plural = '') |
|
| 501 | + { |
|
| 502 | + $class = $this->translationClass; |
|
| 503 | + return $class::create($context, $original, $plural); |
|
| 504 | + } |
|
| 505 | 505 | } |
@@ -134,22 +134,22 @@ discard block |
||
| 134 | 134 | * @see ArrayObject::__construct() |
| 135 | 135 | */ |
| 136 | 136 | public function __construct( |
| 137 | - $input = [], |
|
| 137 | + $input = [ ], |
|
| 138 | 138 | $flags = 0, |
| 139 | 139 | $iterator_class = 'ArrayIterator', |
| 140 | 140 | $translationClass = 'GravityKit\GravityView\Foundation\ThirdParty\Gettext\Translation' |
| 141 | 141 | ) { |
| 142 | - $this->headers = static::$options['defaultHeaders']; |
|
| 142 | + $this->headers = static::$options[ 'defaultHeaders' ]; |
|
| 143 | 143 | |
| 144 | - foreach (static::$options['defaultDateHeaders'] as $header) { |
|
| 145 | - $this->headers[$header] = date('c'); |
|
| 144 | + foreach ( static::$options[ 'defaultDateHeaders' ] as $header ) { |
|
| 145 | + $this->headers[ $header ] = date( 'c' ); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $this->headers[self::HEADER_LANGUAGE] = ''; |
|
| 148 | + $this->headers[ self::HEADER_LANGUAGE ] = ''; |
|
| 149 | 149 | |
| 150 | 150 | $this->translationClass = $translationClass; |
| 151 | 151 | |
| 152 | - parent::__construct($input, $flags, $iterator_class); |
|
| 152 | + parent::__construct( $input, $flags, $iterator_class ); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -158,13 +158,13 @@ discard block |
||
| 158 | 158 | * |
| 159 | 159 | * @return Translations |
| 160 | 160 | */ |
| 161 | - public static function __callStatic($name, $arguments) |
|
| 161 | + public static function __callStatic( $name, $arguments ) |
|
| 162 | 162 | { |
| 163 | - if (!preg_match('/^from(\w+)(File|String)$/i', $name, $matches)) { |
|
| 164 | - throw new BadMethodCallException("The method $name does not exists"); |
|
| 163 | + if ( ! preg_match( '/^from(\w+)(File|String)$/i', $name, $matches ) ) { |
|
| 164 | + throw new BadMethodCallException( "The method $name does not exists" ); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - return call_user_func_array([new static(), 'add'.ucfirst($name)], $arguments); |
|
| 167 | + return call_user_func_array( [ new static(), 'add' . ucfirst( $name ) ], $arguments ); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -174,27 +174,27 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return self|bool |
| 176 | 176 | */ |
| 177 | - public function __call($name, $arguments) |
|
| 177 | + public function __call( $name, $arguments ) |
|
| 178 | 178 | { |
| 179 | - if (!preg_match('/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches)) { |
|
| 180 | - throw new BadMethodCallException("The method $name does not exists"); |
|
| 179 | + if ( ! preg_match( '/^(addFrom|to)(\w+)(File|String)$/i', $name, $matches ) ) { |
|
| 180 | + throw new BadMethodCallException( "The method $name does not exists" ); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - if ($matches[1] === 'addFrom') { |
|
| 184 | - $extractor = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Extractors\\'.$matches[2].'::from'.$matches[3]; |
|
| 185 | - $source = array_shift($arguments); |
|
| 186 | - $options = array_shift($arguments) ?: []; |
|
| 183 | + if ( $matches[ 1 ] === 'addFrom' ) { |
|
| 184 | + $extractor = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Extractors\\' . $matches[ 2 ] . '::from' . $matches[ 3 ]; |
|
| 185 | + $source = array_shift( $arguments ); |
|
| 186 | + $options = array_shift( $arguments ) ?: [ ]; |
|
| 187 | 187 | |
| 188 | - call_user_func($extractor, $source, $this, $options); |
|
| 188 | + call_user_func( $extractor, $source, $this, $options ); |
|
| 189 | 189 | |
| 190 | 190 | return $this; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - $generator = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Generators\\'.$matches[2].'::to'.$matches[3]; |
|
| 193 | + $generator = 'GravityKit\\GravityView\\Foundation\\ThirdParty\\Gettext\\Generators\\' . $matches[ 2 ] . '::to' . $matches[ 3 ]; |
|
| 194 | 194 | |
| 195 | - array_unshift($arguments, $this); |
|
| 195 | + array_unshift( $arguments, $this ); |
|
| 196 | 196 | |
| 197 | - return call_user_func_array($generator, $arguments); |
|
| 197 | + return call_user_func_array( $generator, $arguments ); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -202,13 +202,13 @@ discard block |
||
| 202 | 202 | */ |
| 203 | 203 | public function __clone() |
| 204 | 204 | { |
| 205 | - $array = []; |
|
| 205 | + $array = [ ]; |
|
| 206 | 206 | |
| 207 | - foreach ($this as $key => $translation) { |
|
| 208 | - $array[$key] = clone $translation; |
|
| 207 | + foreach ( $this as $key => $translation ) { |
|
| 208 | + $array[ $key ] = clone $translation; |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $this->exchangeArray($array); |
|
| 211 | + $this->exchangeArray( $array ); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
@@ -221,10 +221,10 @@ discard block |
||
| 221 | 221 | * |
| 222 | 222 | * @return Translation |
| 223 | 223 | */ |
| 224 | - #[\ReturnTypeWillChange] |
|
| 225 | - public function offsetSet($index, $value) |
|
| 224 | + #[\ReturnTypeWillChange ] |
|
| 225 | + public function offsetSet( $index, $value ) |
|
| 226 | 226 | { |
| 227 | - if (!($value instanceof Translation)) { |
|
| 227 | + if ( ! ( $value instanceof Translation ) ) { |
|
| 228 | 228 | throw new InvalidArgumentException( |
| 229 | 229 | 'Only instances of Gettext\\Translation must be added to a Gettext\\Translations' |
| 230 | 230 | ); |
@@ -232,13 +232,13 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | $id = $value->getId(); |
| 234 | 234 | |
| 235 | - if ($this->offsetExists($id)) { |
|
| 236 | - $this[$id]->mergeWith($value); |
|
| 235 | + if ( $this->offsetExists( $id ) ) { |
|
| 236 | + $this[ $id ]->mergeWith( $value ); |
|
| 237 | 237 | |
| 238 | - return $this[$id]; |
|
| 238 | + return $this[ $id ]; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - parent::offsetSet($id, $value); |
|
| 241 | + parent::offsetSet( $id, $value ); |
|
| 242 | 242 | |
| 243 | 243 | return $value; |
| 244 | 244 | } |
@@ -251,12 +251,12 @@ discard block |
||
| 251 | 251 | * |
| 252 | 252 | * @return self |
| 253 | 253 | */ |
| 254 | - public function setPluralForms($count, $rule) |
|
| 254 | + public function setPluralForms( $count, $rule ) |
|
| 255 | 255 | { |
| 256 | - if (preg_match('/[a-z]/i', str_replace('n', '', $rule))) { |
|
| 257 | - throw new \InvalidArgumentException('Invalid Plural form: ' . $rule); |
|
| 256 | + if ( preg_match( '/[a-z]/i', str_replace( 'n', '', $rule ) ) ) { |
|
| 257 | + throw new \InvalidArgumentException( 'Invalid Plural form: ' . $rule ); |
|
| 258 | 258 | } |
| 259 | - $this->setHeader(self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};"); |
|
| 259 | + $this->setHeader( self::HEADER_PLURAL, "nplurals={$count}; plural={$rule};" ); |
|
| 260 | 260 | |
| 261 | 261 | return $this; |
| 262 | 262 | } |
@@ -268,12 +268,12 @@ discard block |
||
| 268 | 268 | */ |
| 269 | 269 | public function getPluralForms() |
| 270 | 270 | { |
| 271 | - $header = $this->getHeader(self::HEADER_PLURAL); |
|
| 271 | + $header = $this->getHeader( self::HEADER_PLURAL ); |
|
| 272 | 272 | |
| 273 | - if (!empty($header) |
|
| 274 | - && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches) |
|
| 273 | + if ( ! empty( $header ) |
|
| 274 | + && preg_match( '/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches ) |
|
| 275 | 275 | ) { |
| 276 | - return [intval($matches[1]), $matches[2]]; |
|
| 276 | + return [ intval( $matches[ 1 ] ), $matches[ 2 ] ]; |
|
| 277 | 277 | } |
| 278 | 278 | } |
| 279 | 279 | |
@@ -285,10 +285,10 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @return self |
| 287 | 287 | */ |
| 288 | - public function setHeader($name, $value) |
|
| 288 | + public function setHeader( $name, $value ) |
|
| 289 | 289 | { |
| 290 | - $name = trim($name); |
|
| 291 | - $this->headers[$name] = trim($value); |
|
| 290 | + $name = trim( $name ); |
|
| 291 | + $this->headers[ $name ] = trim( $value ); |
|
| 292 | 292 | |
| 293 | 293 | return $this; |
| 294 | 294 | } |
@@ -300,9 +300,9 @@ discard block |
||
| 300 | 300 | * |
| 301 | 301 | * @return null|string |
| 302 | 302 | */ |
| 303 | - public function getHeader($name) |
|
| 303 | + public function getHeader( $name ) |
|
| 304 | 304 | { |
| 305 | - return isset($this->headers[$name]) ? $this->headers[$name] : null; |
|
| 305 | + return isset( $this->headers[ $name ] ) ? $this->headers[ $name ] : null; |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -312,8 +312,8 @@ discard block |
||
| 312 | 312 | */ |
| 313 | 313 | public function getHeaders() |
| 314 | 314 | { |
| 315 | - if (static::$options['headersSorting']) { |
|
| 316 | - ksort($this->headers); |
|
| 315 | + if ( static::$options[ 'headersSorting' ] ) { |
|
| 316 | + ksort( $this->headers ); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | return $this->headers; |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | */ |
| 327 | 327 | public function deleteHeaders() |
| 328 | 328 | { |
| 329 | - $this->headers = []; |
|
| 329 | + $this->headers = [ ]; |
|
| 330 | 330 | |
| 331 | 331 | return $this; |
| 332 | 332 | } |
@@ -338,9 +338,9 @@ discard block |
||
| 338 | 338 | * |
| 339 | 339 | * @return self |
| 340 | 340 | */ |
| 341 | - public function deleteHeader($name) |
|
| 341 | + public function deleteHeader( $name ) |
|
| 342 | 342 | { |
| 343 | - unset($this->headers[$name]); |
|
| 343 | + unset( $this->headers[ $name ] ); |
|
| 344 | 344 | |
| 345 | 345 | return $this; |
| 346 | 346 | } |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | */ |
| 353 | 353 | public function getLanguage() |
| 354 | 354 | { |
| 355 | - return $this->getHeader(self::HEADER_LANGUAGE); |
|
| 355 | + return $this->getHeader( self::HEADER_LANGUAGE ); |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | /** |
@@ -364,15 +364,15 @@ discard block |
||
| 364 | 364 | * |
| 365 | 365 | * @return self |
| 366 | 366 | */ |
| 367 | - public function setLanguage($language) |
|
| 367 | + public function setLanguage( $language ) |
|
| 368 | 368 | { |
| 369 | - $this->setHeader(self::HEADER_LANGUAGE, trim($language)); |
|
| 369 | + $this->setHeader( self::HEADER_LANGUAGE, trim( $language ) ); |
|
| 370 | 370 | |
| 371 | - if (($info = Language::getById($language))) { |
|
| 372 | - return $this->setPluralForms(count($info->categories), $info->formula); |
|
| 371 | + if ( ( $info = Language::getById( $language ) ) ) { |
|
| 372 | + return $this->setPluralForms( count( $info->categories ), $info->formula ); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | - throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language)); |
|
| 375 | + throw new InvalidArgumentException( sprintf( 'The language "%s" is not valid', $language ) ); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | /** |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | { |
| 385 | 385 | $language = $this->getLanguage(); |
| 386 | 386 | |
| 387 | - return (is_string($language) && ($language !== '')) ? true : false; |
|
| 387 | + return ( is_string( $language ) && ( $language !== '' ) ) ? true : false; |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | /** |
@@ -394,9 +394,9 @@ discard block |
||
| 394 | 394 | * |
| 395 | 395 | * @return self |
| 396 | 396 | */ |
| 397 | - public function setDomain($domain) |
|
| 397 | + public function setDomain( $domain ) |
|
| 398 | 398 | { |
| 399 | - $this->setHeader(self::HEADER_DOMAIN, trim($domain)); |
|
| 399 | + $this->setHeader( self::HEADER_DOMAIN, trim( $domain ) ); |
|
| 400 | 400 | |
| 401 | 401 | return $this; |
| 402 | 402 | } |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | */ |
| 409 | 409 | public function getDomain() |
| 410 | 410 | { |
| 411 | - return $this->getHeader(self::HEADER_DOMAIN); |
|
| 411 | + return $this->getHeader( self::HEADER_DOMAIN ); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | /** |
@@ -420,7 +420,7 @@ discard block |
||
| 420 | 420 | { |
| 421 | 421 | $domain = $this->getDomain(); |
| 422 | 422 | |
| 423 | - return (is_string($domain) && ($domain !== '')) ? true : false; |
|
| 423 | + return ( is_string( $domain ) && ( $domain !== '' ) ) ? true : false; |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | /** |
@@ -432,15 +432,15 @@ discard block |
||
| 432 | 432 | * |
| 433 | 433 | * @return Translation|false |
| 434 | 434 | */ |
| 435 | - public function find($context, $original = '') |
|
| 435 | + public function find( $context, $original = '' ) |
|
| 436 | 436 | { |
| 437 | - if ($context instanceof Translation) { |
|
| 437 | + if ( $context instanceof Translation ) { |
|
| 438 | 438 | $id = $context->getId(); |
| 439 | 439 | } else { |
| 440 | - $id = Translation::generateId($context, $original); |
|
| 440 | + $id = Translation::generateId( $context, $original ); |
|
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - return $this->offsetExists($id) ? $this[$id] : false; |
|
| 443 | + return $this->offsetExists( $id ) ? $this[ $id ] : false; |
|
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | /** |
@@ -451,8 +451,8 @@ discard block |
||
| 451 | 451 | public function countTranslated() |
| 452 | 452 | { |
| 453 | 453 | $c = 0; |
| 454 | - foreach ($this as $v) { |
|
| 455 | - if ($v->hasTranslation()) { |
|
| 454 | + foreach ( $this as $v ) { |
|
| 455 | + if ( $v->hasTranslation() ) { |
|
| 456 | 456 | $c++; |
| 457 | 457 | } |
| 458 | 458 | } |
@@ -468,9 +468,9 @@ discard block |
||
| 468 | 468 | * |
| 469 | 469 | * @return Translation The translation created |
| 470 | 470 | */ |
| 471 | - public function insert($context, $original, $plural = '') |
|
| 471 | + public function insert( $context, $original, $plural = '' ) |
|
| 472 | 472 | { |
| 473 | - return $this->offsetSet(null, $this->createNewTranslation($context, $original, $plural)); |
|
| 473 | + return $this->offsetSet( null, $this->createNewTranslation( $context, $original, $plural ) ); |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | /** |
@@ -481,10 +481,10 @@ discard block |
||
| 481 | 481 | * |
| 482 | 482 | * @return self |
| 483 | 483 | */ |
| 484 | - public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
|
| 484 | + public function mergeWith( Translations $translations, $options = Merge::DEFAULTS ) |
|
| 485 | 485 | { |
| 486 | - Merge::mergeHeaders($translations, $this, $options); |
|
| 487 | - Merge::mergeTranslations($translations, $this, $options); |
|
| 486 | + Merge::mergeHeaders( $translations, $this, $options ); |
|
| 487 | + Merge::mergeTranslations( $translations, $this, $options ); |
|
| 488 | 488 | |
| 489 | 489 | return $this; |
| 490 | 490 | } |
@@ -497,9 +497,9 @@ discard block |
||
| 497 | 497 | * @param string $plural The original plural string |
| 498 | 498 | * @return Translation New Translation instance |
| 499 | 499 | */ |
| 500 | - public function createNewTranslation($context, $original, $plural = '') |
|
| 500 | + public function createNewTranslation( $context, $original, $plural = '' ) |
|
| 501 | 501 | { |
| 502 | 502 | $class = $this->translationClass; |
| 503 | - return $class::create($context, $original, $plural); |
|
| 503 | + return $class::create( $context, $original, $plural ); |
|
| 504 | 504 | } |
| 505 | 505 | } |
@@ -8,24 +8,21 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | namespace GravityKit\GravityView\Foundation\ThirdParty\Gettext; |
| 10 | 10 | |
| 11 | -abstract class BaseTranslator implements TranslatorInterface |
|
| 12 | -{ |
|
| 11 | +abstract class BaseTranslator implements TranslatorInterface { |
|
| 13 | 12 | /** @var TranslatorInterface */ |
| 14 | 13 | public static $current; |
| 15 | 14 | |
| 16 | 15 | /** |
| 17 | 16 | * @see TranslatorInterface |
| 18 | 17 | */ |
| 19 | - public function noop($original) |
|
| 20 | - { |
|
| 18 | + public function noop($original) { |
|
| 21 | 19 | return $original; |
| 22 | 20 | } |
| 23 | 21 | |
| 24 | 22 | /** |
| 25 | 23 | * @see TranslatorInterface |
| 26 | 24 | */ |
| 27 | - public function register() |
|
| 28 | - { |
|
| 25 | + public function register() { |
|
| 29 | 26 | $previous = static::$current; |
| 30 | 27 | |
| 31 | 28 | static::$current = $this; |
@@ -38,8 +35,7 @@ discard block |
||
| 38 | 35 | /** |
| 39 | 36 | * Include the gettext functions |
| 40 | 37 | */ |
| 41 | - public static function includeFunctions() |
|
| 42 | - { |
|
| 38 | + public static function includeFunctions() { |
|
| 43 | 39 | include_once __DIR__.'/translator_functions.php'; |
| 44 | 40 | } |
| 45 | 41 | } |
@@ -8,8 +8,7 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | namespace GravityKit\GravityView\Foundation\ThirdParty\Gettext\Utils; |
| 10 | 10 | |
| 11 | -class JsFunctionsScanner extends FunctionsScanner |
|
| 12 | -{ |
|
| 11 | +class JsFunctionsScanner extends FunctionsScanner { |
|
| 13 | 12 | protected $code; |
| 14 | 13 | protected $status = []; |
| 15 | 14 | |
@@ -18,8 +17,7 @@ discard block |
||
| 18 | 17 | * |
| 19 | 18 | * @param string $code The php code to scan |
| 20 | 19 | */ |
| 21 | - public function __construct($code) |
|
| 22 | - { |
|
| 20 | + public function __construct($code) { |
|
| 23 | 21 | // Normalize newline characters |
| 24 | 22 | $this->code = str_replace(["\r\n", "\n\r", "\r"], "\n", $code); |
| 25 | 23 | } |
@@ -27,8 +25,7 @@ discard block |
||
| 27 | 25 | /** |
| 28 | 26 | * {@inheritdoc} |
| 29 | 27 | */ |
| 30 | - public function getFunctions(array $constants = []) |
|
| 31 | - { |
|
| 28 | + public function getFunctions(array $constants = []) { |
|
| 32 | 29 | $length = strlen($this->code); |
| 33 | 30 | $line = 1; |
| 34 | 31 | $buffer = ''; |
@@ -240,8 +237,7 @@ discard block |
||
| 240 | 237 | * |
| 241 | 238 | * @return string|bool |
| 242 | 239 | */ |
| 243 | - protected function status($match = null) |
|
| 244 | - { |
|
| 240 | + protected function status($match = null) { |
|
| 245 | 241 | $status = isset($this->status[0]) ? $this->status[0] : null; |
| 246 | 242 | |
| 247 | 243 | if ($match !== null) { |
@@ -256,8 +252,7 @@ discard block |
||
| 256 | 252 | * |
| 257 | 253 | * @param string $status |
| 258 | 254 | */ |
| 259 | - protected function downStatus($status) |
|
| 260 | - { |
|
| 255 | + protected function downStatus($status) { |
|
| 261 | 256 | array_unshift($this->status, $status); |
| 262 | 257 | } |
| 263 | 258 | |
@@ -266,8 +261,7 @@ discard block |
||
| 266 | 261 | * |
| 267 | 262 | * @return string|null |
| 268 | 263 | */ |
| 269 | - protected function upStatus() |
|
| 270 | - { |
|
| 264 | + protected function upStatus() { |
|
| 271 | 265 | return array_shift($this->status); |
| 272 | 266 | } |
| 273 | 267 | |
@@ -278,8 +272,7 @@ discard block |
||
| 278 | 272 | * |
| 279 | 273 | * @return string |
| 280 | 274 | */ |
| 281 | - protected static function prepareArgument($argument) |
|
| 282 | - { |
|
| 275 | + protected static function prepareArgument($argument) { |
|
| 283 | 276 | if ($argument && in_array($argument[0], ['"', "'", '`'], true)) { |
| 284 | 277 | return static::convertString(substr($argument, 1, -1)); |
| 285 | 278 | } |
@@ -292,8 +285,7 @@ discard block |
||
| 292 | 285 | * |
| 293 | 286 | * @return string |
| 294 | 287 | */ |
| 295 | - protected static function convertString($value) |
|
| 296 | - { |
|
| 288 | + protected static function convertString($value) { |
|
| 297 | 289 | if (strpos($value, '\\') === false) { |
| 298 | 290 | return $value; |
| 299 | 291 | } |
@@ -10,8 +10,7 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | use GravityKit\GravityView\Foundation\ThirdParty\Gettext\Extractors\PhpCode; |
| 12 | 12 | |
| 13 | -class PhpFunctionsScanner extends FunctionsScanner |
|
| 14 | -{ |
|
| 13 | +class PhpFunctionsScanner extends FunctionsScanner { |
|
| 15 | 14 | /** |
| 16 | 15 | * PHP tokens of the code to be parsed. |
| 17 | 16 | * |
@@ -31,16 +30,14 @@ discard block |
||
| 31 | 30 | * |
| 32 | 31 | * @param mixed $tag |
| 33 | 32 | */ |
| 34 | - public function enableCommentsExtraction($tag = '') |
|
| 35 | - { |
|
| 33 | + public function enableCommentsExtraction($tag = '') { |
|
| 36 | 34 | $this->extractComments = $tag; |
| 37 | 35 | } |
| 38 | 36 | |
| 39 | 37 | /** |
| 40 | 38 | * Disable comments extraction. |
| 41 | 39 | */ |
| 42 | - public function disableCommentsExtraction() |
|
| 43 | - { |
|
| 40 | + public function disableCommentsExtraction() { |
|
| 44 | 41 | $this->extractComments = false; |
| 45 | 42 | } |
| 46 | 43 | |
@@ -49,8 +46,7 @@ discard block |
||
| 49 | 46 | * |
| 50 | 47 | * @param string $code The php code to scan |
| 51 | 48 | */ |
| 52 | - public function __construct($code) |
|
| 53 | - { |
|
| 49 | + public function __construct($code) { |
|
| 54 | 50 | $this->tokens = array_values( |
| 55 | 51 | array_filter( |
| 56 | 52 | token_get_all($code), |
@@ -64,8 +60,7 @@ discard block |
||
| 64 | 60 | /** |
| 65 | 61 | * {@inheritdoc} |
| 66 | 62 | */ |
| 67 | - public function getFunctions(array $constants = []) |
|
| 68 | - { |
|
| 63 | + public function getFunctions(array $constants = []) { |
|
| 69 | 64 | $count = count($this->tokens); |
| 70 | 65 | /* @var ParsedFunction[] $bufferFunctions */ |
| 71 | 66 | $bufferFunctions = []; |
@@ -180,8 +175,7 @@ discard block |
||
| 180 | 175 | * |
| 181 | 176 | * @return null|ParsedComment Comment or null if comment extraction is disabled or if there is a prefix mismatch. |
| 182 | 177 | */ |
| 183 | - protected function parsePhpComment($value, $line) |
|
| 184 | - { |
|
| 178 | + protected function parsePhpComment($value, $line) { |
|
| 185 | 179 | if ($this->extractComments === false) { |
| 186 | 180 | return null; |
| 187 | 181 | } |
@@ -8,8 +8,7 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | namespace GravityKit\GravityView\Foundation\ThirdParty\Gettext\Utils; |
| 10 | 10 | |
| 11 | -class StringReader |
|
| 12 | -{ |
|
| 11 | +class StringReader { |
|
| 13 | 12 | public $pos; |
| 14 | 13 | public $str; |
| 15 | 14 | public $strlen; |
@@ -19,8 +18,7 @@ discard block |
||
| 19 | 18 | * |
| 20 | 19 | * @param string $str The string to read |
| 21 | 20 | */ |
| 22 | - public function __construct($str) |
|
| 23 | - { |
|
| 21 | + public function __construct($str) { |
|
| 24 | 22 | $this->str = $str; |
| 25 | 23 | $this->strlen = strlen($this->str); |
| 26 | 24 | } |
@@ -32,8 +30,7 @@ discard block |
||
| 32 | 30 | * |
| 33 | 31 | * @return string |
| 34 | 32 | */ |
| 35 | - public function read($bytes) |
|
| 36 | - { |
|
| 33 | + public function read($bytes) { |
|
| 37 | 34 | $data = substr($this->str, $this->pos, $bytes); |
| 38 | 35 | |
| 39 | 36 | $this->seekto($this->pos + $bytes); |
@@ -48,8 +45,7 @@ discard block |
||
| 48 | 45 | * |
| 49 | 46 | * @return int The new position |
| 50 | 47 | */ |
| 51 | - public function seekto($pos) |
|
| 52 | - { |
|
| 48 | + public function seekto($pos) { |
|
| 53 | 49 | $this->pos = ($this->strlen < $pos) ? $this->strlen : $pos; |
| 54 | 50 | |
| 55 | 51 | return $this->pos; |
@@ -11,8 +11,7 @@ discard block |
||
| 11 | 11 | use Exception; |
| 12 | 12 | use GravityKit\GravityView\Foundation\ThirdParty\Gettext\Translations; |
| 13 | 13 | |
| 14 | -abstract class FunctionsScanner |
|
| 15 | -{ |
|
| 14 | +abstract class FunctionsScanner { |
|
| 16 | 15 | /** |
| 17 | 16 | * Scan and returns the functions and the arguments. |
| 18 | 17 | * |
@@ -31,8 +30,7 @@ discard block |
||
| 31 | 30 | * @param array $options The extractor options |
| 32 | 31 | * @throws Exception |
| 33 | 32 | */ |
| 34 | - public function saveGettextFunctions($translations, array $options) |
|
| 35 | - { |
|
| 33 | + public function saveGettextFunctions($translations, array $options) { |
|
| 36 | 34 | $translations = is_array($translations) ? $translations : [$translations]; |
| 37 | 35 | |
| 38 | 36 | /** @var Translations[] $translationByDomain [domain => translations, ..] */ |
@@ -113,8 +111,7 @@ discard block |
||
| 113 | 111 | * @return array|null |
| 114 | 112 | * @throws Exception |
| 115 | 113 | */ |
| 116 | - protected function deconstructArgs($function, $args) |
|
| 117 | - { |
|
| 114 | + protected function deconstructArgs($function, $args) { |
|
| 118 | 115 | $domain = null; |
| 119 | 116 | $context = null; |
| 120 | 117 | $original = null; |
@@ -10,8 +10,7 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | use GravityKit\GravityView\Foundation\ThirdParty\Gettext\Generators\PhpArray; |
| 12 | 12 | |
| 13 | -class Translator extends BaseTranslator implements TranslatorInterface |
|
| 14 | -{ |
|
| 13 | +class Translator extends BaseTranslator implements TranslatorInterface { |
|
| 15 | 14 | protected $domain; |
| 16 | 15 | protected $dictionary = []; |
| 17 | 16 | protected $plurals = []; |
@@ -23,8 +22,7 @@ discard block |
||
| 23 | 22 | * |
| 24 | 23 | * @return static |
| 25 | 24 | */ |
| 26 | - public function loadTranslations($translations) |
|
| 27 | - { |
|
| 25 | + public function loadTranslations($translations) { |
|
| 28 | 26 | if (is_object($translations) && $translations instanceof Translations) { |
| 29 | 27 | $translations = PhpArray::generate($translations, ['includeHeaders' => false]); |
| 30 | 28 | } elseif (is_string($translations) && is_file($translations)) { |
@@ -47,8 +45,7 @@ discard block |
||
| 47 | 45 | * |
| 48 | 46 | * @return static |
| 49 | 47 | */ |
| 50 | - public function defaultDomain($domain) |
|
| 51 | - { |
|
| 48 | + public function defaultDomain($domain) { |
|
| 52 | 49 | $this->domain = $domain; |
| 53 | 50 | |
| 54 | 51 | return $this; |
@@ -59,8 +56,7 @@ discard block |
||
| 59 | 56 | * |
| 60 | 57 | * {@inheritdoc} |
| 61 | 58 | */ |
| 62 | - public function gettext($original) |
|
| 63 | - { |
|
| 59 | + public function gettext($original) { |
|
| 64 | 60 | return $this->dpgettext($this->domain, null, $original); |
| 65 | 61 | } |
| 66 | 62 | |
@@ -69,8 +65,7 @@ discard block |
||
| 69 | 65 | * |
| 70 | 66 | * {@inheritdoc} |
| 71 | 67 | */ |
| 72 | - public function ngettext($original, $plural, $value) |
|
| 73 | - { |
|
| 68 | + public function ngettext($original, $plural, $value) { |
|
| 74 | 69 | return $this->dnpgettext($this->domain, null, $original, $plural, $value); |
| 75 | 70 | } |
| 76 | 71 | |
@@ -79,8 +74,7 @@ discard block |
||
| 79 | 74 | * |
| 80 | 75 | * {@inheritdoc} |
| 81 | 76 | */ |
| 82 | - public function dngettext($domain, $original, $plural, $value) |
|
| 83 | - { |
|
| 77 | + public function dngettext($domain, $original, $plural, $value) { |
|
| 84 | 78 | return $this->dnpgettext($domain, null, $original, $plural, $value); |
| 85 | 79 | } |
| 86 | 80 | |
@@ -89,8 +83,7 @@ discard block |
||
| 89 | 83 | * |
| 90 | 84 | * {@inheritdoc} |
| 91 | 85 | */ |
| 92 | - public function npgettext($context, $original, $plural, $value) |
|
| 93 | - { |
|
| 86 | + public function npgettext($context, $original, $plural, $value) { |
|
| 94 | 87 | return $this->dnpgettext($this->domain, $context, $original, $plural, $value); |
| 95 | 88 | } |
| 96 | 89 | |
@@ -99,8 +92,7 @@ discard block |
||
| 99 | 92 | * |
| 100 | 93 | * {@inheritdoc} |
| 101 | 94 | */ |
| 102 | - public function pgettext($context, $original) |
|
| 103 | - { |
|
| 95 | + public function pgettext($context, $original) { |
|
| 104 | 96 | return $this->dpgettext($this->domain, $context, $original); |
| 105 | 97 | } |
| 106 | 98 | |
@@ -109,8 +101,7 @@ discard block |
||
| 109 | 101 | * |
| 110 | 102 | * {@inheritdoc} |
| 111 | 103 | */ |
| 112 | - public function dgettext($domain, $original) |
|
| 113 | - { |
|
| 104 | + public function dgettext($domain, $original) { |
|
| 114 | 105 | return $this->dpgettext($domain, null, $original); |
| 115 | 106 | } |
| 116 | 107 | |
@@ -119,8 +110,7 @@ discard block |
||
| 119 | 110 | * |
| 120 | 111 | * {@inheritdoc} |
| 121 | 112 | */ |
| 122 | - public function dpgettext($domain, $context, $original) |
|
| 123 | - { |
|
| 113 | + public function dpgettext($domain, $context, $original) { |
|
| 124 | 114 | $translation = $this->getTranslation($domain, $context, $original); |
| 125 | 115 | |
| 126 | 116 | if (isset($translation[0]) && $translation[0] !== '') { |
@@ -135,8 +125,7 @@ discard block |
||
| 135 | 125 | * |
| 136 | 126 | * {@inheritdoc} |
| 137 | 127 | */ |
| 138 | - public function dnpgettext($domain, $context, $original, $plural, $value) |
|
| 139 | - { |
|
| 128 | + public function dnpgettext($domain, $context, $original, $plural, $value) { |
|
| 140 | 129 | $translation = $this->getTranslation($domain, $context, $original); |
| 141 | 130 | $key = $this->getPluralIndex($domain, $value, $translation === false); |
| 142 | 131 | |
@@ -152,8 +141,7 @@ discard block |
||
| 152 | 141 | * |
| 153 | 142 | * @param array $translations |
| 154 | 143 | */ |
| 155 | - protected function addTranslations(array $translations) |
|
| 156 | - { |
|
| 144 | + protected function addTranslations(array $translations) { |
|
| 157 | 145 | $domain = isset($translations['domain']) ? $translations['domain'] : ''; |
| 158 | 146 | |
| 159 | 147 | //Set the first domain loaded as default domain |
@@ -190,8 +178,7 @@ discard block |
||
| 190 | 178 | * |
| 191 | 179 | * @return string|false |
| 192 | 180 | */ |
| 193 | - protected function getTranslation($domain, $context, $original) |
|
| 194 | - { |
|
| 181 | + protected function getTranslation($domain, $context, $original) { |
|
| 195 | 182 | return isset($this->dictionary[$domain][$context][$original]) |
| 196 | 183 | ? $this->dictionary[$domain][$context][$original] |
| 197 | 184 | : false; |
@@ -207,8 +194,7 @@ discard block |
||
| 207 | 194 | * |
| 208 | 195 | * @return int |
| 209 | 196 | */ |
| 210 | - protected function getPluralIndex($domain, $n, $fallback) |
|
| 211 | - { |
|
| 197 | + protected function getPluralIndex($domain, $n, $fallback) { |
|
| 212 | 198 | //Not loaded domain or translation, use a fallback |
| 213 | 199 | if (!isset($this->plurals[$domain]) || $fallback === true) { |
| 214 | 200 | return $n == 1 ? 0 : 1; |
@@ -242,8 +228,7 @@ discard block |
||
| 242 | 228 | * |
| 243 | 229 | * @return string A formatted terse If that PHP can work with. |
| 244 | 230 | */ |
| 245 | - private static function fixTerseIfs($code, $inner = false) |
|
| 246 | - { |
|
| 231 | + private static function fixTerseIfs($code, $inner = false) { |
|
| 247 | 232 | /* |
| 248 | 233 | * (?P<expression>[^?]+) Capture everything up to ? as 'expression' |
| 249 | 234 | * \? ? |