This is a basic MidCOM Service which provides an interfaces to the various I18n facilities of MidCOM.

The I18n service serves as a central access point for all aspects around internationalization and localization. It provides auto-detection of language data using HTTP Content-Negotiation along with a cookie-based fallback.

A good deal of major languages are predefined, see the snippet /lib/midcom/config/language_db.inc for details.

This class is able to run independently from midcom_application due to the fact that it is used in the cache_hit code.

Use this class to set the language preferences (charset and locale) and to gain access to the l10n string databases. A few helpers which can be used to ease translation work (like charset conversion) are in here as well.

All language codes used here are ISO 639-1 two-letter codes.

package midcom.services

 Methods

__construct ()

This method initializes the available i18n framework by determining the desired language from these different sources: HTTP Content Negotiation, Client side language cookie.

It uses the MidCOM Language database now located at /lib/midcom/config/language-db.inc for any decisions. Its two parameters set the default language in case that none is supplied via HTTP Content Negotiation or through Cookies.

The default language set on startup is currently hardcoded to 'en', you should override it after initialization, if you want something else using the setter methods below.

The fallback language is read from the MidCOM configuration directive i18n_fallback_language.

code_to_id (string $code)

Convert the language code to its corresponding ID in Midgard database

Parameters

$code

stringTwo-letter code

Returns

intID field of the database

convert_from_utf8 (string $string)

This function will convert a string assumed to be in UTF-8 to the currently active charset.

Parameters

$string

stringThe string to convert

Returns

stringThe string converted to the current charset

convert_to_current_charset (string $string, string $charset)

Converts the given string to the current site charset.

The charset should be specified explicitly, as autodetection is very very error prone (though sometimes you don't have a choice).

Parameters

$string

stringThe string to convert.

$charset

stringThe charset in which string currently is, omit this parameter to use mb_detect_encoding (error prone!)

Returns

stringThe converted string.

convert_to_utf8 (string $string)

This function will convert a string assumed to be in the currently active charset to UTF8.

Parameters

$string

stringThe string to convert

Returns

stringThe string converted to UTF-8

get_content_language ()

Returns language code corresponding to current content language

Returns

string

get_current_charset ()

Returns the current character set

Returns

string

get_current_language ()

Returns the current language code

Returns

string

get_fallback_language ()

Returns the current fallback language code

Returns

string

get_l10n (string $component, string $database)

Returns a l10n class instance which can be used to access the localization data of the current component.

Using the special name "midcom" you will get the midcom core l10n library.

see \midcom_services_i18n_l10n

Parameters

$component

stringThe component for which to retrieve a string database.

$database

stringThe string table to retrieve from the component's locale directory.

Returns

\midcom_services_i18n_l10nThe cached L10n database; honor the reference for memory consumptions sake.

get_language_db ()

Returns the language database.

Returns

Array

get_language_hosts ()

get_string (string $stringid, string $component, string $database)

Returns a translated string using the l10n database specified in the function arguments.
see \midcom_services_i18n_l10n::get()

Parameters

$stringid

stringThe string to translate.

$component

stringThe component for which to retrieve a string database. If omitted, this defaults to the current component (out of the component context).

$database

stringThe string table to retrieve from the component's locale directory. If omitted, the 'default' database is used.

Returns

stringThe translated string

html_entity_decode (string $text)

Wrapped html_entity_decode call

Parameters

$text

stringThe text with HTML entities, which should be replaced by their native equivalents.

Returns

stringThe translated string.

iconv (string $source_charset, string $destination_charset, string $string)

This is a calling wrapper to the iconv library.

See the PHP iconv() function for the exact parameter definitions.

Parameters

$source_charset

stringThe charset to convert from.

$destination_charset

stringThe charset to convert to.

$string

stringThe string to convert.

Returns

mixedThe converted string or false on any error.

id_to_code (int $id)

Convert the ID to its corresponding language code in Midgard database

Parameters

$id

intID field of the database

Returns

StringTwo-letter code

list_languages ()

Lists languages as identifier -> name pairs

Returns

Array

set_charset (string $charset)

Set output character set.

Parameters

$charset

stringCharset name.

set_fallback_language (string $lang)

Set the 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::set_charset() after calling this method.

see \_synchronize_midgard_language()

Parameters

$lang

stringLanguage ISO 639-1 code

show_string (string $stringid, string $component, string $database)

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

..);.

To keep the naming stable with the actual l10n class, this is not called echo_string (Zend won't allow $l10n->echo().)

see \midcom_services_i18n_l10n::get()
see \get_string()

Parameters

$stringid

stringThe string to translate.

$component

stringThe component for which to retrieve a string database. If omitted, this defaults to the current component (out of the component context).

$database

stringThe string table to retrieve from the component's locale directory. If omitted, the 'default' database is used.

_get_q (array $params)

Parameters

$params

_load_l10n_db (string $component, string $database)

Load the specified l10n library.

If loading the library failed, midcom_error is thrown, otherwise the l10n db cache is populated accordingly.

Parameters

$component

stringThe component for which to retrieve a string database.

$database

stringThe string table to retrieve from the component's locale directory.

_load_language_db ()

Loads the language database.

_read_http_negotiation ()

This method pulls available language and content type data out of the HTTP Headers delivered by the browser and populates the member variables $_http_lang and $_http_content_type.

q-parameters for prioritization are supported.

_set_startup_langs ()

Scans the HTTP negotiation and the cookie data and tries to set a suitable default language.

Cookies have priority here.

 Properties

 

string $_current_charset

Current character set
 

string $_current_language

Current language.
 

string $_fallback_language

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

Array $_http_charset

Preferred charsets extracted out of the HTTP content negotiation.

Array keys are the charsets, the value is their q-index.

 

Array $_http_lang

Preferred languages extracted out of the HTTP content negotiation.

Array keys are the languages, the value is their q-index.

 

Array $_language_db

The language database, loaded from /lib/midcom/config/language_db.inc
 

array $_language_hosts

List of different language versions of the site

Format: An array indexed by language ID and containing midgard_host objects

 

Array $_obj_l10n

Cache of all instantiated localization classes.

They are delivered by reference to all clients.