1

This has been bugging me for quite some time now. All I want to do is return a value from an array given its key, but use a variable (_interview) to choose what array to return from.

var utils = function () {

var _language = "";
var _interview = "";
var _base = "";

var _alerts = function (code) {

    var en = new Array(),
        fr = new Array();

    en[0] = "You must enter a value in the box before continuing.";
    fr[0] = "Vous devez entrer une sélection pour les options mises en évidence.";

    // I know _interview is returning the correct value of either "en" or "fr"
    // I have tried this[_interview][code] but I get property not defined at the index
    // How do I return the index from the right array?
};

return {

    lang : function () {

        _language = $("#language option:selected").val();
        var answer = confirm(_alerts(0)); // Call from here

    }
};
}();

$(document).ready(function ()
{
    utils.lang();
});

4 Answers 4

3

Better to use 2D Array (deep object structure), and strings as keys instead of integer indexes

var languages = {
    'en': {
        'string_key_1' : 'String For Key 1'
    },
    'fr': {
        'string_key_1' : 'Le String ...'
    }
};

To get values like

var key = 'string_key_1';
var translated_value = languages[_interview][key];
Sign up to request clarification or add additional context in comments.

Comments

1

You can access the value of an object property using square brackets, like so:

var index = 'foo',
    myObj = {
      foo: 'bar'
    };

alert(myObj[index]); // 'bar'

The same goes for arrays, but only numeric keys will work:

var index = 1,
    myArr = ['foo','bar'];

alert(myArr[index]); // 'bar'

1 Comment

Not just numeric keys, arrays are just objects with a special length property. You can add and read properties just like other objects, but of course they are meant to be used with numeric property names that are used as indexes.
1

Bracket notation is the right way to do it. However, you need to put the languages into an array, since this does not refer to the function scope as you seem to think. In this eedparticular case, this === window which is obviously not what you want!

Give this a shot instead:

var utils = function() {

    var _language = "";
    var _interview = "";
    var _base = "";

    var _alerts = function(code) {

        var messages = {
            en: ["You must enter a value in the box before continuing."],
            fr: ["Vous devez entrer une sélection pour les options mises en évidence."]
        };

        // I know _interview is returning the correct value of either "en" or "fr"
        // I have tried this[_interview][code] but I get property not defined at the index
        // How do I return the index from the right array?
        return messages[_language][code];
    };

    return {

        lang: function() {

            _language = $("#language").val(); // note the selector change
            var answer = confirm(_alerts(0)); // Call from here
        }
    };
}();

$(document).ready(function() {
    utils.lang();
});​

Working demos: english|french

2 Comments

If I add more messages does this still work? I am getting undefined errors when adding more.
Add more messages for each language, or add new languages? Either way, it should work. How about editing one of the jsFiddles to show what you're trying?
-1

Check out the eval() function. This should help you accomplish what you need.

2 Comments

eval is, in general, evil and unnecessary. There are much better solutions. It can be particularly harmful to suggest that someone use eval when they might not necessarily understand the full implications.
Then what's the alternative solution because now I'd like to know.

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.