问题
In the blog module for refinery CMS there are some strings in the source code that I would like to translate. When I override the view for _post.html.erb of the blog module there is code like this inside:
<%= content_tag(:span,
"#{t('by', :scope => 'refinery.blog.posts.show')} #{@post.author.username}",
:class => "blog_author") if @post.author.present? %>
I would like to localize the "by" string, so that in the blog, the default english "By authorname" is replaced by a phrase in another language.
Now, I have modified the en.yml and hr.yml localisation files in the rails config/locales directory and added the translation. However, this makes no effect to the strings displayed on my page.
I have tried setting the config.i18n.default_locale variable in config/application.rb to :en and to my desired language but this also accomplishes nothing.
The furthest I came was that if I change the config.current_locale variable in initializers/refinery/i18n.rb to :de for example, that has the effect of translating the admin interface for refinery and its blog module. And yet, the strings in the blog entries remain the same.
I have also added a yml file with translations for my locale in the gems library of the refinery blog component, but it still does not work.
Any help on how to translate the strings in the refinery blog module would be appreciated. I have searched the internet on how to translate refinery, but haven't managed to find any specific information for the translation of the the blog component, only general guides for rails, which don't seem to help with the refinery blog.
I am using the following gems versions:
- rails (4.2.4)
- rails-i18n (4.0.7)
- railties (4.2.4)
- refinerycms (3.0.0 1d13007)
- refinerycms-acts-as-indexed (2.0.1)
- refinerycms-authentication-devise (1.0.4)
- refinerycms-blog (3.0.0 5ee8336)
- refinerycms-core (3.0.0 1d13007)
- refinerycms-i18n (3.0.1 ff93ad9)
- refinerycms-images (3.0.0 1d13007)
- refinerycms-pages (3.0.0 1d13007)
- refinerycms-resources (3.0.0 1d13007)
- refinerycms-search (3.0.0 aa8098c)
- refinerycms-settings (3.0.0)
- refinerycms-wymeditor (1.0.6) ...
Thank you in advance!
P.S
I have addded a translation string to hr.yml and set the
config.i18n.default_locale = :hr
config.i18n.locale = :hr
in application.rb.
In the rails console I get:
*I18n.locale* => :hr,
I18n.translate('hello') => "Pozdrav svima"
But, when I start the application, in the rails server messages there is:
*Parameters: {"locale"=>:en}*
and no translations work... Why?
回答1:
According to the rails locale guide the application locale can be set based on the url. For localhost, the locale was set to english. In order to change this, one can modify the application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :set_locale
def set_locale
I18n.locale = I18n.default_locale
end
end
来源:https://stackoverflow.com/questions/34417556/refinery-cms-on-ruby-rails-translating-localising-the-strings-in-the-blog-mod