Language handling
Sitemagic CMS has build in support for language files, allowing the developer to create extensions targeted for many different nationalities. A language file is an XML file in its simplest form.
English language support (Languages/en.xml)
<?xml version="1.0" encoding="ISO-8859-1"?> <entries> <entry key="Name" value="First and last name" /> <entry key="Age" value="Your age" /> <entry key="Save" value="Save data" /> </entries> |
Danish language support (Languages/da.xml)
<?xml version="1.0" encoding="ISO-8859-1"?> <entries> <entry key="Name" value="Fornavn og efternavn" /> <entry key="Age" value="Din alder" /> <entry key="Save" value="Gem data" /> </entries> |
The two files above represents each their own language. Tree identical entries are defined with the same keys, but with different values (the translations). All the developer has to do, is read the language file, and use the key to insert the given sentence or word.
$language = new SMLanguageHandler("MyExtension");
$labelName = $language->GetTranslation("Name"); $labelAge = $language->GetTranslation("Age"); $labelSave = $language->GetTranslation("Save"); |
The language files must be stored in a folder called Languages under the given extension. Each language has its own file with the name xx.xml, where xx is the language code. The en.xml file should always be defined, since the language handler will default to english, if the prefered language is not found.
The extension will now be displayed in the language specified in config.xml.php, or in the language specified when logging in to the administration section, which overrides the setting in config.xml.php (for the given session only).
SMLanguageHandler class
| Function |
Return type |
Description |
| __construct($extension:string) |
|
Create instance of the language handler for the specified extension. |
| __construct($extension:string, $langCode:string) |
|
See description for __construct($extension:string). Force the language handler to use the language specified as the second argument. |
| GetTranslation($key:string) |
String |
Get translation for the given key. |
| ::OverrideSystemLanguage($langCode:string) |
|
Change language for the given session (will not affect other users). |
| ::GetSystemLanauge() |
String |
Get language currently being used. |
| ::RestoreSystemLanguage() |
|
Change language back to default language specified in config.xml.php. |
| ::GetDefaultSystemLanguage() |
String |
Get the language code specified in config.xml.php |
| ::GetLanguages() |
String[]
|
Get available languages specified in config.xml.php |