Class: I18n

I18n is a class that makes it easier to add multiple language support to PHP programs. It is lightweight and not very sophisticated, attempting to keep it straight-forward while emulating some of the more elegant features of other internationalization systems, such as gettext.

Translation files are PHP, so they benefit from language-level caching, and string replacement is simply an associative array lookup.

Properties

public $language

The language code, corresponding to the name of the language file.

public $locale

The locale code, corresponding to the name of the country to localize numbers and dates for.

public $directory = 'lang'

The location of the language files.

public $lang_hash = array ()

The language hash, a key/value list.

public $hash_order = array ()

The language fallback order.

public $charset = 'UTF-8'

The charset of the current language, which can be used to tell the browser, or any language-aware PHP functions, which to use.

public $fullname = ''

The full name of language in use (ie. 'English' for 'en').

public $default = ''

Default language, as read from the configuration in __construct().

public $day_date_format = 'dddd, mmmm d, yyyy'

The full date format to send to jQuery.localize.

public $short_day_date_format = 'ddd, mmm d'

The full date format to send to jQuery.localize.

public $date_format = 'mmmm d, yyyy'

The full date format to send to jQuery.localize.

public $short_format = 'mmm d'

The short date format to send to jQuery.localize.

public $time_format = 'h:MMa'

The time format to send to jQuery.localize.

public $year_format = 'yyyy'

The year format to send to jQuery.localize.

public $fallbacks = array ()

Contains fallback text replacements.

public $languages = array ()

2-D list of available languages, retrieved from getLanguages().

public $cookieName = 'lang'

The name of the language cookie for $negotiation=cookie.

public $url_includes_lang = false

Tells the controller that the first level of the URL is the language and not the page ID when $negotiation=url.

public $new_request_uri = ''

If $url_includes_lang is true, this will include the request with the language stripped out.

public $prefix = ''

If $url_includes_lang is true, this will include the stripped out part of the URL. Otherwise it will be empty. This is useful for including language URL prefixes in views via {{ i18n.prefix }}.

public $negotiation = 'url'

The negotiation method used to determine the current language.

public $error

If an error occurs during any portion of this class, this will contain the message.

Methods

public __construct ($directory = 'lang', $conf = array ())

Includes the appropriate language file in the provided $directory. This file is intended to fill out the $lang_hash array. $negotiationMethod determines the method whereby the language of choice is determined. See the negotiate() method for more info on this.

public setLocale ()

Calls setlocale() based on the current language.

public getIndex ()

Includes the language index.

public initApp ($app)

Includes the language index for an app.

public get ($original = '')

Takes a string, serializes it to generate a key, and performs a key/value lookup on the $lang_hash array. Returns the value found, or the original string if not found. This is the method used in I18n to return translated text.

public getf ()

Takes a string, serializes it to generate a key, and performs a key/value lookup on the $lang_hash array. Returns the value found, or the original string if not found. This method is similar to get(), except it uses vsprintf() to insert values. If you pass an array as the second value, it will use that instead of however many additional arguments you fed it. This is handy because if you already have all your values in an array, you can simply say getf($original, $array) instead of getf($original, $array[0], $array[1], $array[2]).

public getLanguages ()

Returns a 2-D array from the specified language file, which is an INI file. Each section name in the file corresponds to a different available language. Keys in each section include 'name', 'code', 'locale', 'charset', 'fallback', and 'default'.

public negotiate ($method = false)

Returns the preferred language of the current visitor. If the $method is 'http' then it uses the HTTP Accept-Language string for this info. If the $method is 'cookie' it uses a cookie (specified by the $cookieName property) to determine, if the $method is 'subdomain' then it looks for it in the subdomain of the site (e.g., en.sitename.com, fr.sitename.com), and if the $method is 'url' then it uses the start of the URL to determine the language (e.g., /fr/ or /en/). Default is 'url'.

public static export ($strings)

Export an array of strings into a JavaScript block that calls $.i18n_add() which can be passed to $page->add_script(). Can also be called from a view template on an array of strings like this: {{ my_array|I18n::export }}.

Note that you may pass either a single array, or each string as a separate parameter, which allows you to omit the array() wrapper when calling it in PHP code.

private static _date ($date, $class, $format)

Applies the specified class and format to a date for the date(), short_date(), time(), date_time() and short_date_time() methods.

public static date ($date)

Filter for outputting dates. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::date }}

public static short_date ($date)

Filter for outputting a shortened date. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_date }}

public static short_date_year ($date)

Filter for outputting a shortened date with year included. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_date_year }}

public static day_date ($date)

Filter for outputting a shortened day and date. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::day_date }}

public static short_day_date ($date)

Filter for outputting a shortened day and date. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_day_date }}

public static time ($date)

Filter for outputting times. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::time }}

public static date_time ($date)

Filter for outputting date and time. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::date_time }}

public static day_date_time ($date)

Filter for outputting a shortened date and time. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::day_date_time }}

public static short_date_time ($date)

Filter for outputting a shortened date and time. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_date_time }}

public static short_date_year_time ($date)

Filter for outputting a shortened date and time with year. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_date_year_time }}

public static short_day_date_time ($date)

Filter for outputting a shortened date and time. Used with the jQuery localize plugin to convert dates into the current user's time zone.

Usage:

{{ date_value|I18n::short_day_date_time }}