Problems with dynamic loading in JavaScript

I am a JavaScript newbie and learn by working on a pure JavaScript "project" that calculates mathematical functions. It all works well. Now, as a further step, I want to make the messaging multilingual. The code should be capable of loading the appropriate language file at runtime. For the dynamic loading issue, I read and found solutions on Web pages like this one.

Before writing the dynamic code, I loaded it statically and the test code worked well. The code I am asking for help about is just making the minor difference of loading a "script" element.

The code where I run into problems is the this.getString function, where it is not possible to access the de element in the language file. At line console.log(eval(language, tag));, I get the error message "Uncaught ReferenceError: de is not defined".

Thank you for your help!
Utils.zip
Multilingual.zip
LVL 1
Ahmet Ekrem SABANSenior IT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings sae1962at, , I got your codes from the Utils.zip  and the Multilingual.zip, , but you have several many HTML codes on there like -
    TestMultilingual.html
     TestLang.html

so I did NOT try and figure out the code work in all of these?

But I looked at some of your scattered javascript, I see no reason to have so many separate Javascript files for so little javascript,

But in the -
    function Language(language) {

you have this method -
     this.getString = function(tag, strDefault)


and in that Method you have some code that I consider to be not correct as -
        var strReturn = eval('eval(language).' + tag);

When I work in javascript, I know that using the JS  eval( )   is a bad thing to do, and should be avoided, , however it does work when applied correctly, ,but in this line -
         var strReturn = eval('eval(language).' + tag);

you have an eval  INSIDE of an  eval !  I can not see this as functional?

and you call the   method with -
message += translator.getString('pleaseWait');

with only one parameter, but you have two in the definition
     this.getString = function(tag, strDefault)

You need to rework your operations to work with the JS objects in the language files like lang.de.js so you do not need the eval( )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ahmet Ekrem SABANSenior IT consultantAuthor Commented:
Thank you very much for your answer & sorry for the missing HTML files that I obviously forgot to add. The code is split in order to learn to work with "packages". I asked the same question on stack overflow & got a good hint there, which helped me to solve the problem. By the way: The double eval is part of code I found online & not a good idea, but it works.

Thank you for your help!
0
Slick812Commented:
As I said, if you are using ANY javascript eval( ) , then you can use much better JS operations to do your work, and have a better understanding of using javascript methods. The code you use in the -

function Language(language) {

is very awkward, some woud even say bad,  and you do not seem to know that you can assign Objects to a common variable, and get a change of the Object used, without using any eval( ).

also in your language JS files, you have something like-
    de = { pleaseWait : "some text here"}

if you declare a global JS variable like -
    var dynamic = {};

then instead in your language JS files you can use
    dynamic.de = { pleaseWait : "some text here" }

and then access the  "de" contents in your Language object
   var lan = new Language("de") ;

with the method
   lan.getString('pleaseWait');

and use -
    var strReturn = dynamic[language][tag];

no eval( ) is needed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.