React-Intl 和 JSON - 我可以在一个字符串中使用多个键吗?



我想使用 React-Intl 库来 i18n 我们的应用程序。可能我们会使用 weblate 中的 json 格式,有一个主要问题。我可以将几个转换键使用到一个字符串中吗?

不明白吗?没问题,有一个例子。

{
    "test1": "ABC",
    "test2": "CBA",
    "result": "test {test1} <--- (or any different notation)"
}
// The output for result should be: "test ABC"

// 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);

截至目前,值是通过 prop 显式传入values因此不受官方支持。随意创建一个 GitHub 问题,我们可以看看。

最新更新