Class i18n
Base-class for storage and retrieval of translated entities.
Please see the Translatable extension for managing translations of
database-content.
Usage
PHP:
_t('MyNamespace.MYENTITY', 'My default natural language value'); _t('MyNamespace.MYENTITY', 'My default natural language value', PR_MEDIUM, 'My explanatory context'); sprintf(_t('MyNamespace.MYENTITY', 'Counting %s things'), 42);
Templates:
<% _t('MyNamespace.MYENTITY', 'My default natural language value') %> <% sprintf(_t('MyNamespace.MYENTITY','Counting %s things'),$ThingsCount) %>
Javascript (see sapphire/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 Translatable for
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.
- Object
-
i18n
Author: Bernat Foj Capell <bernat@silverstripe.com>
See: http://doc.silverstripe.org/i18n
See: http://www.w3.org/TR/i18n-html-tech-lang
Used by:
i18nTextCollector Located at sapphire/core/i18n.php
Methods summary
public static
|
#
set_js_i18n( boolean $bool )
Use javascript i18n through the ss.i18n class (enabled by default). If set to TRUE, includes javascript requirements for the base library (sapphire/javascript/i18n.js) and all necessary lang files (e.g. sapphire/lang/de_DE.js) plus fallbacks to the default locale (e.g. sapphire/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. |
public static
boolean
|
|
public static
|
|
public static
string
|
|
public static
locale
|
|
public static
|
|
public static
string
|
|
public static
string
|
|
public static
list
|
|
public static
list
|
|
public static
list
|
|
public static
array
|
#
get_existing_translations( )
Searches the root-directory for module-directories (identified by having a _config.php on their first directory-level). Returns all found locales. |
public static
Name
|
#
get_language_name( mixed $code, boolean $native = false )
Get a name from a language code (two characters, e.g. "en"). |
public static
Name
|
|
public static
Language
|
|
public static
Language
|
|
public 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). |
public static
string
|
#
get_lang_from_locale( string $locale )
Returns the "short" language name from a locale, e.g. "en_US" would return
"en". This conversion is determined internally by the |
public 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. |
public static
string
|
|
public static
string
|
#
get_owner_module( string $name )
Given a file name (a php class name, without the .php ext, or a template name, including the .ss extension) this helper function determines the module where this file is located |
public static
boolean
|
#
validate_locale( mixed $locale )
Validates a "long" locale format (e.g. "en_US") by checking it against |
public static
|
#
set_locale( string $locale )
|
public static
string
|
|
public static
|
|
public static
String
|
|
public static
String
|
#
default_locale( )
This is the "fallback locale", in case resources with the "current locale"
(set through |
public static
|
|
public static
|
|
public static
|
|
public static
|
#
include_locale_file( string $module, string $locale )
Include a locale file determined by module name and locale |
public static
|
#
include_by_locale( string $locale, boolean $load_plugins = true, boolean $force_load = false )
Includes all available language files for a certain defined locale |
public 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) |
public
|
|
public static
|
#
register_plugin( mixed $name, mixed $callback, integer $priority = 10 )
Register a named translation plug-in function. Plug-ins are assumed to be registered before any call to _t. If registered after a call to _t for a given local, it will not be called. |
public static
|
|
public static
|
#
plugins_load( mixed $locale )
Load any translations from registered plugins. Merges them directly into $lang. |
public static
|
#
merge_locale_data( mixed $locale, mixed $extra )
Merge an extra of language translations into $lang[$locale]. We'd use array_merge_recursive, except it doesn't work for translations that specify priorities and comments, because they are indexed by number. |
Methods inherited from Object
__call(),
__construct(),
__toString(),
__wakeup(),
addMethodsFrom(),
addStaticVars(),
addWrapperMethod(),
add_extension(),
add_static_var(),
allMethodNames(),
cacheToFile(),
cacheToFileWithArgs(),
clearCache(),
combined_static(),
create(),
createMethod(),
create_from_string(),
defineMethods(),
exists(),
extInstance(),
extend(),
getCustomClass(),
getExtensionInstance(),
getExtensionInstances(),
get_extensions(),
get_static(),
hasExtension(),
hasMethod(),
has_extension(),
invokeWithExtensions(),
is_a(),
loadCache(),
parentClass(),
parse_class_spec(),
remove_extension(),
sanitiseCachename(),
saveCache(),
set_stat(),
set_static(),
set_uninherited(),
stat(),
strong_create(),
uninherited(),
uninherited_static(),
useCustomClass()
Magic methods summary
Properties summary
protected static
string
|
$current_locale |
#
This static variable is used to store the current defined locale. |
protected static
string
|
$default_locale | |
protected static
boolean
|
$js_i18n | |
protected static
string
|
$date_format | |
protected static
string
|
$time_format | |
public static
array
|
$all_locales |
#
An exhaustive list of possible locales (code => language and country) |
public static
array
|
$common_languages |
#
$common_languages A list of commonly used languages, in the form langcode => array( EnglishName, NativeName) |
public static
array
|
$common_locales |
#
$common_locales Sorted alphabtically by the common language name, not the locale key. |
public static
array
|
$tinymce_lang | |
public static
array
|
$likely_subtags |
#
$likely_subtags 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". |