diff --git a/application/config/constants.php b/application/config/constants.php index 77f28b830..ac2ecc649 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -58,6 +58,14 @@ define('AUTH_INVALID_CREDENTIALS', 2); define('LDAP_NO_USER_DN', 10); define('LDAP_TOO_MANY_USER_DN', 11); +/* +|-------------------------------------------------------------------------- +| Language constants +|-------------------------------------------------------------------------- +*/ +define('LANG_SESSION_NAME', 'LANGUAGE'); +define('LANG_SESSION_INDEXES', 'LANGUAGE_INDEXES'); + /* |-------------------------------------------------------------------------- | File and directory modes diff --git a/application/helpers/hlp_language_helper.php b/application/helpers/hlp_language_helper.php index d80175fa7..4dfb53c7b 100644 --- a/application/helpers/hlp_language_helper.php +++ b/application/helpers/hlp_language_helper.php @@ -13,7 +13,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); // ------------------------------------------------------------------------ -// Functions needed to manage the user language preference +// Functions needed to manage multi lingual contents // ------------------------------------------------------------------------ /** @@ -44,3 +44,59 @@ function getUserLanguage($language = null) return $language; } + +/** + * Function to retrieve a phrase from an array of phrases (same phrase in more languages) using the given language as parameter + * The given $phraseLanguagesArray parameter contains more translations of the same phrase + * $language parameter contains the language used to get the phrase + * The first time this function is called an array, that has the language as its key and the language index as its value, + * is retrived from database and then stored in the user session. + * All subsequent calls retrieves this array from the session itself. + */ +function getPhraseByLanguage($phraseLanguagesArray, $language) +{ + $phrase = null; + $ci =& get_instance(); // get CI instance + + // Try to get the language session + $langArray = getSessionElement(LANG_SESSION_NAME, LANG_SESSION_INDEXES); + if ($langArray == null) // If not already loaded in session + { + // Loads the Sprache_model to retrieve the language settings from the DB + // NOTE: Stores the loaded model with the alias SpracheModelLanguage to avoid to overwrite + // an already loaded SpracheModel used somewhere else + $ci->load->model('system/Sprache_model', 'SpracheModelLanguage'); + + // Add order clause by index and select only the sprache column + $ci->SpracheModelLanguage->addOrder('index'); + $ci->SpracheModelLanguage->addSelect('sprache'); + + // Retrieves from public.tbl_sprache + $dbLanguages = $ci->SpracheModelLanguage->load(); + if (hasData($dbLanguages)) // If everything is ok and contains data + { + $index = 0; // Incremental integer + $languageIndexes = array(); // Array that will contains languages and their indexes + + // Loops through database results + foreach (getData($dbLanguages) as $dbLanguage) + { + $languageIndexes[$dbLanguage->sprache] = $index++; // set $languageIndexes array elements + } + } + + $langArray = $languageIndexes; // copy $languageIndexes to $langArray + // Set session element $_SESSION['LANG']['LANG_INDEXES'] with $languageIndexes + setSessionElement(LANG_SESSION_NAME, LANG_SESSION_INDEXES, $langArray); + } + + // Checks to avoid ugly php error messages + if (!isEmptyArray($phraseLanguagesArray) && !isEmptyArray($langArray) + && isset($langArray[$language]) && isset($phraseLanguagesArray[$langArray[$language]])) + { + // If everything is ok then set phrase + $phrase = $phraseLanguagesArray[$langArray[$language]]; + } + + return $phrase; +} diff --git a/application/models/system/Sprache_model.php b/application/models/system/Sprache_model.php index 30736f0c8..202157a83 100644 --- a/application/models/system/Sprache_model.php +++ b/application/models/system/Sprache_model.php @@ -1,7 +1,7 @@