0

I'd like to use React-Intl library, to i18n our app. Probably we'll use json format from weblate, and there is a main question. Can I use couple translation keys into one single string?

Dont get it? No problem, there is an example.

{
    "test1": "ABC",
    "test2": "CBA",
    "result": "test {test1} <--- (or any different notation)"
}

// The output for result should be: "test ABC"
CreaTian
  • 1
  • 1
  • 2

2 Answers2

0

// The output for result should be: "test ABC"
let a = {  
          "test1": "ABC", 
          "test2": "CBA",
          "result": "test : {test1}" 
        }

// get the line with the result
function takeResult(obj){
    let {test1, test2, result} = obj;
    return result;
}

// get the key that is in the result line
function takeKey(value){
    begin = value.search('{');
    end = value.search('}');
    return value.substring(begin+1, end);
}

// get the text 
function takeText(value){
    begin = value.search('"');
    end = value.search('{');
    return value.substring(begin+1, end);
}

//
function printTranslation(obj){
    line = takeResult(obj);
    result = takeText(line) + obj[takeKey(line)];  // use the key to access the right value
    console.log(result);
    return result; 
}

printTranslation(a);
Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35
  • This solution works only in this specific case. I mean, lets change the order of words in result, like: "result": "{test1} ABC" - and output is incorrect. Lets add another "variable" to the result: "result": "test {test1} {test2}" - still failed. I am hoping, that React-Intl has a some feature, and provide me some method to solve that problem. Thanks, anyway. – CreaTian Jul 19 '19 at 13:43
0

As of right now values are explicitly passed in via values prop so it is not officially supported. Feel free to create a GitHub issue and we can take a look.

Long Ho
  • 599
  • 4
  • 15