为 Rails 项目添加动态 I18n 内容

场景:对于一个已经做好静态 I18n 的 Rails 项目,需要对动态数据内容也适配国际化。

首先,动态内容的数据肯定是存在数据库中的,并且字段名也采用统一的 fieldname_#{I18n.locale} ,方便统一管理。

问题的核心是如何在尽量不修改现有代码的情况下动态读取模型某些字段的当前 locale 的值。

比如:原来的 user.name #=> Marry ,现在需要根据当前 locale (比如 cn)变成 user.name # => 翠花

首先接口肯定不能改,view 层中有很多很多的 user.name 的调用,就算用批量修改的方式改为类似 user.name_#{I18n.locale} 的写法, 也很不优雅,何况不只是 name 属性需要国际化,以后每增加一个字段或模型的国际化都将成为很大的负担。

说到如何在原有类的基础上增加功能,那自然会想到使用装饰器模式了。

关于装饰器模式的实现,一种方式是使用 delegate:

require 'delegate'

class User < ApplicationRecord
  # attribute name
end

class InternationalUserDecorator < SimpleDelegator
  def name
    __getobj__.send("name_#{I18n.locale}")
  end
end

user = InternationalUserDecorator.new(User.find(12345))

I18n.locale = :en
user.name # => 'Marry'

I18n.locale = :cn
user.name # => '翠花'

但是这显然是不行的,因为这需要修改每一个 User 实例的生成,使用 Decorator 去显式地包装它。

类似的,还有一种通过继承 module 的方式,同样需要显示的修改每一个模型的实例,这样的改动对原代码改变很大,也不能使用。

module EnglishUser
  def name
    "Marry"
  end
end

module ChineseUser
  def name
   "翠花"
  end
end

user = User.find(123)
user.extend(EnglishUser) #=> name "Marry"
user.extend(ChineseUser) #=> name "翠花"

可见,需要在获取模型实例时进行修改的思路是行不通,如果项目一开始就使用仓储模式的话,修改起来会容易很多,不过这超出了本文的范围。

想要尽可能小的修改原代码,那只能使用元编程了,我们需要一个 Module,来动态生成 locale 对应的 field 供模型调用。

localecn 的时候, user.name => user.name_cnlocaleen 的时候, user.name => user.name_en

当然,不是模型所有的 field 都需要做国际化,必须可以指定需要国际化的字段

client 端的代码应该是这样的:

class User < ApplicationRecord
  # attributes :name, :position, :age
  include I18nDecorator.new(:name, :position)
end

这里的难点在于,需要传参数给这个 Module,可 include 的时候是不能传参的。

在这里,我们把 I18nDecorator 定义为 Module 的一个子类,这样就可以通过 new 的时候的 initialize 方法中,对父类进行元编程,动态定义 Module 的方法,这样就能在模型中进行调用了。

class I18nDecorator < Module
  def initialize(*attrs)
    super() do
      attrs.each do |attr|
        define_method attr do
          send("#{attr}_#{I18n.locale}")
        end
      end
    end
  end
end

现在,当任何 User 类的实例调用 name 或者 position 的方法时,就会被 I18nDecorator 动态转发给 name_cn 或者 position_en 的属性上了。

就这样,一共十几行代码,我们完成了一个 Rails 项目简单的动态 I18n 的功能,以后需要增加模型或属性的时候,都只需要 include 这一行代码就可以了。