i18n - 创建语言文件时使用哪些命名约定?

发布于 2024-08-31 22:14:10 字数 1135 浏览 5 评论 0原文

我正在开发一个需要 i18n 支持的 CMS。翻译字符串作为数组存储在语言文件(即 en.php)中。是否有任何命名约定......

我如何改进下面的示例语言文件......

 // General
 'general.title' => 'CMS - USA / English',
 'general.save' => 'Save',
 'general.choose_category' => 'Choose category',
 'general.add' => 'Add',
 'general.continue' => 'Continue',
 'general.finish' => 'Finish',

 // Navigation
 'nav.categories' => 'Categories',
 'nav.products' => 'Products',
 'nav.collections' => 'Collections',
 'nav.styles' => 'Styles',
 'nav.experts' => 'Experts',
 'nav.shareyourstory' => 'Share Your Story',

 // Products
 'cms.products' => 'Products',
 'cms.add_product' => 'Add Product',

 // Categories
 'cms.categories' => 'Categories',
 'cms.add_category' => 'Add Category',

 // Collections
 'cms.collections'=> 'Collections',
 'cms.add_collections' => 'Add Collection',

 // Stylists
 'cms.styles' => 'Stylists',
 'cms.add_style' => 'Add Style',
 'cms.add_a_style' => 'Add a style',

 // Share your story
 'cms.share_your_story' => 'Share Your Story',

 // Styles
 'cms.add_style' => 'Add Style',

I'm developing a CMS that required i18n support. The translation strings are stored as an array in a language file (ie, en.php). Are there any naming conventions for this..

How can I improve on the sample language file below...

 // General
 'general.title' => 'CMS - USA / English',
 'general.save' => 'Save',
 'general.choose_category' => 'Choose category',
 'general.add' => 'Add',
 'general.continue' => 'Continue',
 'general.finish' => 'Finish',

 // Navigation
 'nav.categories' => 'Categories',
 'nav.products' => 'Products',
 'nav.collections' => 'Collections',
 'nav.styles' => 'Styles',
 'nav.experts' => 'Experts',
 'nav.shareyourstory' => 'Share Your Story',

 // Products
 'cms.products' => 'Products',
 'cms.add_product' => 'Add Product',

 // Categories
 'cms.categories' => 'Categories',
 'cms.add_category' => 'Add Category',

 // Collections
 'cms.collections'=> 'Collections',
 'cms.add_collections' => 'Add Collection',

 // Stylists
 'cms.styles' => 'Stylists',
 'cms.add_style' => 'Add Style',
 'cms.add_a_style' => 'Add a style',

 // Share your story
 'cms.share_your_story' => 'Share Your Story',

 // Styles
 'cms.add_style' => 'Add Style',

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月如刀 2024-09-07 22:14:10

我见过的一个有趣的选择是使用英文字符串本身作为键:

// General
'CMS - USA / English' => 'CMS - USA / English',
'Save' => 'Save',
'Choose category' => 'Choose category',
...

需要注意的一些要点:

  1. 这使得应用程序代码更易读,并且对开发人员更透明。开发人员可能没有注意到 cms.styles 在应用中错误地显示为 Stylists。但是 'Styles' => “造型师” 非常引人注目,尤其是对于单行审核脚本而言。
  2. 它更加脆弱,因为对英文文本的更改会影响所有其他语言文件。然而,它应该会非常吵闹地失败,因此很容易被发现。
  3. 它更加健壮,因为如果测试未能发现遗漏,生产系统可以轻松地退回到英文文本。
  4. 您不必仅仅因为 Category 等术语出现在不同的位置而重复它们。

One interesting option I've seen is to use the English strings themselves as the keys:

// General
'CMS - USA / English' => 'CMS - USA / English',
'Save' => 'Save',
'Choose category' => 'Choose category',
...

Some points to note:

  1. This makes the application code more readable, and more transparent to the developer. The developer might not notice that cms.styles incorrectly appears as Stylists in the app. But 'Styles' => 'Stylists' stands out like a sore thumb, particularly to a one-line auditing script.
  2. It is more fragile, since changes to the English text affect all the other language files. It should fail quite noisily, however, so it's easy to spot.
  3. It is more robust, since the production system can trivially fall back to the English text if testing failed to spot an omission.
  4. You don't have to repeat terms like Category just because they appear in different places.
赠佳期 2024-09-07 22:14:10

关于文件命名约定,您可能需要使用 ISO 639-2 Alpha 3 代码:
http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes

With regards to file naming conventions you might want to use ISO 639-2 Alpha 3 codes :
http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes

暮凉 2024-09-07 22:14:10

使用英语单词作为键可以很好地发挥作用,但是稍后您可能会遇到问题。随着应用程序的增长,您可能需要开始为键命名,以便更容易找到它们,例如通过控制器和操作/目的/上下文。

示例:如果您有一个带有关键“类别”的翻译,这将阻止诸如“categories.flashes.failure”之类的命名空间,这些命名空间对开发人员来说是有表达力的,但又不会与文本过于紧密地联系在一起。

这是我处理上面示例的方法,假设没有 WordsController ;-)

t(:'words.categories', :default => 'Categories')

这也使得编写不涉及翻译的测试变得更容易。

Using English words as keys can work well, however you may run into issues later. As the app grows, you may want to start namespacing your keys so they're easier to find, eg by controller and action/purpose/context.

Example: If you have a translation with key "categories", that would would prevent namespacing things like "categories.flashes.failure", which are expressive to the developer without being too closely tied to the text.

Here's how I'd handle the above example, assuming there's no WordsController ;-)

t(:'words.categories', :default => 'Categories')

This also makes writing tests which aren't concerned with translations easier.

贵在坚持 2024-09-07 22:14:10

您也可以使用 gettext 代替。

You could also use gettext instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文