Page 1 of 1

Optimising nuTranslate() in js

Posted: Sat Dec 12, 2020 5:03 pm
by apmuthu
The current code for translation is in nucommon.js at lines 644 to 656 :

Code: Select all

function nuTranslate(s){
	
	for(var i = 0 ; i < nuLANGUAGE.length ; i ++){
		
		if(nuLANGUAGE[i].english == s){
			return nuLANGUAGE[i].translation;
		}
		
	}
	
	return s;
	
}
This can be improved by avoiding the for loop and directly getting the translation using the likes of nuLANGUAGE.find(s) or similar construct. Alternatively store it in an array where the english string is the index - it can then be a mere lookup.

Also refer:
https://attacomsian.com/blog/javascript-array-search

Re: Optimising nuTranslate() in js

Posted: Sat Dec 12, 2020 5:26 pm
by apmuthu
The translation function for PHP code is in nucommon.php at lines 1560 to 1576:

Code: Select all

function nuTranslate($e){

        $l      = nuUserLanguage();
        $s      = "
                        SELECT *
                        FROM zzzzsys_translate
                        WHERE trl_language = ?
                        AND trl_english = ?

                ";

        $t      = nuRunQuery($s, [$l, $e]);
		$tr		= db_fetch_object($t)->trl_translation;

        return $tr == '' ? $e : $tr;

}
As this is a PDO SQL query, with the right indexes, it should churn out fine.

Re: Optimising nuTranslate() in js

Posted: Sat Dec 12, 2020 6:25 pm
by kev1n
Hi,

Thanks for your suggestion!

How about this?

Code: Select all

function nuTranslate(s) {

    var l = nuLANGUAGE.find(elem => elem.english === s);
    return !l ? s : l.translation;

}
Tests:

Code: Select all

nuTranslate('Language');
Returns the translated string.

Code: Select all

nuTranslate('Something_Else');
Returns Something_Else

Re: Optimising nuTranslate() in js

Posted: Sat Dec 12, 2020 7:04 pm
by apmuthu
Need to try out missing translations, invalid translations and other edge cases like case sensitivity.

Nubuilder4 just has too many DB requests just to put up a screen!

Translation strings are spread all over!

Missing translations, unknown usage strings translated and locations of strings fathomed and attached.

Re: Optimising nuTranslate() in js

Posted: Mon Dec 14, 2020 5:48 am
by kev1n
Thanks for that! I will look into it.

Suggestions for improvement are very welcome.

Re: Optimising nuTranslate() in js

Posted: Fri Dec 18, 2020 7:51 am
by apmuthu
Nubuilder file based translation resources culled using:

Code: Select all

SET @LANG:='Vietnamese';
SELECT CONCAT('$',"t['",trl_english,"']=",'"',trl_translation,'";') AS Translation FROM `zzzzsys_translate` WHERE trl_language=@LANG ORDER BY trl_english;
Attachment has all 14 languages parsed from table into files with sample code and links.

Re: Optimising nuTranslate() in js

Posted: Fri Dec 18, 2020 8:54 am
by kev1n
Hi,

Thanks for that! How would you include the language files and also merge in the user-translated strings?

Re: Optimising nuTranslate() in js

Posted: Mon Dec 28, 2020 5:25 pm
by apmuthu
The setup page has the language setting. Map that to the language files you need on demand and create a cache, overwriting the earlier one that exists with the complete English set as an auto fallback (the key).

Made one commit to just optimise the js translation function now.

Later we can work on the split language files and files and cache.