1

Here is the original function* ----------------------------------------

function lang(t) {

        $(".OutLbl").each(function () {
             $(this).html(
               $(this).html()
                .replace("Search", diction.wordlist.Search[t])
                .replace("By", diction.wordlist.By[t])
                .replace("Select Language", diction.wordlist["Select Language"][t])
                .replace("ID", diction.wordlist["ID"][t])
              );
            });
    };

var diction = {
    wordlist: {
        "Search": {
            dh: "ހޯދާ",
            en: "Search"
        },
        "By": {
            dh: "އިން",
            en: "by"
        },
        "Select Language": {
            dh: "ބަސް ޙިޔާރުކުރޭ",
            en: "Select Language"
        },
        "ID": {
            dh: "އައިޑީ",
            en: "ID"
        },
    }
}

**I need to simplify the replace in the above function instead of multiple replaces by

looping for each option in diction[wordlist]options

some way like**

function lang(t) {
       $(diction[wordlist] options).each( function () {
       var lll = this;
              $(".OutLbl").each(function () {
                $(this).html(
                $(this).html().replace(lll, diction.wordlist[lll][t])
                );
            });
        });
    };

the question is how to loop all options of a variable (diction.wordlist in my case) in a function, as the one I tried above doesn't work.

Thanks in advance

3
  • Can you clarify what you're asking? What's the question? Commented Jun 15, 2014 at 23:59
  • the question is how to loop all options of a variable (diction.wordlist in my case) in a function, as the one I tried above doesn't work. Commented Jun 16, 2014 at 0:14
  • Related/possible dup: stackoverflow.com/questions/8881436/… Commented Jun 16, 2014 at 0:20

2 Answers 2

1

I'd suggest:

var diction = {
    wordlist: {
        "Search": {
            dh: "ހޯދާ",
            en: "Search"
        },
        "By": {
            dh: "އިން",
            en: "by"
        },
        "Select Language": {
            dh: "ބަސް ޙިޔާރުކުރޭ",
            en: "Select Language"
        },
        "ID": {
            dh: "އައިޑީ",
            en: "ID"
        }
    }
};

function lang(t) {
    // initialise an array to store all the words/phrases:
    var words = [];
    // iterate over the properties of the 'diction.wordlist' object:
    for (var word in diction.wordlist) {
        // if the property is a property of the defined object:
        if (diction.wordlist.hasOwnProperty(word)) {
            // we push that word into the array:
            words.push(word);
        }
    }

    // creating a new RegExp, using the constructor, which allows us
    // to use variables. We create the literal regular expression:
    // /(Search)|(By)|(Select Language)|(ID)/
    // and use case-insensitivity (i) and global (g) flags:
    var reg = new RegExp('(' + words.join(')|(') + ')', 'gi');

    // using the html method and its anonymous function, which exposes:
    // i: the index of the current element amongst the collection, and
    // oldhtml: the current innerHTML of the current element (prior to manipulation)
    $('.OutLbl ').html(function (index, oldhtml) {
        // we return the html after replacing the matchedWords (using the callback
        // of replace():
        return oldhtml.replace(reg, function (matchedWord) {
            // finding the, for example, 'diction.wordlist['Search'][t]:
            return diction.wordlist[matchedWord][t];
        });
    });
}

$('#trigger').on('click', function(e){
    e.preventDefault();
    lang('dh');
});

JS Fiddle demo.

References:

Sign up to request clarification or add additional context in comments.

1 Comment

Well, I'm glad to have helped.
1

Try a for...in loop, sanitized with hasOwnProperty to avoid properties inherited through prototype:

$(".OutLbl").each(function () {
  var html = $(this).html();
  for(var word in diction.wordlist){
    if(diction.wordlist.hasOwnProperty(word))  {
      html = html.replace(word, diction.wordlist[word][t])
    }
  }
  $(this).html(html);
});

Note that, like your current code, it will replace only the first occurrence of each word.

To replace all of them, use regular expressions with g flag, escaping necessary characters.

var regExp = new RegExp(word.replace(/[.^$*+?()[{\|]/g, '\\$&'), "g");
html = html.replace(regExp, diction.wordlist[word][t])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.