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();
});
Errol Fitzgerald
  • 2,978
  • 2
  • 26
  • 34

4 Answers4

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];
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
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'
rjz
  • 16,182
  • 3
  • 36
  • 35
  • 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. – RobG Mar 12 '12 at 00:23
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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • If I add more messages does this still work? I am getting undefined errors when adding more. – Errol Fitzgerald Mar 11 '12 at 22:46
  • 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? – Matt Ball Mar 11 '12 at 22:47
-1

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

Kory Sharp
  • 490
  • 3
  • 11
  • 2
    [`eval` is, in general, evil and unnecessary.](http://stackoverflow.com/questions/4812288/why-is-eval-unsafe-in-javascript) 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. – Matt Ball Mar 11 '12 at 22:37
  • Then what's the alternative solution because now I'd like to know. – Kory Sharp Mar 11 '12 at 22:44