i18n
class i18n extends SS_Object implements TemplateGlobalProvider, Flushable
Base-class for storage and retrieval of translated entities.
Please see the 'translatable' module for managing translations of database-content.
Usage
PHP:
_t('MyNamespace.MYENTITY', 'My default natural language value');
_t('MyNamespace.MYENTITY', 'My default natural language value', 'My explanatory context');
sprintf(_t('MyNamespace.MYENTITY', 'Counting %s things'), 42);
Templates:
<%t MyNamespace.MYENTITY 'My default natural language value' %>
<%t MyNamespace.MYENTITY 'Counting %s things' s=$ThingsCount %>
<%t MyNamespace.MYENTITY 'Counting {count} things' count=$ThingsCount %>
Javascript (see framework/javascript/i18n.js):
ss.i18n._t('MyEntity.MyNamespace','My default natural language value');
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.
Text Collection
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']).
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.
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.
Locales
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.
Config options
default_locale | string | ||
js_i18n | boolean | ||
date_format | string | ||
time_format | string | ||
all_locales | array | An exhaustive list of possible locales (code => language and country) | |
common_languages | array | ||
common_locales | array | ||
tinymce_lang | array | ||
likely_subtags | array |
Properties
string | $class | from SS_Object |
Methods
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
Create an object from a string representation. It treats it as a PHP constructor without the 'new' keyword. It also manages to construct the object without the use of eval().
Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().
Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to TRUE when using {@link Object::useCustomClass()}
This class allows you to overload classes with other classes when they are constructed using the factory method {@link Object::create()}
If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name
Get the value of a static property of a class, even in that property is declared protected (but not private), without any inheritance, merging or parent lookup if it doesn't exist on the given class.
Return TRUE if a class has a specified extension.
Add an extension to a specific class.
No description
Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located
Return the names of all the methods available on this object
Check if this class is an instance of a specific class, or has that class as one of its parents
Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array
Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed
Get an extension instance attached to this object by name.
Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.
Get all extension instances for this specific object instance.
Cache the results of an instance method in this object to a file, or if it is already cache return the cached results
Clears the cache for the given cacheToFile call
Triggered early in the request when someone requests a flush.
Return an instance of the cache used for i18n data.
Use javascript i18n through the ss.i18n class (enabled by default).
This is the main translator function. Returns the string defined by $class and $entity according to the currently set locale.
No description
No description
No description
No description
Get a list of commonly used languages
Get a list of commonly used locales
Matches a given locale with the closest translation available in the system
Searches the root-directory for module-directories (identified by having a _config.php on their first directory-level).
Get a name from a language code (two characters, e.g. "en").
Get a name from a locale code (xx_YY).
Get a code from an English language name
Get the current tinyMCE language
Searches the root-directory for module-directories (identified by having a _config.php on their first directory-level and a language-file with the default locale in the /lang-subdirectory).
Returns the "short" language name from a locale, e.g. "en_US" would return "en".
Provides you "likely locales" for a given "short" language code. This is a guess, as we can't disambiguate from e.g. "en" to "en_US" - it could also mean "en_UK". Based on the Unicode CLDR project.
Gets a RFC 1766 compatible language code, e.g. "en-US".
Given a PHP class name, finds the module where it's located.
Validates a "long" locale format (e.g. "en_US") by checking it against {@link $all_locales}.
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.
Get the current locale.
This is the "fallback locale", in case resources with the "current locale" (set through {@link set_locale()}) can't be found.
Returns the script direction in format compatible with the HTML "dir" attribute.
Includes all available language files for a certain defined locale.
Given a class name (a "locale namespace"), will search for its module and, if available, will load the resources for the currently defined locale.
Called by SSViewer to get a list of global variables to expose to the template, the static method to call on this class to get the value for those variables, and the class to use for casting the returned value for use in a template
Details
in SS_Object at line 60
static Config_ForClass|null
config()
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .
....).
in SS_Object at line 132
static SS_Object
create()
An implementation of the factory method, allows you to create an instance of a class
This method first for strong class overloads (singletons & DB interaction), then custom class overloads. If an overload is found, an instance of this is returned rather than the original class. To overload a class, use {@link Object::useCustomClass()}
This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create('SiteTree'); $list = SiteTree::get();
in SS_Object at line 155
static SS_Object
singleton()
Creates a class instance by the "singleton" design pattern.
It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).
in SS_Object at line 190
static
create_from_string($classSpec, $firstArg = null)
Create an object from a string representation. It treats it as a PHP constructor without the 'new' keyword. It also manages to construct the object without the use of eval().
Construction itself is done with Object::create(), so that Object::useCustomClass() calls are respected.
Object::create_from_string("Versioned('Stage','Live')")
will return the result of
Versioned::create('Stage', 'Live);
It is designed for simple, clonable objects. The first time this method is called for a given string it is cached, and clones of that object are returned.
If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's impossible to pass null as the firstArg argument.
Object::create_from_string("Varchar(50)", "MyField")
will return the result of
Vachar::create('MyField', '50');
Arguments are always strings, although this is a quirk of the current implementation rather than something that can be relied upon.
in SS_Object at line 215
static
parse_class_spec($classSpec)
Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().
Returns a 2-elemnent array, with classname and arguments
in SS_Object at line 341
static SS_Object
strong_create()
Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to TRUE when using {@link Object::useCustomClass()}
in SS_Object at line 361
static
useCustomClass(string $oldClass, string $newClass, bool $strong = false)
This class allows you to overload classes with other classes when they are constructed using the factory method {@link Object::create()}
in SS_Object at line 375
static string
getCustomClass(string $class)
If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name
in SS_Object at line 396
static any
static_lookup($class, $name, null $default = null)
Get the value of a static property of a class, even in that property is declared protected (but not private), without any inheritance, merging or parent lookup if it doesn't exist on the given class.
in SS_Object at line 436
static
get_static($class, $name, $uncached = false)
deprecated
deprecated
in SS_Object at line 444
static
set_static($class, $name, $value)
deprecated
deprecated
in SS_Object at line 452
static
uninherited_static($class, $name, $uncached = false)
deprecated
deprecated
in SS_Object at line 460
static
combined_static($class, $name, $ceiling = false)
deprecated
deprecated
in SS_Object at line 470
static
addStaticVars($class, $properties, $replace = false)
deprecated
deprecated
in SS_Object at line 478
static
add_static_var($class, $name, $value, $replace = false)
deprecated
deprecated
in SS_Object at line 494
static
has_extension(string $classOrExtension, string $requiredExtension = null, boolean $strict = false)
Return TRUE if a class has a specified extension.
This supports backwards-compatible format (static Object::has_extension($requiredExtension)) and new format ($object->has_extension($class, $requiredExtension))
in SS_Object at line 536
static
add_extension(string $classOrExtension, string $extension = null)
Add an extension to a specific class.
The preferred method for adding extensions is through YAML config, since it avoids autoloading the class, and is easier to override in more specific configurations.
As an alternative, extensions can be added to a specific class directly in the {@link Object::$extensions} array. See {@link SiteTree::$extensions} for examples. Keep in mind that the extension will only be applied to new instances, not existing ones (including all instances created through {@link singleton()}).
in SS_Object at line 594
static
remove_extension(string $extension)
Remove an extension from a class.
Keep in mind that this won't revert any datamodel additions of the extension at runtime, unless its used before the schema building kicks in (in your _config.php). Doesn't remove the extension from any {@link Object} instances which are already created, but will have an effect on new extensions. Clears any previously created singletons through {@link singleton()} to avoid side-effects from stale extension information.
in SS_Object at line 633
static array
get_extensions(string $class, bool $includeArgumentString = false)
in SS_Object at line 655
static
get_extra_config_sources($class = null)
in SS_Object at line 694
__construct()
in SS_Object at line 725
mixed
__call(string $method, array $arguments)
Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located
You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or {@link Object::addWrapperMethod()}
in SS_Object at line 792
bool
hasMethod(string $method)
Return TRUE if a method exists on this object
This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via extensions
in SS_Object at line 802
array
allMethodNames(bool $custom = false)
Return the names of all the methods available on this object
in SS_Object at line 963
stat($name, $uncached = false)
in SS_Object at line 970
set_stat($name, $value)
in SS_Object at line 977
uninherited($name)
in SS_Object at line 991
bool
exists()
Return true if this object "exists" i.e. has a sensible value
This method should be overriden in subclasses to provide more context about the classes state. For example, a {@link DataObject} class could return false when it is deleted from the database
in SS_Object at line 998
string
parentClass()
in SS_Object at line 1008
bool
is_a(string $class)
Check if this class is an instance of a specific class, or has that class as one of its parents
in SS_Object at line 1015
string
__toString()
in SS_Object at line 1030
mixed
invokeWithExtensions(string $method, mixed $argument = null)
Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge all results into an array
in SS_Object at line 1058
array
extend(string $method, mixed $a1 = null, mixed $a2 = null, mixed $a3 = null, mixed $a4 = null, mixed $a5 = null, mixed $a6 = null, mixed $a7 = null)
Run the given function on all of this object's extensions. Note that this method originally returned void, so if you wanted to return results, you're hosed
Currently returns an array, with an index resulting every time the function is called. Only adds returns if they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't do type checking, an included NULL return would fail the permission checks.
The extension methods are defined during {@link __construct()} in {@link defineMethods()}.
in SS_Object at line 1097
Extension
getExtensionInstance(string $extension)
Get an extension instance attached to this object by name.
in SS_Object at line 1115
bool
hasExtension(string $extension)
Returns TRUE if this object instance has a specific extension applied in {@link $extension_instances}. Extension instances are initialized at constructor time, meaning if you use {@link add_extension()} afterwards, the added extension will just be added to new instances of the extended class. Use the static method {@link has_extension()} to check if a class (not an instance) has a specific extension.
Caution: Don't use singleton(
in SS_Object at line 1126
array
getExtensionInstances()
Get all extension instances for this specific object instance.
See {@link get_extensions()} to get all applied extension classes for this class (not the instance).
in SS_Object at line 1142
mixed
cacheToFile(string $method, int $lifetime = 3600, string $ID = false, array $arguments = array())
Cache the results of an instance method in this object to a file, or if it is already cache return the cached results
in SS_Object at line 1171
clearCache($method, $ID = false, $arguments = array())
Clears the cache for the given cacheToFile call
at line 104
static
flush()
Triggered early in the request when someone requests a flush.
at line 123
static Zend_Cache_Core
get_cache()
Return an instance of the cache used for i18n data.
at line 152
static
set_js_i18n(bool $bool)
deprecated
deprecated 4.0 Use the "i18n.js_i18n" config setting instead
Use javascript i18n through the ss.i18n class (enabled by default).
If set to TRUE, includes javascript requirements for the base library (framework/javascript/i18n.js) and all necessary lang files (e.g. framework/lang/de_DE.js) plus fallbacks to the default locale (e.g. framework/lang/en_US.js). If set to FALSE, only includes a stub implementation which is necessary. Mainly disabled to save bandwidth in a frontend context when website is in single language.
Caution: This flag gets overwritten in {@link LeftAndMain::init()} to enforce javascript i18n for the CMS interfaces.
at line 161
static bool
get_js_i18n()
deprecated
deprecated 4.0 Use the "i18n.js_i18n" config setting instead
at line 170
static
set_date_format(string $format)
deprecated
deprecated 4.0 Use the "i18n.date_format" config setting instead
at line 179
static string
get_date_format()
deprecated
deprecated since version 4.0
at line 188
static
set_time_format(string $format)
deprecated
deprecated 4.0 Use the "i18n.time_format" config setting instead
at line 197
static string
get_time_format()
deprecated
deprecated since version 4.0
at line 2045
static string
_t(string $entity, string $string = "", string $context = "", array $injection = array())
This is the main translator function. Returns the string defined by $class and $entity according to the currently set locale.
at line 2152
static array
get_translators()
at line 2178
static Zend_Translate
get_translator(String $name)
at line 2190
static
register_translator(Zend_Translate $translator, String $name, Int $priority = 10)
at line 2210
static
unregister_translator(String $name)
at line 2222
static list
get_common_languages(boolean $native = false)
Get a list of commonly used languages
at line 2236
static list
get_common_locales(boolean $native = false)
Get a list of commonly used locales
at line 2250
static list
get_locale_list()
deprecated
deprecated since version 4.0
Get a list of locales (code => language and country)
at line 2261
static string
get_closest_translation(string $locale)
Matches a given locale with the closest translation available in the system
at line 2280
static array
get_existing_translations()
Searches the root-directory for module-directories (identified by having a _config.php on their first directory-level).
Finds locales by filename convention ("
at line 2322
static Name
get_language_name(mixed $code, boolean $native = false)
Get a name from a language code (two characters, e.g. "en").
at line 2339
static Name
get_locale_name(mixed $code)
Get a name from a locale code (xx_YY).
at line 2350
static Language
get_language_code(mixed $name)
Get a code from an English language name
at line 2360
static Language
get_tinymce_lang()
Get the current tinyMCE language
at line 2376
static array
get_translatable_modules()
Searches the root-directory for module-directories (identified by having a _config.php on their first directory-level and a language-file with the default locale in the /lang-subdirectory).
at line 2402
static string
get_lang_from_locale(string $locale)
Returns the "short" language name from a locale, e.g. "en_US" would return "en".
at line 2417
static string
get_locale_from_lang(string $lang)
Provides you "likely locales" for a given "short" language code. This is a guess, as we can't disambiguate from e.g. "en" to "en_US" - it could also mean "en_UK". Based on the Unicode CLDR project.
at line 2438
static string
convert_rfc1766(string $locale)
Gets a RFC 1766 compatible language code, e.g. "en-US".
at line 2448
static string
get_owner_module(string $name)
Given a PHP class name, finds the module where it's located.
at line 2477
static boolean
validate_locale($locale)
Validates a "long" locale format (e.g. "en_US") by checking it against {@link $all_locales}.
To add a locale to {@link $all_locales}, use the following example
in your mysite/_config.php:
i18n::$allowed_locales['xx_XX'] = '
Note: Does not check for {@link $allowed_locales}.
at line 2494
static
set_locale(string $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.
at line 2504
static string
get_locale()
Get the current locale.
Used by {@link Member::populateDefaults()}
at line 2521
static String
default_locale()
deprecated
deprecated since version 4.0; Use the "i18n.default_locale" config setting instead
This is the "fallback locale", in case resources with the "current locale" (set through {@link set_locale()}) can't be found.
If you just want to globally read/write a different locale (e.g. in a CMS interface), please use {@link get_locale()} and {@link set_locale()} instead.
For example, {@link Requirements::add_i18n_javascript()} and {@link i18n::include_by_class()} use this "fallback locale" value to include fallback language files.
at line 2532
static
set_default_locale(String $locale)
deprecated
deprecated since version 4.0; Use the "i18n.default_locale" config setting instead
See {@link default_locale()} for usage.
at line 2544
static String
get_script_direction(String $locale = null)
Returns the script direction in format compatible with the HTML "dir" attribute.
at line 2562
static
include_by_locale(string $locale, Boolean $clean = false)
Includes all available language files for a certain defined locale.
at line 2652
static
include_by_class(string $class)
Given a class name (a "locale namespace"), will search for its module and, if available, will load the resources for the currently defined locale.
If not available, the original English resource will be loaded instead (to avoid blanks)
at line 2670
static array
get_template_global_variables()
Called by SSViewer to get a list of global variables to expose to the template, the static method to call on this class to get the value for those variables, and the class to use for casting the returned value for use in a template
If the method to call is not included for a particular template variable, a method named the same as the template variable will be called
If the casting class is not specified for a particular template variable, ViewableData::$default_cast is used
The first letter of the template variable is case-insensitive. However the method name is always case sensitive.