1
var dict = {
  "configMigratedTo": {
        "message": "Migrated configuration to configurator: $1"
    }
}
var parametersForTranslation = {};
function __tr(src, params) {
  parametersForTranslation[src] = params;
  return buildMessage(src);
}

function buildMessage(src){
    var message=dict[src] ? dict[src].message : src
    console.log(message);
    var messageArray = message.split("$");
    var output = "";
    messageArray.forEach(function(elem, index){
        if(index === 0){
            output += elem;
        }else{
            // get variable and index
            var paramIndex = configMigratedTo.substring(0, 1);
            var paramValue = parametersForTranslation[src][paramIndex-1];
            output += paramValue;
            output += configMigratedTo.substring(1);
        }
    });
    return output;
}
__tr("configMigratedTo", [2]);
console.log(buildMessage("configMigratedTo"));

i want get result like __tr("configMigratedTo", [2]); then it will give me

Migrated configuration to configurator: 2

i do not know where is wrong in my code

8
  • What is __tr() ? Commented Jul 23, 2018 at 18:19
  • @LouysPatriceBessette it's a function, defined above, in code. Commented Jul 23, 2018 at 18:19
  • basically, it means find translation according to the key...__tr means translation Commented Jul 23, 2018 at 18:20
  • Ha. ok. But you mess on the source. You want to access configMigratedTo in the dict object. It should be something like : dict.configMigratedTo.message to get the value Migrated configuration to configurator: $1... Now I really don't know if the $1 will taken in account as a variable... That depends on the buildMessage() function And I can't tell just like this. Commented Jul 23, 2018 at 18:22
  • 1
    Well, before you can play around with programming logic you need a solid foundation of programming (JavaScript, in this case) basic concepts. And from trying to access a variable which has not yet been defined it looks like you should focus on the latter. Do you understand the difference between the missing variable configMigratedTo var and the existing property on the dict object? Commented Jul 23, 2018 at 18:31

3 Answers 3

1

Try this one. Hope it helps!

var dict = {
  "configMigratedTo": {
        "message": "Migrated configuration to configurator: $1"
    }
}

function __tr(src, params) 
{
    for (var key in dict)
  {
    if (key === src)
    {
        var message = dict[key].message;
        return message.substring(0, message.length - 2) + params[0];
    }
  }
  return;
}


console.log(__tr("configMigratedTo", [2]))

https://jsfiddle.net/eLd9u2pq/

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

Comments

1

Would that be enought?

var dict = {
  "configMigratedTo": {
        "message": "Migrated configuration to configurator: "
    }
}

function buildMessage(src,param){
    var output = dict[src].message + param;
    return output;
}

console.log(buildMessage("configMigratedTo",2));

Comments

1

You are overcomplicating this, it's much easier using a regex and passing a function as replacer

var dict = {
  "configMigratedTo": {
    "message": "Migrated configuration to configurator: $1"
  }
}
function __tr(src, params) {
  if (! dict[src]) return src;
  if (! /\$0/.test(dict[src].message)) params.unshift('');
  return dict[src].message.replace(/\$(\d)+/g, (orig, match) => params[match] || orig);
}

console.log(__tr("configMigratedTo", [2]));

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.