使用i18n-active_record进行I18n转换:相同键的相同形式

时间:2022-12-08 07:40:50

I am working on an app in Rails 4 using i18n-active_record 0.1.0 to keep my translations in the database rather than in a .yml-file. It works fine.

我正在使用i18n-active_record 0.1.0在Rails 4中开发一个应用程序来保存我的翻译而不是.yml文件。它工作正常。

One thing that I am struggling with, however, is that each translation record is one record per locale, i.e.

然而,我正在努力的一件事是每个翻译记录是每个语言环境一个记录,即

#1. { locale: "en", key: "hello", value: "hello")
#2. { locale: "se", key: "hello", value: "hej")

which makes updating them a tedious effort. I would like instead to have it as one, i.e.:

这使得更新它们是一项繁琐的工作。我想把它作为一个,即:

{ key: "hello", value_en: "hello", value_se: "hej" }

or similar in order to update all instances of one key in one form. I can't seem to find anything about that, which puzzles me.

或类似的,以便以一种形式更新一个密钥的所有实例。我似乎无法找到任何关于这一点,这让我很困惑。

Is there any way to easily do this? Any type of hacks would be ok as well.

有没有办法轻松做到这一点?任何类型的黑客也都可以。

2 个解决方案

#1


2  

You could make an ActiveRecord object for the translation table, and then create read and write functions on that model.

您可以为转换表创建一个ActiveRecord对象,然后在该模型上创建读写函数。

Read function would pull all associated records then combine them into a single hash.

读取函数将拉取所有相关记录,然后将它们组合成单个哈希。

Write function would take your single hash input and split them into multiple records for writing/updating.

Write函数将采用您的单个哈希输入并将它们拆分为多个记录以进行写入/更新。

#2


0  

I ended up creating my own Translation functionality using Globalize. It does not explicitly rely on I18n so it is a parallell system but it works, although not pretty and it is not a replacement to I18n but it has the important functionality of being able to easily add a locale and handle all translations in one form.

我最终使用Globalize创建了自己的翻译功能。它并没有明确依赖I18n所以它是一个并行系统,但它可以工作,虽然不是很漂亮,它不是I18n的替代品,但它具有能够轻松添加语言环境并以一种形式处理所有翻译的重要功能。

  • Translation model with key:string
  • 带键的翻译模型:字符串

  • In Translation model:

    在翻译模型中:

    translates :value globalize_accessors :locales => I18n.available_locales, :attributes => [:value]

    翻译:value globalize_accessors:locales => I18n.available_locales,:attributes => [:value]

In ApplicationHelper:

  def t2(key_str)
    key_stringified = key_str.to_s.gsub(":", "")
    t = Transl8er.find_by_key(key_stringified)
    if t.blank? 
      # Translation missing
      if t.is_a? String
        return_string = "Translation missing for #{key_str}"
      else
        return_string = key_str 
      end
    else
      begin
        return_string = t.value.strip
      rescue
        return_string = t.value
      end
    end
    return_string
  end

#1


2  

You could make an ActiveRecord object for the translation table, and then create read and write functions on that model.

您可以为转换表创建一个ActiveRecord对象,然后在该模型上创建读写函数。

Read function would pull all associated records then combine them into a single hash.

读取函数将拉取所有相关记录,然后将它们组合成单个哈希。

Write function would take your single hash input and split them into multiple records for writing/updating.

Write函数将采用您的单个哈希输入并将它们拆分为多个记录以进行写入/更新。

#2


0  

I ended up creating my own Translation functionality using Globalize. It does not explicitly rely on I18n so it is a parallell system but it works, although not pretty and it is not a replacement to I18n but it has the important functionality of being able to easily add a locale and handle all translations in one form.

我最终使用Globalize创建了自己的翻译功能。它并没有明确依赖I18n所以它是一个并行系统,但它可以工作,虽然不是很漂亮,它不是I18n的替代品,但它具有能够轻松添加语言环境并以一种形式处理所有翻译的重要功能。

  • Translation model with key:string
  • 带键的翻译模型:字符串

  • In Translation model:

    在翻译模型中:

    translates :value globalize_accessors :locales => I18n.available_locales, :attributes => [:value]

    翻译:value globalize_accessors:locales => I18n.available_locales,:attributes => [:value]

In ApplicationHelper:

  def t2(key_str)
    key_stringified = key_str.to_s.gsub(":", "")
    t = Transl8er.find_by_key(key_stringified)
    if t.blank? 
      # Translation missing
      if t.is_a? String
        return_string = "Translation missing for #{key_str}"
      else
        return_string = key_str 
      end
    else
      begin
        return_string = t.value.strip
      rescue
        return_string = t.value
      end
    end
    return_string
  end