- Added new constants LANG_SESSION_NAME and LANG_SESSION_INDEXES to constants.php

- Added new function getPhraseByLanguage to helpers/hlp_language_helper.php
This commit is contained in:
Paolo
2019-05-14 15:50:04 +02:00
parent 6cc99bf2b9
commit dcdfbbe0d7
3 changed files with 66 additions and 2 deletions
+8
View File
@@ -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
+57 -1
View File
@@ -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;
}
+1 -1
View File
@@ -1,7 +1,7 @@
<?php
class Sprache_model extends DB_Model
{
/**
* Constructor
*/