silverstripe-framework/src/i18n/i18n.php

407 lines
13 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\i18n;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\i18n\Data\Locales;
use SilverStripe\i18n\Data\Sources;
use SilverStripe\i18n\Messages\MessageProvider;
use SilverStripe\View\TemplateGlobalProvider;
use InvalidArgumentException;
/**
* Base-class for storage and retrieval of translated entities.
2014-08-04 22:26:22 +02:00
*
* Please see the 'translatable' module for managing translations of database-content.
2014-08-04 22:26:22 +02:00
*
* <b>Usage</b>
2014-08-04 22:26:22 +02:00
*
* PHP:
* <code>
* _t('MyNamespace.MYENTITY', 'My default natural language value');
* _t('MyNamespace.MYENTITY', 'My default natural language value', 'My explanatory context');
* _t('MyNamespace.MYENTITY', 'Counting {number} things', ['number' => 42]);
* </code>
2014-08-04 22:26:22 +02:00
*
* Templates:
* <code>
2015-07-22 14:51:34 +02:00
* <%t MyNamespace.MYENTITY 'My default natural language value' %>
* <%t MyNamespace.MYENTITY 'Counting {count} things' count=$ThingsCount %>
* </code>
*
* Javascript (see framework/client/dist/js/i18n.js):
* <code>
* ss.i18n._t('MyEntity.MyNamespace','My default natural language value');
* </code>
2014-08-04 22:26:22 +02:00
*
* File-based i18n-translations always have a "locale" (e.g. 'en_US').
* Common language names (e.g. 'en') are mainly used in the 'translatable' module
* database-entities.
2014-08-04 22:26:22 +02:00
*
* <b>Text Collection</b>
2014-08-04 22:26:22 +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
*
* 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
*
* 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
*
* <b>Locales</b>
*
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,
* 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
*
* @see http://doc.silverstripe.org/i18n
* @see http://www.w3.org/TR/i18n-html-tech-lang
* @author Bernat Foj Capell <bernat@silverstripe.com>
*/
class i18n implements TemplateGlobalProvider
2016-11-29 00:31:16 +01:00
{
use Configurable;
2016-11-29 00:31:16 +01:00
/**
* This static variable is used to store the current defined locale.
*
* @var string
2016-11-29 00:31:16 +01:00
*/
protected static $current_locale = '';
/**
* @config
* @var string
*/
private static $default_locale = 'en_US';
/**
* System-wide date format. Will be overruled for CMS UI display
* by the format defaults inferred from the browser as well as
* any user-specific locale preferences.
*
2016-11-29 00:31:16 +01:00
* @config
* @var string
*/
private static $date_format = 'yyyy-MM-dd';
/**
* System-wide time format. Will be overruled for CMS UI display
* by the format defaults inferred from the browser as well as
* any user-specific locale preferences.
*
2016-11-29 00:31:16 +01:00
* @config
* @var string
*/
private static $time_format = 'H:mm';
/**
* 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.
*
* Also supports pluralisation of strings. Pass in a `count` argument, as well as a
* default value with `|` pipe-delimited options for each plural form.
*
* @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.
2020-12-21 22:23:23 +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
*/
public static function _t($entity, $arg = null)
2016-11-29 00:31:16 +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
}
// 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')) {
user_error("Missing default for localisation key $entity", E_USER_WARNING);
2016-11-29 00:31:16 +01:00
}
// Deprecate legacy injection format (`string %s, %d`)
// inject the variables from injectionArray (if present)
$sprintfArgs = [];
2022-04-14 03:12:59 +02:00
if ($default && !preg_match('/\{[\w\d]*\}/i', $default ?? '') && preg_match('/%[s,d]/', $default ?? '')) {
$sprintfArgs = array_values($injection ?? []);
$injection = [];
2016-11-29 00:31:16 +01:00
}
// If injection isn't associative, assume legacy injection format
$failUnlessSprintf = false;
2022-04-14 03:12:59 +02:00
if ($injection && array_values($injection ?? []) === $injection) {
$failUnlessSprintf = true; // Note: Will trigger either a deprecation error or exception below
2022-04-14 03:12:59 +02:00
$sprintfArgs = array_values($injection ?? []);
$injection = [];
2016-11-29 00:31:16 +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;
}
// Pass back to translation backend
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
// Sometimes default is omitted, so we don't know we have %s injection format until after translation
2022-04-14 03:12:59 +02:00
if (!$default && !preg_match('/\{[\w\d]*\}/i', $result ?? '') && preg_match('/%[s,d]/', $result ?? '')) {
Deprecation::notice('5.0', 'sprintf style localisation is deprecated');
if ($injection) {
2022-04-14 03:12:59 +02:00
$sprintfArgs = array_values($injection ?? []);
2016-11-29 00:31:16 +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
}
// @deprecated (see above)
if ($sprintfArgs) {
2022-04-14 03:12:59 +02:00
return vsprintf($result ?? '', $sprintfArgs ?? []);
2016-11-29 00:31:16 +01:00
}
return $result;
2016-11-29 00:31:16 +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.
*
* Note: Only splits in the default (en) locale as the string form contains limited metadata.
*
* @param string $string Input string
* @return array List of plural forms, or empty array if not plural
*/
public static function parse_plurals($string)
{
2022-04-14 03:12:59 +02:00
if (strstr($string ?? '', '|') && strstr($string ?? '', '{count}')) {
2017-02-22 04:14:53 +01:00
$keys = i18n::config()->uninherited('default_plurals');
2022-04-14 03:12:59 +02: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)
*
* @param array $plurals
* @return string Delimited string, or null if not plurals
2016-11-29 00:31:16 +01:00
*/
public static function encode_plurals($plurals)
2016-11-29 00:31:16 +01:00
{
// Validate against global plural list
2017-02-22 04:14:53 +01:00
$forms = i18n::config()->uninherited('plurals');
2022-04-14 03:12:59 +02: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
$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
$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)
{
2022-04-14 03:12:59 +02:00
return str_replace('_', '-', $locale ?? '');
2016-11-29 00:31:16 +01:00
}
/**
* 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;
}
}
/**
* 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-08-21 07:21:12 +02:00
if (!self::$current_locale) {
self::$current_locale = i18n::config()->uninherited('default_locale');
}
return self::$current_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/
* @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)
{
return static::getData()->scriptDirection($locale);
2016-11-29 00:31:16 +01:00
}
public static function get_template_global_variables()
{
return [
2016-11-29 00:31:16 +01:00
'i18nLocale' => 'get_locale',
'get_locale',
'i18nScriptDirection' => 'get_script_direction',
];
2016-11-29 00:31:16 +01:00
}
/**
* @return MessageProvider
*/
public static function getMessageProvider()
{
return Injector::inst()->get(MessageProvider::class);
}
/**
* 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);
}
}