代码之家  ›  专栏  ›  技术社区  ›  Matt Sheppard

不要重复自己与国际化的较量

  •  11
  • Matt Sheppard  · 技术社区  · 17 年前

    不久前,我正在读W3C关于 Re-using Strings in Scripted Content ,其中包含了一些关于国际化的有用建议,但我觉得这与消除重复代码的枯燥(不要重复)原则不一致。

    以他们为例,我们可能会有这样的代码。。。

    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";
    }
    

    我的本能是消除重复,大致如下。。。

    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";
        }
    }
    

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

    我有一部分想说国际化是一种极端的编程方式 you arent gonna need it

    6 回复  |  直到 17 年前
        1
  •  16
  •   Mendelt    17 年前

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

    只是储存

    The printer is on
    The printer is off
    The stapler is on
    The stapler is off
    

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

        2
  •  7
  •   user19050    17 年前

    这不仅是一个维护问题,也可能是一个语言问题。 罗马尼亚的例子

      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
    

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

        3
  •  2
  •   Bill the Lizard    17 年前

    我同意Mendelt Siebenga的观点,他说你应该在语言资源文件中保留完整的句子或短语。语法上的差异总是会妨碍你跨语言替换单个单词。这仍然会导致比第一个示例更少的重复代码,因为您只需要检查对象类型及其状态,然后从语言资源打印适当的消息。

        4
  •  2
  •   maccullt    17 年前

    loc。团队实际上更喜欢单独但几乎重复的消息。 但是,它们将接受参数化消息。

    例如,“%(设备)%为%(打开或关闭)%”

        5
  •  1
  •   Chris McAtackney    17 年前

    我想这取决于你想要达到的语言质量水平。

    通过尽量减少处理这些真实语言字符串的代码的重复,您将自己暴露在不同语言的语法和结构中的另一层逻辑中。在生成代码时需要做大量的工作,这些代码仍然保留语言的原始结构,同时尽量减少重复。

    当然,你可以找到一个折衷点,尽量减少代码重复,但放弃令人满意的语法口才。以Ultima Online为例,当它被本地化时,一个先前读为“一堆329金币”的字符串变成了类似“一堆329金币”的东西。不是很好,但这是一个相当合理的解决方案,易于本地化。

        6
  •  0
  •   badbod99    17 年前

    我建议在文本值中使用CMS而不是硬编码来覆盖本地化。