开发者

Internationalizing whole text with markup in Rails 3

开发者 https://www.devze.com 2023-02-09 01:48 出处:网络
What\'s the best practice for internati开发者_如何学运维onalizing, say, a Terms of Service document in Rails 3? I can think of two options:

What's the best practice for internati开发者_如何学运维onalizing, say, a Terms of Service document in Rails 3? I can think of two options:

  • Creating a partial for each locale and choosing which one to load according to the current user's locale.
  • <li><%= I18n.t :tos_paragraph_1 %></li><li><%= I18n.t :tos_paragraph_2 %></li>

None of these seems like a good solution. Any ideas?


There are a few solutions, but if I were doing this for a production project, I would probably do something like the following:

  1. Create files for your translated Terms of Services in config/locales/terms/, naming them terms.en.html, replacing en with the locale for each translation and html with the format of the file (e.g. you could use Haml, Markdown, etc.).
  2. Create the following helper methods (put them in app/helpers/application_helper.rb to use them everywhere, but you can put them in whatever helper file you need/want):

    def localized_document_for(document, locale)
      raise ArgumentError.new('nil is not a valid document') if document.nil?
      raise I18n::InvalidLocale.new('nil is not a valid locale') if locale.nil?
      localized_document = path_for_localized_document(document, locale)
      raise MissingTranslationData unless File.exists?(localized_document)
      # If you're using Markdown, etc. replace with code to parse/format your document
      File.open(localized_document).readlines.join
    end
    
    def path_for_localized_document(document, locale)
      "#{Rails.root}/config/locales/#{document}/#{document}.#{locale.to_s}.html"
    end
    

Now, in your views, you can use localized_document_for('terms', I18n.locale) any time you need to get the contents of the Terms of Service in the user's language. Now the code you're using to fetch the document is DRY (you can easily fetch other documents by creating another directory in config/locales and changing the value of the document argument), and your translated documents are stored in their own directory and can easily be edited (and don't depend on YAML, which brings no value to storing a single document in a file).

Note that if you wanted to do it the "Rails 3 Way," you could use the I18n::Backend::Chain (see https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/chain.rb), and pass in I18n::Backend::Simple.new along with a custom backend that reads the files as necessary, but for a one-off deal I believe the helpers work sufficiently.


Creating a partial for each language is definitly undry.

The Rails way is the second option you suggest with a .yml for each language.

The doc provides great examples to let you organize the yml according to each page. Thus you have shortcuts for all your variables.

See http://guides.rubyonrails.org/i18n.html

0

精彩评论

暂无评论...
验证码 换一张
取 消