This is the L10n main interface class, used by the components.

It allows you to get entries from the l10n string tables in the current language with an automatic conversion to the destination character set.

L10n language database file format specification:

Lines starting with --- are considered command lines and treated specially, unless they occur within string data. All commands are separated with at least a single space from their content, unless they don't have an argument.

Empty lines are ignored, unless within string data.

All keys and values will be trim'ed when encountered, so leading and trailing whitespace will be eliminated completely.

Windows-style line endings (\r\n) will be silently converted to the UNIX \n style.

Commented example:

---# Lines starting with a # command are ignored.

---# File format version
---VERSION 2.1.0

---# Language of the table
---LANGUAGE en

---STRING string identifier
TRANSLATED STRING taken literally until ---STRINGEND, which is the
only reserved value at the beginning of the line, everything else is
fine. Linebreaks within the translation are preserved.
\r\n sequences are translated into to \n
---STRINGEND

File naming scheme: {$component_directory}/locale/{$database_name}.{$lang}.txt

package midcom.services

 Methods

__construct (string $library, string $database)

The constructor loads the translation library indicated by the snippetdir path $library and initializes the system completely.

The output character set will be initialized to the language's default.

Parameters

$library

stringName of the locale library to use.

$database

stringName of the database in the library to load.

delete (string $string, string $language)

Deletes a string from the database.

If the string is not present, it will fail silently.

Parameters

$string

stringThe string-ID to edit.

$language

stringThe language to edit.

flush ()

This will flush the complete string table to the filesystem.

No locking code is in place, so check that there are no concurrent accesses to the file have to be done on a social level.

It will write all loaded languages to disk, regardless of changes.

get (string $string, string $language)

Retrieves a localized string from the database using $language as destination.

If $language is unset, the currently set default language is used. If the string is not found in the selected language, the fallback is checked. If even the fallback cannot be found, then $string is returned and the event is logged to MidCOMs Debugging system.

L10n DB loads are done through string_exists.

Parameters

$string

stringThe string-ID to search for.

$language

stringThe language to search in, uses the current language as default.

Returns

stringThe translated string if available, the fallback string otherwise.

get_all_string_ids ()

Scans the current library and delivers all string ids that are in use.

Returns

ArrayA list of all string-IDs

get_language_name ($lang)

Parameters

$lang

get_languages ()

get_stringdb (string $language)

Helper function that returns the entire translation table for the given language

Parameters

$language

stringThe language to query

set_charset (string $charset)

Set output character set.

This is usually set through midcom_services_i18n.

see \midcom_services_i18n::set_charset()

Parameters

$charset

stringCharset name.

set_fallback_language (string $lang)

Set the fallback language.

This is usually set through midcom_services_i18n.

see \midcom_services_i18n::set_fallback_language()

Parameters

$lang

stringLanguage name.

set_language (string $lang)

Set output language.

This will set the character encoding to the language's default encoding and will also set the system locale to the one specified in the language database.

If you want another character encoding as the default one, you have to override it manually using midcom_services_i18n_l10n::set_charset() after calling this method.

This is usually set through midcom_services_i18n.

see \midcom_services_i18n::set_language()

Parameters

$lang

stringLanguage code.

show (string $string, string $language)

This is a shortcut for "echo $this->get(.

..);", useful in style code.

Note, that due to the stupidity of the Zend engine, it is not possible to call this function echo, like it should have been called.

see \get()

Parameters

$string

stringThe string-ID to search for.

$language

stringThe language to search in, uses the current language as default.

string_available (string $string)

Checks whether the given string is available in either the current or the fallback language.

Use this to determine if an actually processed result is returned by get. This is helpful especially if you want to "catch" cases where a string might translate to itself in some languages.

Parameters

$string

stringThe string-ID to search for

Returns

booleanIndicating availability.

string_exists (string $string, string $language)

Checks if a localized string for $string exists.

If $language is unset, the current language is used.

Parameters

$string

stringThe string-ID to search for.

$language

stringThe language to search in.

Returns

booleanIndicating availability.

update (string $string, string $language, string $translation)

Updates a string in the database.

If it does not exist, it will be created automatically.

Parameters

$string

stringThe string-ID to edit.

$language

stringThe language to edit.

$translation

stringThe UTF-8 encoded string to add/update.

_check_for_language (string $lang)

Checks, whether the referenced language is already loaded.

If not, it is automatically made available.

see \midcom_services_i18n_l10n::_load_language()

Parameters

$lang

stringThe language to check for.

_load_all_languages ()

This tries to load the language files for all languages defined in the i18n's language database.

_load_language ($lang)

Load a language database
  • Leading and trailing whitespace will be eliminated

Parameters

$lang

 Properties

 

string $_charset

Current character set
 

string $_component_name

The name of the current component
 

string $_fallback_language

Fallback language, in case the selected language is not available.
 

string $_language

Current language.
 

Array $_language_db

A copy of the language DB from i18n.
 

string $_library

The name of the locale library we use, this is usually a component's name.
 

string $_library_filename

The full path basename to the active library files.

The individual files are ending with .$lang.txt.

 

Array $_localedb

Global string table cache, it stores the string tables loaded during runtime.
 

Array $_stringdb

The string database, a reference into the global cache.
 

string $_version

The current L10n DB file format number