| Total Complexity | 43 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like i18n often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use i18n, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 69 | class i18n implements TemplateGlobalProvider |
||
| 70 | { |
||
| 71 | use Configurable; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * This static variable is used to store the current defined locale. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected static $current_locale = ''; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @config |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private static $default_locale = 'en_US'; |
||
|
|
|||
| 85 | |||
| 86 | /** |
||
| 87 | * System-wide date format. Will be overruled for CMS UI display |
||
| 88 | * by the format defaults inferred from the browser as well as |
||
| 89 | * any user-specific locale preferences. |
||
| 90 | * |
||
| 91 | * @config |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | private static $date_format = 'yyyy-MM-dd'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * System-wide time format. Will be overruled for CMS UI display |
||
| 98 | * by the format defaults inferred from the browser as well as |
||
| 99 | * any user-specific locale preferences. |
||
| 100 | * |
||
| 101 | * @config |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private static $time_format = 'H:mm'; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Map of rails plurals into standard order (fewest to most) |
||
| 108 | * Note: Default locale only supplies one|other, but non-default locales |
||
| 109 | * can specify custom plurals. |
||
| 110 | * |
||
| 111 | * @config |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private static $plurals = [ |
||
| 115 | 'zero', |
||
| 116 | 'one', |
||
| 117 | 'two', |
||
| 118 | 'few', |
||
| 119 | 'many', |
||
| 120 | 'other', |
||
| 121 | ]; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Plural forms in default (en) locale |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | private static $default_plurals = [ |
||
| 129 | 'one', |
||
| 130 | 'other', |
||
| 131 | ]; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Warn if _t() invoked without a default. |
||
| 135 | * |
||
| 136 | * @config |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | private static $missing_default_warning = true; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * This is the main translator function. Returns the string defined by $entity according to the |
||
| 143 | * currently set locale. |
||
| 144 | * |
||
| 145 | * Also supports pluralisation of strings. Pass in a `count` argument, as well as a |
||
| 146 | * default value with `|` pipe-delimited options for each plural form. |
||
| 147 | * |
||
| 148 | * @param string $entity Entity that identifies the string. It must be in the form |
||
| 149 | * "Namespace.Entity" where Namespace will be usually the class name where this |
||
| 150 | * string is used and Entity identifies the string inside the namespace. |
||
| 151 | * @param mixed $arg,... Additional arguments are parsed as such: |
||
| 152 | * - Next string argument is a default. Pass in a `|` pipe-delimited value with `{count}` |
||
| 153 | * to do pluralisation. |
||
| 154 | * - Any other string argument after default is context for i18nTextCollector |
||
| 155 | * - Any array argument in any order is an injection parameter list. Pass in a `count` |
||
| 156 | * injection parameter to pluralise. |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public static function _t($entity, $arg = null) |
||
| 160 | { |
||
| 161 | // Detect args |
||
| 162 | $default = null; |
||
| 163 | $injection = []; |
||
| 164 | foreach (array_slice(func_get_args(), 1) as $arg) { |
||
| 165 | if (is_array($arg)) { |
||
| 166 | $injection = $arg; |
||
| 167 | } elseif (!isset($default)) { |
||
| 168 | $default = $arg ?: ''; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // Encourage the provision of default values so that text collector can discover new strings |
||
| 173 | if (!$default && i18n::config()->uninherited('missing_default_warning')) { |
||
| 174 | user_error("Missing default for localisation key $entity", E_USER_WARNING); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Deprecate legacy injection format (`string %s, %d`) |
||
| 178 | // inject the variables from injectionArray (if present) |
||
| 179 | $sprintfArgs = []; |
||
| 180 | if ($default && !preg_match('/\{[\w\d]*\}/i', $default) && preg_match('/%[s,d]/', $default)) { |
||
| 181 | Deprecation::notice('5.0', 'sprintf style localisation variables are deprecated'); |
||
| 182 | $sprintfArgs = array_values($injection); |
||
| 183 | $injection = []; |
||
| 184 | } |
||
| 185 | |||
| 186 | // If injection isn't associative, assume legacy injection format |
||
| 187 | $failUnlessSprintf = false; |
||
| 188 | if ($injection && array_values($injection) === $injection) { |
||
| 189 | $failUnlessSprintf = true; // Note: Will trigger either a deprecation error or exception below |
||
| 190 | $sprintfArgs = array_values($injection); |
||
| 191 | $injection = []; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Detect plurals: Has a {count} argument as well as a `|` pipe delimited string (if provided) |
||
| 195 | $isPlural = isset($injection['count']); |
||
| 196 | $count = $isPlural ? $injection['count'] : null; |
||
| 197 | // Refine check against default |
||
| 198 | if ($isPlural && $default && !static::parse_plurals($default)) { |
||
| 199 | $isPlural = false; |
||
| 200 | } |
||
| 201 | |||
| 202 | // Pass back to translation backend |
||
| 203 | if ($isPlural) { |
||
| 204 | $result = static::getMessageProvider()->pluralise($entity, $default, $injection, $count); |
||
| 205 | } else { |
||
| 206 | $result = static::getMessageProvider()->translate($entity, $default, $injection); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Sometimes default is omitted, so we don't know we have %s injection format until after translation |
||
| 210 | if (!$default && !preg_match('/\{[\w\d]*\}/i', $result) && preg_match('/%[s,d]/', $result)) { |
||
| 211 | Deprecation::notice('5.0', 'sprintf style localisation is deprecated'); |
||
| 212 | if ($injection) { |
||
| 213 | $sprintfArgs = array_values($injection); |
||
| 214 | } |
||
| 215 | } elseif ($failUnlessSprintf) { |
||
| 216 | // Note: After removing deprecated code, you can move this error up into the is-associative check |
||
| 217 | // Neither default nor translated strings were %s substituted, and our array isn't associative |
||
| 218 | throw new InvalidArgumentException('Injection must be an associative array'); |
||
| 219 | } |
||
| 220 | |||
| 221 | // @deprecated (see above) |
||
| 222 | if ($sprintfArgs) { |
||
| 223 | return vsprintf($result, $sprintfArgs); |
||
| 224 | } |
||
| 225 | |||
| 226 | return $result; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Split plural string into standard CLDR array form. |
||
| 231 | * A string is considered a pluralised form if it has a {count} argument, and |
||
| 232 | * a single `|` pipe-delimiting character. |
||
| 233 | * |
||
| 234 | * Note: Only splits in the default (en) locale as the string form contains limited metadata. |
||
| 235 | * |
||
| 236 | * @param string $string Input string |
||
| 237 | * @return array List of plural forms, or empty array if not plural |
||
| 238 | */ |
||
| 239 | public static function parse_plurals($string) |
||
| 240 | { |
||
| 241 | if (strstr($string, '|') && strstr($string, '{count}')) { |
||
| 242 | $keys = i18n::config()->uninherited('default_plurals'); |
||
| 243 | $values = explode('|', $string); |
||
| 244 | if (count($keys) == count($values)) { |
||
| 245 | return array_combine($keys, $values); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | return []; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Convert CLDR array plural form to `|` pipe-delimited string. |
||
| 253 | * Unlike parse_plurals, this supports all locale forms (not just en) |
||
| 254 | * |
||
| 255 | * @param array $plurals |
||
| 256 | * @return string Delimited string, or null if not plurals |
||
| 257 | */ |
||
| 258 | public static function encode_plurals($plurals) |
||
| 259 | { |
||
| 260 | // Validate against global plural list |
||
| 261 | $forms = i18n::config()->uninherited('plurals'); |
||
| 262 | $forms = array_combine($forms, $forms); |
||
| 263 | $intersect = array_intersect_key($plurals, $forms); |
||
| 264 | if ($intersect) { |
||
| 265 | return implode('|', $intersect); |
||
| 266 | } |
||
| 267 | return null; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Matches a given locale with the closest translation available in the system |
||
| 272 | * |
||
| 273 | * @param string $locale locale code |
||
| 274 | * @return string Locale of closest available translation, if available |
||
| 275 | */ |
||
| 276 | public static function get_closest_translation($locale) |
||
| 277 | { |
||
| 278 | // Check if exact match |
||
| 279 | $pool = self::getSources()->getKnownLocales(); |
||
| 280 | if (isset($pool[$locale])) { |
||
| 281 | return $locale; |
||
| 282 | } |
||
| 283 | |||
| 284 | // Fallback to best locale for common language |
||
| 285 | $localesData = static::getData(); |
||
| 286 | $lang = $localesData->langFromLocale($locale); |
||
| 287 | $candidate = $localesData->localeFromLang($lang); |
||
| 288 | if (isset($pool[$candidate])) { |
||
| 289 | return $candidate; |
||
| 290 | } |
||
| 291 | return null; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets a RFC 1766 compatible language code, |
||
| 296 | * e.g. "en-US". |
||
| 297 | * |
||
| 298 | * @see http://www.ietf.org/rfc/rfc1766.txt |
||
| 299 | * @see http://tools.ietf.org/html/rfc2616#section-3.10 |
||
| 300 | * |
||
| 301 | * @param string $locale |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public static function convert_rfc1766($locale) |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set the current locale, used as the default for |
||
| 311 | * any localized classes, such as {@link FormField} or {@link DBField} |
||
| 312 | * instances. Locales can also be persisted in {@link Member->Locale}, |
||
| 313 | * for example in the {@link CMSMain} interface the Member locale |
||
| 314 | * overrules the global locale value set here. |
||
| 315 | * |
||
| 316 | * @param string $locale Locale to be set. See |
||
| 317 | * http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html for a list |
||
| 318 | * of possible locales. |
||
| 319 | */ |
||
| 320 | public static function set_locale($locale) |
||
| 321 | { |
||
| 322 | if ($locale) { |
||
| 323 | self::$current_locale = $locale; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Temporarily set the locale while invoking a callback |
||
| 329 | * |
||
| 330 | * @param string $locale |
||
| 331 | * @param callable $callback |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | public static function with_locale($locale, $callback) |
||
| 335 | { |
||
| 336 | $oldLocale = self::$current_locale; |
||
| 337 | static::set_locale($locale); |
||
| 338 | try { |
||
| 339 | return $callback(); |
||
| 340 | } finally { |
||
| 341 | static::set_locale($oldLocale); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get the current locale. |
||
| 347 | * Used by {@link Member::populateDefaults()} |
||
| 348 | * |
||
| 349 | * @return string Current locale in the system |
||
| 350 | */ |
||
| 351 | public static function get_locale() |
||
| 352 | { |
||
| 353 | if (!self::$current_locale) { |
||
| 354 | self::$current_locale = i18n::config()->uninherited('default_locale'); |
||
| 355 | } |
||
| 356 | return self::$current_locale; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the script direction in format compatible with the HTML "dir" attribute. |
||
| 361 | * |
||
| 362 | * @see http://www.w3.org/International/tutorials/bidi-xhtml/ |
||
| 363 | * @param string $locale Optional locale incl. region (underscored) |
||
| 364 | * @return string "rtl" or "ltr" |
||
| 365 | */ |
||
| 366 | public static function get_script_direction($locale = null) |
||
| 367 | { |
||
| 368 | return static::getData()->scriptDirection($locale); |
||
| 369 | } |
||
| 370 | |||
| 371 | public static function get_template_global_variables() |
||
| 372 | { |
||
| 373 | return array( |
||
| 374 | 'i18nLocale' => 'get_locale', |
||
| 375 | 'get_locale', |
||
| 376 | 'i18nScriptDirection' => 'get_script_direction', |
||
| 377 | ); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return MessageProvider |
||
| 382 | */ |
||
| 383 | public static function getMessageProvider() |
||
| 384 | { |
||
| 385 | return Injector::inst()->get(MessageProvider::class); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Localisation data source |
||
| 390 | * |
||
| 391 | * @return Locales |
||
| 392 | */ |
||
| 393 | public static function getData() |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get data sources for localisation strings |
||
| 400 | * |
||
| 401 | * @return Sources |
||
| 402 | */ |
||
| 403 | public static function getSources() |
||
| 406 | } |
||
| 407 | } |
||
| 408 |