如何在翻译HTML网站时处理带变量的字符串?

时间:2015-01-30 10:22:43

标签: javascript html json html5 dictionary

我正在尝试制作字典来翻译网站。问题是我无法找到一种方法来捕获变量中的字符串!例如:

<div ... lang="fr">Something 52 something else</div>

其中52是变量,可以是任何数字。我希望的是这样的:

  

&#34;东西*其他东西&#34;:&#34; blabla * blablabla bla&#34;

任何想法如何使这项工作?

1 个答案:

答案 0 :(得分:0)

您可以随意自行实施:

function frmtStr() {
  var argCnt, out;

  argCnt = arguments.length;
  if (argCnt < 1) {
    return ""
  }
  out = arguments[0]; // your localized string.

  if (argCnt < 2) {
    return out
  }
  for(var i = 1; i < argCnt; i++) {
    // replace '*' with the arguments
    out = out.replace(/\*/, arguments[i]);
  }
  return out; 
}

frmtStr('foo *', 'bar'); // foo bar
frmtStr('foo * *', 'bar', 'lorem'); // foo bar lorem
frmtStr('* bar * * ', 'foo', 'lorem', 'ipsum'); // foo bar lorem ipsum

这不完美,但应该给你一个想法。我敢打赌,有大量的图书馆提供此功能。