0

对于具有大量固定字符串(标签、公告等)的网站,我将在项目中添加国际化,我想知道您在原始代码中使用关键字与真实短语的经验?哪个更好用?

检查这个例子 - 在 django 中:

实句:

<p>
    {% trans "Welcome to my website" %}
</p>

关键词:

<p>
    {% trans "WELCOME_MESSAGE" %}
</p>

当然使用真实的短语更容易,但我有一些不好的经历。考虑一个通用词,{% trans "contacts" %}用作页面的标题,而在其他地方使用在短语中,例如{{selected_contacts_count}}{% trans "contacts" %}contacts页面标题的翻译可能与contacts短语中单词的翻译不同,所以这是一个冲突。

4

3 回答 3

2

我认为这主要是个人品味的问题。

使用真实的短语,您将遇到提到的冲突,对于您的示例,您可以使用

{% blocktrans %}{{selected_contacts_count}} contacts{% endblocktrans %}反而。

使用关键字,您必须为所有翻译创建一个唯一的关键字,这在您的项目增长时可能并不容易,因此事先设计某种命名约定可能是个好主意。

于 2012-09-04T20:27:44.153 回答
2

Of course that keywords are better, not only because they are easier to read for a programmer, but also makes finding those strings in files faster, and you are not 'tied' to one language (e.g. English)

于 2012-09-04T19:04:56.597 回答
1

关键词。

在某个地方,某个时间,某个地方,你需要一个翻译表。它可能是平面文件,也可能是数据库存储,但它可能看起来像这样:

field | language | message

构建和维护基于关键字的系统会容易得多:

WELCOME_MESSAGE | EN | Welcome to our online h ome
WELCOME_MESSAGE | FR | bienvenue chez nous en ligne
WELCOME_MESSAGE | SW | kuwakaribisha nyumbani kwetu online

如果您将数据插入到这些翻译中,您可能会遇到的一个问题是必须处理复数形式:

There is one item.  
There are four items.

You have one new message.
You have four new messages.

因此,在您前进时请记住这一点。

于 2012-09-04T22:11:13.747 回答