Back to the module list

Language selector

The user can define the interface language of a web application.

This module allow to choose the language to display by using the user prefs proposed by its browser and the application's translations available.

The selected choice can also be stored in a cookie to overwrite the automatic process

// Language
$language = new Domframework\Language ();
// The application name is used to choose the
// locale/fr_FR.UTF8/LC_MESSAGES/application.po file.
// The second parameter is the cookie name.
$lang = $language->activeLanguage ("application", "application-Language");
// Define the default value if none can be automatically detected from the
// user preferences.
if ($lang === "C") $lang = "fr_FR";

The class definition

Class Domframework\Language

Namespace Domframework

Description

/**
 Language class : change the messages

Properties

public $cacheDir="data/locale";
/**
 Language cache directory

Methods

public function activeLanguage ( $package, $languageCookie, $forcedLanguage=null)
/**
 The complete stack of language selection
 @param string $package The package name (package.(po|mo) files)
 @param string $languageCookie The name of the cookie saved in the browser
 @param string $forcedLanguage The name of a forced language
 @return string The language with format fr_FR to be used

public function languageActivation ( $moFile, $languageCode, $category=5, $repLocale="./locale")
/**
 Start the Gettext support with the cached file .mo provided as parameter
 @param string $moFile The .mo file
 @param string $languageCode The language code to use
 @param integer $category the LC_ type to use
 @param string|null $repLocale The locale directory. Use "./locale" if not
 provided

public function languageCache ( $languageCode, $package="messages", $category=5, $repLocale="./locale")
/**
 This function manage the cache of $package.mo files as Apache cache them
 It return the directory to use
 @param string $languageCode Language with format "fr_FR"
 @param string|null $package The package name of the soft ($package.mo
                             file). "messages" by default
 @param string|null $category The folder name LC_MESSAGES by default
 @param string|null $repLocale The folder where all the locales are stored

public function languageCookie ( $cookieName, $repLocale="./locale")
/**
 Return the language recorded in the Cookie. Check if this language is
 @param string $cookieName The cookie name
 @param string|null $repLocale The directory use to store the locale files
 allowed
 @return string The language allowed or FALSE

public function languageCookieSet ( $cookieName, $languageCode, $sitepath)
/**
 Set the cookie with a TTL of one month
 @param string $cookieName The name of the cookie
 @param string $languageCode Language to store
 @param string $sitepath The site path

public function languageName ( $languageCode)
/**
 Return the language name from the language
 Ex. : $languageCode=fr_FR, return France
 @param string $languageCode Language with format "fr_FR"
 @return string Then language name

public function languageSelection ( $repLocale="./locale", $languageCode="fr_FR")
/**
 Choose the best language in the browser list and which is available in
 locale path
 @param string|null $repLocale Directory where are stored the translations
 @param string|null $languageCode The coding langugage of the soft
 @return string The choosed locale whithout charset (like fr_FR)

public function languageSubTag ( $languageCode)
/**
 Return the language subtag for the language
 http://www.iana.org/assignments/language-subtag-registry/
 language-subtag-registry
 @param string $languageCode The language code to convert

public function languageTraductionsList ( $repLocale="./locale")
/**
 Return an array with all the languages available in the $repLocale dir
 The languages are in the format 'en_US' without the codeset.
 @param string|null $repLocale The directory use to store the locale files
 Return FALSE if there is an error