不要重复自己与国际化

Mat*_*ard 11 internationalization

前段时间我正在阅读W3C关于" 重写脚本内容中使用字符串 "的文章,其中包含一些有关国际化的有用建议,但这让我感到不安,因为我们认为DRY(不要重复自己)消除重复代码的原则.

举个例子,我们可能会有这样的代码......

print "The printer is ";
if (printer.working) {
    print "on.\n";
} else {
    print "off.\n";
}

print "The stapler is ";
if (stapler.working) {
    print "on.\n";
} else {
    print "off.\n";
}
Run Code Online (Sandbox Code Playgroud)

我的直觉是大致消除重复如下......

report-state(printer, "printer");
report-state(stapler, "stapler");

function report-state(name, object) {
    print "The "+name+" is ";
    if (object.working) {
        print "on\n";
    } else {
        print "off\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

...但是,如果我们需要将其本地化为西班牙语,那么这样做会导致代码出现问题,因为在这两种情况下,"on"这个词明显不同.

所以,我想我的问题是,其他开发人员如何平衡DRY原则与其代码的国际化?

我的一部分想要争辩说,国际化是" 你不会需要它 "的极端编程之一.然而,另一方面,考虑到DRY原则的重构应该通过使其易于实现所需的功能来平衡这一点,而不是像在这里那样更难.

Men*_*elt 16

我会尝试在语言资源中保留完整的句子.正如你所说,在不同的背景下你可能需要不同的词.但更大的问题是句子的顺序在不同的语言中可能会有所不同.因此,从单词构建字符串可能会导致问题.

只是存储

The printer is on
The printer is off
The stapler is on
The stapler is off
Run Code Online (Sandbox Code Playgroud)

在每种语言的语言资源中.这里的重复不是一个维护头痛,而是试图找出应用程序中弹出所有单个单词的位置.


小智 7

100%同意Mendelt.

这不仅是一个维护问题,也可能是一个语言问题.在所有拉丁语言中,主题的性别,数量和案例都会影响其他元素.罗马尼亚语的例子

  The printer is on: Imprimanta este pornit? // feminine
  The printer is off: Imprimanta este oprit?
  The stapler is on: Perforatorul este pornit // masculine
  The stapler is off: Perforatorul este oprit
Run Code Online (Sandbox Code Playgroud)

另见http://www.mihai-nita.net/article.php?artID=20060430a