Rails 3 I18n用于数据库表 [英] Rails 3 I18n for database tables

查看:220
本文介绍了Rails 3 I18n用于数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找有关使用国际化的最佳做法的一些提示和建议。我搜索过,但我并没有真正满意我读到的。我读过的大多数文章都集中在使用yml文件为I18n,这将不能在我的情况下工作。



我目前有几个数据库表与英文文本。这些表中的一些具有文本字段,其具有几个句子长,并且一些是6+段文本。我也想在西班牙语中有这些字段。



我目前考虑的方法是使用I18n活动记录gem,并有1个翻译表,应用程序将用于应用程序中的所有翻译

  class CreateTranslations< ActiveRecord :: Migration 
def self.up
create_table:translations do | t |
t.string:locale
t.string:key
t.text:value
t.text:interpolations
t.boolean:is_proc,:default => ;。 false

t.timestamps
end
end

def self.down
drop_table:translations
end
end

这是最好的办法吗? b
$ b

一方面,所有的翻译都将很好地存储在一个表中。另一方面,每当用户向数据库查询I18n内容时。应用程序将查询原始表中的键,然后查询转换表。另一个问题是,翻译表将是巨大的,并有大量的行,因为它将存储应用程序的所有翻译(一切从部分标题[几个字]到整个文本页。



感谢任何信息。感谢

解决方案



另一个更快,更快的方法是,可能更好的解决方案是使用Redis作为I18n的后端。请参见 http://guides.rubyonrails.org/ i18n.html#using-different-backends http://railscasts.com/episodes/256-i18n-后端



无论你在哪里存储翻译,都不需要尝试自己管理插值,因为I18n库处理这个相当不错(除非你'做一些真正的习惯,那是)。


I'm looking for some tips and advice for the best practices for using internationalization. I've search around, but I haven't really been satisfied with what I read. Most of the articles I've read focus on using yml files for I18n which will not work in my situation.

I currently have several database tables with text in English. Some of these tables have text fields that are a few sentences long and some are 6+ paragraphs of text. I would like to also have these fields in Spanish.

The approach I'm currently considering is to use the I18n-active record gem and have 1 translations table that the app will use for all the translations in the app

 class CreateTranslations < ActiveRecord::Migration
    def self.up
      create_table :translations do |t|
        t.string :locale
        t.string :key
        t.text   :value
        t.text   :interpolations
        t.boolean :is_proc, :default => false

        t.timestamps
      end
    end

    def self.down
      drop_table :translations
    end
  end

Is this the best way to proceed?

On one hand all the translations will be nicely stored in one table. On the other hand, every time a user queries the database for I18n content. The app will query the original table for the key, and then query the translations table as well. Another concern is that the translation table will be massive and have a huge amount of rows since it will be storing all the translations for the app (everything from section title [a few words] to entire pages of text.

Any information is appreciated. Thanks

解决方案

Storing translations in the db is not too bad a solution. Don't be afraid of large tables - databases are made for that! Just make sure your indexes are configured correctly and cache whatever you can.

Another, faster and possibly better solution is to use Redis as a backend for I18n. See http://guides.rubyonrails.org/i18n.html#using-different-backends and http://railscasts.com/episodes/256-i18n-backends.

Wherever you do store the translations, there's no need to try to manage the interpolations yourself as the I18n library handles this quite nicely (unless you're doing something really custom, that is).

这篇关于Rails 3 I18n用于数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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