2007-09-15 02:21:51 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\i18n;
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-01-18 04:58:48 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-01-26 05:20:08 +01:00
|
|
|
use SilverStripe\i18n\Data\Locales;
|
|
|
|
use SilverStripe\i18n\Data\Sources;
|
2017-01-18 04:58:48 +01:00
|
|
|
use SilverStripe\i18n\Messages\MessageProvider;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\TemplateGlobalProvider;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2007-09-15 02:21:51 +02:00
|
|
|
/**
|
2007-10-21 21:31:30 +02:00
|
|
|
* Base-class for storage and retrieval of translated entities.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2011-03-22 09:30:34 +01:00
|
|
|
* Please see the 'translatable' module for managing translations of database-content.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* <b>Usage</b>
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* PHP:
|
2008-11-06 03:50:14 +01:00
|
|
|
* <code>
|
|
|
|
* _t('MyNamespace.MYENTITY', 'My default natural language value');
|
2012-04-14 00:16:22 +02:00
|
|
|
* _t('MyNamespace.MYENTITY', 'My default natural language value', 'My explanatory context');
|
2008-11-06 03:50:14 +01:00
|
|
|
* sprintf(_t('MyNamespace.MYENTITY', 'Counting %s things'), 42);
|
|
|
|
* </code>
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* Templates:
|
2008-11-06 03:50:14 +01:00
|
|
|
* <code>
|
2015-07-22 14:51:34 +02:00
|
|
|
* <%t MyNamespace.MYENTITY 'My default natural language value' %>
|
|
|
|
* <%t MyNamespace.MYENTITY 'Counting %s things' s=$ThingsCount %>
|
|
|
|
* <%t MyNamespace.MYENTITY 'Counting {count} things' count=$ThingsCount %>
|
2008-11-06 03:50:14 +01:00
|
|
|
* </code>
|
|
|
|
*
|
2016-04-19 11:04:43 +02:00
|
|
|
* Javascript (see framework/client/dist/js/i18n.js):
|
2008-11-06 03:50:14 +01:00
|
|
|
* <code>
|
|
|
|
* ss.i18n._t('MyEntity.MyNamespace','My default natural language value');
|
|
|
|
* </code>
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2007-10-21 21:31:30 +02:00
|
|
|
* File-based i18n-translations always have a "locale" (e.g. 'en_US').
|
2011-03-22 09:30:34 +01:00
|
|
|
* Common language names (e.g. 'en') are mainly used in the 'translatable' module
|
2007-10-21 21:31:30 +02:00
|
|
|
* database-entities.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* <b>Text Collection</b>
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2007-10-21 21:31:30 +02:00
|
|
|
* Features a "textcollector-mode" that parses all files with a certain extension
|
|
|
|
* (currently *.php and *.ss) for new translatable strings. Textcollector will write
|
|
|
|
* updated string-tables to their respective folders inside the module, and automatically
|
|
|
|
* namespace entities to the classes/templates they are found in (e.g. $lang['en_US']['AssetAdmin']['UPLOADFILES']).
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2007-10-21 21:31:30 +02:00
|
|
|
* Caution: Does not apply any character-set conversion, it is assumed that all content
|
|
|
|
* is stored and represented in UTF-8 (Unicode). Please make sure your files are created with the correct
|
|
|
|
* character-set, and your HTML-templates render UTF-8.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2008-10-10 19:36:31 +02:00
|
|
|
* Caution: The language file has to be stored in the same module path as the "filename namespaces"
|
|
|
|
* on the entities. So an entity stored in $lang['en_US']['AssetAdmin']['DETAILSTAB'] has to
|
|
|
|
* in the language file cms/lang/en_US.php, as the referenced file (AssetAdmin.php) is stored
|
|
|
|
* in the "cms" module.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* <b>Locales</b>
|
2007-09-15 02:21:51 +02:00
|
|
|
*
|
2014-08-04 22:26:22 +02:00
|
|
|
* For the i18n class, a "locale" consists of a language code plus a region code separated by an underscore,
|
2009-04-30 04:51:17 +02:00
|
|
|
* for example "de_AT" for German language ("de") in the region Austria ("AT").
|
|
|
|
* See http://www.w3.org/International/articles/language-tags/ for a detailed description.
|
2014-08-04 22:26:22 +02:00
|
|
|
*
|
2010-10-15 03:27:01 +02:00
|
|
|
* @see http://doc.silverstripe.org/i18n
|
2008-10-03 02:47:35 +02:00
|
|
|
* @see http://www.w3.org/TR/i18n-html-tech-lang
|
2007-09-15 02:21:51 +02:00
|
|
|
* @author Bernat Foj Capell <bernat@silverstripe.com>
|
|
|
|
*/
|
2017-01-18 04:58:48 +01:00
|
|
|
class i18n implements TemplateGlobalProvider
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-01-18 04:58:48 +01:00
|
|
|
use Configurable;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This static variable is used to store the current defined locale.
|
2017-01-18 04:58:48 +01:00
|
|
|
*
|
|
|
|
* @var string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected static $current_locale = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_locale = 'en_US';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $date_format = 'yyyy-MM-dd';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $time_format = 'H:mm';
|
|
|
|
|
|
|
|
/**
|
2017-01-25 04:35:13 +01:00
|
|
|
* Map of rails plurals into standard order (fewest to most)
|
|
|
|
* Note: Default locale only supplies one|other, but non-default locales
|
|
|
|
* can specify custom plurals.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $plurals = [
|
|
|
|
'zero',
|
|
|
|
'one',
|
|
|
|
'two',
|
|
|
|
'few',
|
|
|
|
'many',
|
|
|
|
'other',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plural forms in default (en) locale
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $default_plurals = [
|
|
|
|
'one',
|
|
|
|
'other',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Warn if _t() invoked without a default.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $missing_default_warning = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main translator function. Returns the string defined by $entity according to the
|
2016-11-29 00:31:16 +01:00
|
|
|
* currently set locale.
|
|
|
|
*
|
2017-01-25 04:35:13 +01:00
|
|
|
* Also supports pluralisation of strings. Pass in a `count` argument, as well as a
|
|
|
|
* default value with `|` pipe-delimited options for each plural form.
|
|
|
|
*
|
2017-01-18 04:58:48 +01:00
|
|
|
* @param string $entity Entity that identifies the string. It must be in the form
|
|
|
|
* "Namespace.Entity" where Namespace will be usually the class name where this
|
|
|
|
* string is used and Entity identifies the string inside the namespace.
|
2017-01-25 04:35:13 +01:00
|
|
|
* @param mixed $arg,... Additional arguments are parsed as such:
|
|
|
|
* - Next string argument is a default. Pass in a `|` pipe-delimited value with `{count}`
|
|
|
|
* to do pluralisation.
|
|
|
|
* - Any other string argument after default is context for i18nTextCollector
|
|
|
|
* - Any array argument in any order is an injection parameter list. Pass in a `count`
|
|
|
|
* injection parameter to pluralise.
|
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-01-25 04:35:13 +01:00
|
|
|
public static function _t($entity, $arg = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-01-25 04:35:13 +01:00
|
|
|
// Detect args
|
|
|
|
$default = null;
|
|
|
|
$injection = [];
|
|
|
|
foreach (array_slice(func_get_args(), 1) as $arg) {
|
|
|
|
if (is_array($arg)) {
|
|
|
|
$injection = $arg;
|
|
|
|
} elseif (!isset($default)) {
|
|
|
|
$default = $arg ?: '';
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Encourage the provision of default values so that text collector can discover new strings
|
2017-02-22 04:14:53 +01:00
|
|
|
if (!$default && i18n::config()->uninherited('missing_default_warning')) {
|
2017-01-25 04:35:13 +01:00
|
|
|
user_error("Missing default for localisation key $entity", E_USER_WARNING);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Deprecate legacy injection format (`string %s, %d`)
|
|
|
|
// inject the variables from injectionArray (if present)
|
|
|
|
$sprintfArgs = [];
|
2017-01-25 04:35:13 +01:00
|
|
|
if ($default && !preg_match('/\{[\w\d]*\}/i', $default) && preg_match('/%[s,d]/', $default)) {
|
2017-01-18 04:58:48 +01:00
|
|
|
Deprecation::notice('5.0', 'sprintf style localisation variables are deprecated');
|
|
|
|
$sprintfArgs = array_values($injection);
|
|
|
|
$injection = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// If injection isn't associative, assume legacy injection format
|
|
|
|
$failUnlessSprintf = false;
|
|
|
|
if ($injection && array_values($injection) === $injection) {
|
|
|
|
$failUnlessSprintf = true; // Note: Will trigger either a deprecation error or exception below
|
|
|
|
$sprintfArgs = array_values($injection);
|
|
|
|
$injection = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 04:35:13 +01:00
|
|
|
// Detect plurals: Has a {count} argument as well as a `|` pipe delimited string (if provided)
|
|
|
|
$isPlural = isset($injection['count']);
|
|
|
|
$count = $isPlural ? $injection['count'] : null;
|
|
|
|
// Refine check against default
|
|
|
|
if ($isPlural && $default && !static::parse_plurals($default)) {
|
|
|
|
$isPlural = false;
|
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Pass back to translation backend
|
2017-01-25 04:35:13 +01:00
|
|
|
if ($isPlural) {
|
|
|
|
$result = static::getMessageProvider()->pluralise($entity, $default, $injection, $count);
|
|
|
|
} else {
|
|
|
|
$result = static::getMessageProvider()->translate($entity, $default, $injection);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// Sometimes default is omitted, so we don't know we have %s injection format until after translation
|
|
|
|
if (!$default && !preg_match('/\{[\w\d]*\}/i', $result) && preg_match('/%[s,d]/', $result)) {
|
|
|
|
Deprecation::notice('5.0', 'sprintf style localisation is deprecated');
|
|
|
|
if ($injection) {
|
|
|
|
$sprintfArgs = array_values($injection);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-01-18 04:58:48 +01:00
|
|
|
} elseif ($failUnlessSprintf) {
|
|
|
|
// Note: After removing deprecated code, you can move this error up into the is-associative check
|
|
|
|
// Neither default nor translated strings were %s substituted, and our array isn't associative
|
|
|
|
throw new InvalidArgumentException('Injection must be an associative array');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
// @deprecated (see above)
|
|
|
|
if ($sprintfArgs) {
|
|
|
|
return vsprintf($result, $sprintfArgs);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 04:58:48 +01:00
|
|
|
return $result;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-25 04:35:13 +01:00
|
|
|
* Split plural string into standard CLDR array form.
|
|
|
|
* A string is considered a pluralised form if it has a {count} argument, and
|
|
|
|
* a single `|` pipe-delimiting character.
|
2017-01-18 04:58:48 +01:00
|
|
|
*
|
2017-01-25 04:35:13 +01:00
|
|
|
* Note: Only splits in the default (en) locale as the string form contains limited metadata.
|
2017-01-18 04:58:48 +01:00
|
|
|
*
|
2017-01-25 04:35:13 +01:00
|
|
|
* @param string $string Input string
|
|
|
|
* @return array List of plural forms, or empty array if not plural
|
|
|
|
*/
|
|
|
|
public static function parse_plurals($string)
|
|
|
|
{
|
|
|
|
if (strstr($string, '|') && strstr($string, '{count}')) {
|
2017-02-22 04:14:53 +01:00
|
|
|
$keys = i18n::config()->uninherited('default_plurals');
|
2017-01-25 04:35:13 +01:00
|
|
|
$values = explode('|', $string);
|
|
|
|
if (count($keys) == count($values)) {
|
|
|
|
return array_combine($keys, $values);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert CLDR array plural form to `|` pipe-delimited string.
|
|
|
|
* Unlike parse_plurals, this supports all locale forms (not just en)
|
2017-01-18 04:58:48 +01:00
|
|
|
*
|
2017-01-25 04:35:13 +01:00
|
|
|
* @param array $plurals
|
|
|
|
* @return string Delimited string, or null if not plurals
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-01-25 04:35:13 +01:00
|
|
|
public static function encode_plurals($plurals)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-01-25 04:35:13 +01:00
|
|
|
// Validate against global plural list
|
2017-02-22 04:14:53 +01:00
|
|
|
$forms = i18n::config()->uninherited('plurals');
|
2017-01-25 04:35:13 +01:00
|
|
|
$forms = array_combine($forms, $forms);
|
|
|
|
$intersect = array_intersect_key($plurals, $forms);
|
|
|
|
if ($intersect) {
|
|
|
|
return implode('|', $intersect);
|
|
|
|
}
|
|
|
|
return null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matches a given locale with the closest translation available in the system
|
|
|
|
*
|
|
|
|
* @param string $locale locale code
|
|
|
|
* @return string Locale of closest available translation, if available
|
|
|
|
*/
|
|
|
|
public static function get_closest_translation($locale)
|
|
|
|
{
|
|
|
|
// Check if exact match
|
2017-01-26 05:20:08 +01:00
|
|
|
$pool = self::getSources()->getKnownLocales();
|
2016-11-29 00:31:16 +01:00
|
|
|
if (isset($pool[$locale])) {
|
|
|
|
return $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to best locale for common language
|
2017-01-26 05:20:08 +01:00
|
|
|
$localesData = static::getData();
|
|
|
|
$lang = $localesData->langFromLocale($locale);
|
|
|
|
$candidate = $localesData->localeFromLang($lang);
|
2016-11-29 00:31:16 +01:00
|
|
|
if (isset($pool[$candidate])) {
|
|
|
|
return $candidate;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a RFC 1766 compatible language code,
|
|
|
|
* e.g. "en-US".
|
|
|
|
*
|
|
|
|
* @see http://www.ietf.org/rfc/rfc1766.txt
|
|
|
|
* @see http://tools.ietf.org/html/rfc2616#section-3.10
|
|
|
|
*
|
|
|
|
* @param string $locale
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function convert_rfc1766($locale)
|
|
|
|
{
|
|
|
|
return str_replace('_', '-', $locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current locale, used as the default for
|
|
|
|
* any localized classes, such as {@link FormField} or {@link DBField}
|
|
|
|
* instances. Locales can also be persisted in {@link Member->Locale},
|
|
|
|
* for example in the {@link CMSMain} interface the Member locale
|
|
|
|
* overrules the global locale value set here.
|
|
|
|
*
|
|
|
|
* @param string $locale Locale to be set. See
|
|
|
|
* http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html for a list
|
|
|
|
* of possible locales.
|
|
|
|
*/
|
|
|
|
public static function set_locale($locale)
|
|
|
|
{
|
|
|
|
if ($locale) {
|
|
|
|
self::$current_locale = $locale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
/**
|
|
|
|
* Temporarily set the locale while invoking a callback
|
|
|
|
*
|
|
|
|
* @param string $locale
|
|
|
|
* @param callable $callback
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function with_locale($locale, $callback)
|
|
|
|
{
|
|
|
|
$oldLocale = self::$current_locale;
|
|
|
|
static::set_locale($locale);
|
|
|
|
try {
|
|
|
|
return $callback();
|
|
|
|
} finally {
|
|
|
|
static::set_locale($oldLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Get the current locale.
|
|
|
|
* Used by {@link Member::populateDefaults()}
|
|
|
|
*
|
|
|
|
* @return string Current locale in the system
|
|
|
|
*/
|
|
|
|
public static function get_locale()
|
|
|
|
{
|
2017-02-22 04:14:53 +01:00
|
|
|
return self::$current_locale ?: i18n::config()->uninherited('default_locale');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the script direction in format compatible with the HTML "dir" attribute.
|
|
|
|
*
|
|
|
|
* @see http://www.w3.org/International/tutorials/bidi-xhtml/
|
2017-01-18 04:58:48 +01:00
|
|
|
* @param string $locale Optional locale incl. region (underscored)
|
|
|
|
* @return string "rtl" or "ltr"
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function get_script_direction($locale = null)
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
return static::getData()->scriptDirection($locale);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_template_global_variables()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'i18nLocale' => 'get_locale',
|
|
|
|
'get_locale',
|
|
|
|
'i18nScriptDirection' => 'get_script_direction',
|
|
|
|
);
|
|
|
|
}
|
2017-01-18 04:58:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return MessageProvider
|
|
|
|
*/
|
|
|
|
public static function getMessageProvider()
|
|
|
|
{
|
|
|
|
return Injector::inst()->get(MessageProvider::class);
|
|
|
|
}
|
2017-01-26 05:20:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Localisation data source
|
|
|
|
*
|
|
|
|
* @return Locales
|
|
|
|
*/
|
|
|
|
public static function getData()
|
|
|
|
{
|
|
|
|
return Injector::inst()->get(Locales::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data sources for localisation strings
|
|
|
|
*
|
|
|
|
* @return Sources
|
|
|
|
*/
|
|
|
|
public static function getSources()
|
|
|
|
{
|
|
|
|
return Injector::inst()->get(Sources::class);
|
|
|
|
}
|
2007-09-15 02:21:51 +02:00
|
|
|
}
|