Added PHRASES translation for javascript

added:
. PhrasesLib.js: translate method for retreiving phrasentexte in users language with JS
. PhrasesLib.php: methods to retrieve data from Phrase_model and return as JSON
. Phrase_model: method to perform query for categories AND phrases-array AND language
. FHC-Header.php:
 -- included js lib
 -- generate global FHC_JS_PHRASES_STORAGE_OBJECT

Changes of other files: loading libs, inits,...
This commit is contained in:
Cris
2018-05-29 14:13:00 +02:00
parent 11ca6fb0cf
commit 865eea2245
8 changed files with 244 additions and 10 deletions
+2 -2
View File
@@ -25,11 +25,11 @@ class FHC_Controller extends CI_Controller
* NOTE: The library is loaded with the alias 'p', so must me used with this alias in the rest of the code.
* EX: $this->p->t(<category>, <phrase name>)
*/
public function loadPhrases($categories, $language = null)
protected function loadPhrases($categories, $language = null)
{
$this->load->library('PhrasesLib', array($categories, $language), 'p');
}
/**
* Sets the unique id for the called controller
* NOTE: it is only working with HTTP GET request, not neeaded with POST
+36 -7
View File
@@ -259,7 +259,7 @@ class PhrasesLib
* - language: optional parameter must be a string. It's used to load phrases
*/
private function _extend_construct($params)
{
{
// Checks if the $params is an array with at least one element
if (is_array($params) && count($params) > 0)
{
@@ -287,15 +287,44 @@ class PhrasesLib
$language = $this->_ci->PersonModel->getLanguage(getAuthUID());
}
// Loads phrases
$phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language);
//checks if categories is associative or indexed array
if (ctype_digit(implode('', array_keys($categories))))
{
// Loads phrases
$phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language);
// If there are phrases loaded then store them in the property _phrases
if (hasData($phrases))
// If there are phrases loaded then store them in the property _phrases
if (hasData($phrases))
{
$this->_phrases = $phrases->retval;
}
}
else
{
$this->_phrases = $phrases->retval;
// Loads specific phrasentexte by category and phrases
$phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language);
// If there are phrases loaded then store them in the property _phrases
if (hasData($phrases))
{
$this->_phrases = $phrases->retval;
}
}
}
}
}
}
public function getJSON()
{
if (is_array($this->_phrases) && !is_null($this->_phrases))
{
return json_encode($this->_phrases);
}
else
{
//...CECK: better a return than console.log?
console.log('No phrases set to be returned to JS.');
}
}
}
@@ -77,6 +77,40 @@ class Phrase_model extends DB_Model
return $this->execQuery($query, array($categories, $language));
}
/**
*
*/
public function getPhrasesByCategoryAndPhrasesAndLanguage($phrasesParams, $language)
{
$qry = '
SELECT p.category, p.phrase, pt.orgeinheit_kurzbz, pt.orgform_kurzbz, pt.text
FROM system.tbl_phrase p
INNER JOIN system.tbl_phrasentext pt USING(phrase_id)
WHERE ';
foreach ($phrasesParams as $category => $phrases)
{
$qry .= '
(p.category = \'' . $category . '\'
AND phrase IN (';
foreach ($phrases as $phrase)
{
$qry .= '\'' . $phrase . '\', ';
}
$qry = rtrim($qry, ', ');
$qry .= ')) AND pt.sprache = \'' . $language . '\' OR ';
}
$qry = rtrim($qry, 'OR ');
return $this->execQuery($qry, array($phrasesParams, $language));
}
/**
* Loads all categories
@@ -5,12 +5,18 @@
'title' => 'Info Center',
'jquery' => true,
'jqueryui' => true,
'ajaxlib' => true,
'bootstrap' => true,
'fontawesome' => true,
'sbadmintemplate' => true,
'tablesorter' => true,
'ajaxlib' => true,
'filterwidget' => true,
'phrases'=> array(
'person' => array('vorname','nachname'),
'ui' => array('speichern'),
'global' => array('mailAnXversandt')
),
'navigationwidget' => true,
'customCSSs' => 'public/css/sbadmin2/tablesort_bootstrap.css',
'customJSs' => array('public/js/bootstrapper.js', 'public/js/infocenter/infocenterPersonDataset.js')
@@ -7,6 +7,8 @@
'bootstrap' => true,
'fontawesome' => true,
'jqueryui' => true,
'ajaxlib' => true,
'phraseslib' => true,
'tablesorter' => true,
'tinymce' => true,
'sbadmintemplate' => true,
+28 -1
View File
@@ -11,6 +11,7 @@ $calledMethod = $this->router->method;
$title = isset($title) ? $title : null;
$customCSSs = isset($customCSSs) ? $customCSSs : null;
$customJSs = isset($customJSs) ? $customJSs : null;
$phrases = isset($phrases) ? $phrases : null;
// By default set the parameters to false
$jquery = isset($jquery) ? $jquery : false;
@@ -84,6 +85,26 @@ function _generateJSDataStorageObject($calledPath, $calledMethod)
echo $toPrint;
}
/**
* Generates global JS-Object to pass phrases to other javascripts
*/
function _generateJSPhrasesStorageObject($phrases)
{
$ci =& get_instance();
$ci->load->library('PhrasesLib', array($phrases), 'pj');
$toPrint = "\n";
$toPrint .= '<script type="text/javascript">';
$toPrint .= '
var FHC_JS_PHRASES_STORAGE_OBJECT = '.$ci->pj->getJSON();
$toPrint .= "\n";
$toPrint .= '</script>';
$toPrint .= "\n\n";
echo $toPrint;
}
/**
* Generates tags for the javascripts you want to include, the parameter could by a string or an array of strings
*/
@@ -183,6 +204,9 @@ function _generateAddonsJSsInclude($calledFrom)
// Generates the global object to pass useful parameters to other javascripts
// NOTE: must be called before any other JS include
_generateJSDataStorageObject($calledPath, $calledMethod);
// Generates the global object to pass phrases to javascripts
_generateJSPhrasesStorageObject($phrases);
// JQuery V3
if ($jquery === true) _generateJSsInclude('vendor/components/jquery/jquery.min.js');
@@ -196,6 +220,9 @@ function _generateAddonsJSsInclude($calledFrom)
// AjaxLib JS
if ($ajaxlib === true) _generateJSsInclude('public/js/AjaxLib.js');
// // PhfrasesLib JS
if ($phrases != null) _generateJSsInclude('public/js/PhrasesLib.js');
// Bootstrap JS
if ($bootstrap === true) _generateJSsInclude('vendor/twbs/bootstrap/dist/js/bootstrap.min.js');
@@ -230,6 +257,6 @@ function _generateAddonsJSsInclude($calledFrom)
// Eventually required JS
_generateJSsInclude($customJSs);
?>
</head>
<!-- Header end -->
+115
View File
@@ -0,0 +1,115 @@
/**
* FH-Complete
*
* @package
* @author
* @copyright Copyright (c) 2016 fhcomplete.org
* @license GPLv3
* @link https://fhcomplete.org
* @since Version 1.0.0
*/
/**
* Definition and initialization of object FHC_PhraseLib
*/
var FHC_PhraseLib = {
//------------------------------------------------------------------------------------------------------------------
// Public methods
/**
Returns the phrase-text in the user's language
* @param {String} category : phrase-category
* @param {String} phrase : phrase-name
* @param {array} params : String-parameters to be set in variables in phrasentext
* @returns {String} : phrase-text
*/
t: function(category, phrase, params = [])
{
var category_found = false;
var phrase_found = false;
//check category and phrase first
if(typeof(category) == "undefined" || category === null
|| typeof(phrase) == "undefined" || phrase === null)
{
console.log('Category and/or phrase not found. \n\
1. Check params in PhrasesLib.js t-method\n\
2. Check params in FHC-Header.php phrases-array');
return;
}
//loop through global JS PHRASES STORAGE OBJECT and search for phrase
for(i in FHC_JS_PHRASES_STORAGE_OBJECT)
{
e = FHC_JS_PHRASES_STORAGE_OBJECT[i];
if (e.category === category)
{
category_found = true;
if (e.phrase === phrase)
{
phrase_found = true;
//replace if params are set
if ($.isArray(params) && typeof params !== 'undefined' && params !== null)
{
if (params.length !== 0)
{
e.text = replacePhraseVariable(e.text, params);
}
}
else
{
console.log('Could not replace variable. \n\
Replace-params should be an array.')
}
return e.text;
}
}
}
//show error messages for missing categories/phrases
if (!category_found)
{
console.log('Category not found. \n\
1. Check params in PhrasesLib.js t-method\n\
2. Check params in FHC-Header.php phrases-array');
}
if (category_found && !phrase_found)
{
console.log('Phrase not found. \n\
1. Check params in PhrasesLib.js t-method\n\
2. Check params in FHC-Header.php phrases-array');
}
}
}
//------------------------------------------------------------------------------------------------------------------
// Helper methods
/**
Returns phrase with variables being replaced
* @param {String} phrase : phrasen-text (with one ore more variables)
* @param {array} replaceStringArr : String-array to be set in variables in phrasentext (order matters)
* @returns {String} : replaced phrasen-text
*/
function replacePhraseVariable(phrase, replaceStringArr)
{
for (var i = 0; i < replaceStringArr.length; i++)
{
phrase = phrase.replace(/\{(.*?)\}/, replaceStringArr[i]);
}
return phrase;
}
//...TEST
console.log(FHC_PhraseLib.t('global', 'mailAnXversandt', new Array('c@yahoo.de', 'test')));
+21
View File
@@ -909,6 +909,27 @@ $phrases = array(
)
),
array(
'app' => 'core',
'category' => 'global',
'phrase' => 'mailAnXversandt',
'insertvon' => 'system',
'phrases' => array(
array(
'sprache' => 'German',
'text' => 'Mail an {email} versandt.',
'description' => '',
'insertvon' => 'system'
),
array(
'sprache' => 'English',
'text' => 'Mail was sent to {email}.',
'description' => '',
'insertvon' => 'system'
)
)
),
//******************************* CORE/ui
array(