Javascript 通过json数组命令翻译变量文本

Javascript 通过json数组命令翻译变量文本,javascript,json,Javascript,Json,我正在尝试建立一个文本翻译。我有下面的脚本,其中找到的单词将被翻译。现在我需要将json数组中未找到的单词保留在翻译文本中 有没有更简单的方法来替换特殊字符? 我已经有了我自己的json指令,就像示例中的那个,它不能来自somwhere online或第三方,因为它非常技术性,脚本必须脱机工作 我喜欢函数,因为文本是可变的 var dictionary = [ { dutch: "motor", english: "motor" }, { dutch: &

我正在尝试建立一个文本翻译。我有下面的脚本,其中找到的单词将被翻译。现在我需要将json数组中未找到的单词保留在翻译文本中

有没有更简单的方法来替换特殊字符?
我已经有了我自己的json指令,就像示例中的那个,它不能来自somwhere online或第三方,因为它非常技术性,脚本必须脱机工作

我喜欢函数,因为文本是可变的

var dictionary = [
{
dutch: "motor",
english: "motor"
},
{
dutch: "voeding",
english: "supply"
}
];

//Translate this from dutch to english  
var text = "12V motor voeding is laag.";

text = text.replace(",", " ,");  
text = text.replace(".", " .");  
textArray = text.split(" ");

for (a = 0; a < textArray.length; a++) {  
var textItem = compare(textArray[a]);  
}

function compare(word){  
for (i = 0; i < dictionary.length; i++) {  
if (word == dictionary[i].dutch){  
var textTranslated = dictionary[i].english;  
document.write(textTranslated + " ");  
}
缺少
12v

请尝试以下操作:

function compare(word){  
    for (i = 0; i < dictionary.length; i++) {  
        if (word == dictionary[i].dutch) {  
            var textTranslated = dictionary[i].english;  
            document.write(textTranslated + " ");
            return;
        }  
    }
    document.write(word + " ");
}
函数比较(word){
对于(i=0;i
谢谢KienHT,它工作得更好,但我有更多可变文本需要翻译。
请看下面我的html脚本的一部分。
要使用相同的功能进行翻译:

document.write(foutgebied);  
document.write(fout);  
document.write(oplossing); 
也许我还需要编辑html脚本

<form method="POST" action="">
<hr>
<p>     
<b>Foutgebied: </b>
<script>
document.write(foutgebied);
</script>
<br><br>
<b>Klacht: </b><br>
<script>
document.write(fout);
</script>
<br><br>
<b>Oplossing:</b><br>
<script>
document.write(oplossing);
</script>
</p>
<hr>
</form>


Foutgebied: 文件。编写(foutgebied);

克拉赫特:
文件编写(fout);

Oplossing:
文件。编写(oplossing);


<form method="POST" action="">
<hr>
<p>     
<b>Foutgebied: </b>
<script>
document.write(foutgebied);
</script>
<br><br>
<b>Klacht: </b><br>
<script>
document.write(fout);
</script>
<br><br>
<b>Oplossing:</b><br>
<script>
document.write(oplossing);
</script>
</p>
<hr>
</form>