在Django中提供Rails-i18n支持的好方法 [英] Good way to provide Rails-way i18n support in Django

查看:124
本文介绍了在Django中提供Rails-i18n支持的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Rails的主要区别在于(新)Rails我羡慕:国际化支持(Django也有一个,但我更喜欢Rails的风格) '和Django的方法是什么样的字符串表现为键值转换映射中的键,即



Django版本(键 - 字符串以主语言,例如英语):

  msgid保存并退出
msgstrZapisz iwyjdź
  //英文翻译文件

msgidSAVE_QUIT_MESSAGE
msgstr保存并退出

  // polish translation file 

msgidSAVE_QUIT_MESSAGE
msgstrZapisz iwyjdź

Rails的支持i18n的方式是更好的IMHO(想到关键的不变性 - 抵抗语法/拼写更正;语言不可知论等)。



在Django中使用此模式的一种方法是使用一些抽象语言来唯一的翻译目的(该语言中的字符串将使不可变的键),但Django只支持固定的语言集。另一个解决方案 - 牺牲一个支持(未使用)的语言来扮演这个角色 - 但这只是坏的:P



任何想法/第三方应用程序/解决这个问题的技巧?






Sidenote:扩展i18n对图书馆语言的支持将给出有趣的机会: / p>

  //俚语翻译文件

msgidSAVE_QUIT_MESSAGE
msgstr保存狗屎' 'quit,bro


解决方案

退回一分钟二。你在这里做三重工作首先,您必须提出一个 UNIQUE_ID ,然后强制人们从代码或其他语言文件中查找上下文,以找出正确的消息为 AMBIGUOUS_ARGUMENT_PROVIDED 将直到您提供实际翻译。谁曾经说过,创建可以有意义地传达上下文并提供良好的消息提示的ID是很容易的?



你想做什么是一些荒谬的狗屎兄弟!除了笑话之外,gettext 是最普遍和广泛使用的i18n和l10n API的原因是因为每个消息都从其内容中分配出一个唯一的消息目录ID,并且因为证明你会有更好的时间翻译消息而不是提供ID的翻译,这让人想起每个人都试图制作自己的键 - 值i18n框架,因为它是最直接的设计。



你最终会得出结论,使用gettext的方式不是一个坏主意,现在可以通过忘记整个想法来节省自己。


There's one thing in (new) Rails I envy: internationalization support (Django has one too, but I prefer Rails' flavour).

The key difference between Rails' and Django's approaches is what kind of string behaves like keys in key-value translation mapping, i.e.

Django version (keys - strings in "main" language, for example english):

msgid "Save and quit"
msgstr "Zapisz i wyjdź"

Rails version equivalent (keys - abstract strings; standalone unusable - one need to provide at least 1 "translation") - actually, Rails uses YAML format, but following example present the idea:

// english translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Save and quit"

and

// polish translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Zapisz i wyjdź"

Rails' way of supporting i18n is IMHO much better (think of key immutability - resistant to grammar/spelling corrections; language agnosticism etc).

One way to utilize this schema in Django would be to use some abstract language for the sole purpose of being translated (strings in that language would make immutable keys), but Django support only fixed set of languages. Another solution - sacrifice one of the supported (unused) languages to play this role - but this is just bad :P

Any ideas/third-party apps/techniques to solve this issue?


Sidenote: extending i18n support for artibrary languages would give funny opportunities:

// slang translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Save shit 'n' quit, bro"

解决方案

Step back for a minute or two. Your doing triple work here. First you have to come up with a UNIQUE_ID and then you force people to look up either the context from the code or another language file to figure out what would the proper message for AMBIGUOUS_ARGUMENT_PROVIDED would be until you get down to providing the actual translation. And who ever said that creating IDs that can meaningfully convey the context and provide good message hints was ever easy?

What your trying to do is some preposterous shit bro! Jokes aside, the reason gettext is the most prevalent and widely used i18n and l10n API is because each message gets a unique message catalog ID assigned from it's contents and because it's proven you'll have a way better time translating messages than providing translations for IDs, reminiscent of when everyone tried making their own key->value i18n framework because it was the most straightforward to design.

You'll eventually conclude that it was a bad idea to use gettext the way it wasn't meant to and you can save yourself right now by forgetting about the whole idea.

这篇关于在Django中提供Rails-i18n支持的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆