Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Optimising nuTranslate() in js

Questions related to using nuBuilder Forte.
Post Reply
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Optimising nuTranslate() in js

Unread post 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
Last edited by apmuthu on Sat Dec 12, 2020 5:41 pm, edited 1 time in total.
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: Optimising nuTranslate() in js

Unread post 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.
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Optimising nuTranslate() in js

Unread post 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
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: Optimising nuTranslate() in js

Unread post 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.
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Optimising nuTranslate() in js

Unread post by kev1n »

Thanks for that! I will look into it.

Suggestions for improvement are very welcome.
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: Optimising nuTranslate() in js

Unread post 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.
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Optimising nuTranslate() in js

Unread post by kev1n »

Hi,

Thanks for that! How would you include the language files and also merge in the user-translated strings?
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: Optimising nuTranslate() in js

Unread post 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.
Post Reply