Ruby on rails 如何修复:i18n始终转换为默认语言环境
我正在尝试用i18n实现Rails应用程序的国际化。我用两种语言做了一些小测试:英语和法语 我遇到的问题是i18n总是转换为默认语言环境。因此,如果是英语,一切都将是英语,法语也是如此 以下是我尝试过的: config/initializers/locales.rbRuby on rails 如何修复:i18n始终转换为默认语言环境,ruby-on-rails,ruby,rails-i18n,Ruby On Rails,Ruby,Rails I18n,我正在尝试用i18n实现Rails应用程序的国际化。我用两种语言做了一些小测试:英语和法语 我遇到的问题是i18n总是转换为默认语言环境。因此,如果是英语,一切都将是英语,法语也是如此 以下是我尝试过的: config/initializers/locales.rb # Permitted locales available for the application I18n.available_locales = [:en, :fr] class ApplicationController &
# Permitted locales available for the application
I18n.available_locales = [:en, :fr]
class ApplicationController < ActionController::Base
def default_url_options
{ locale: I18n.locale }
end
end
module LanguageApp
class Application < Rails::Application
...
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
config.i18n.default_locale = :en
# I change the default locale here to :fr or :en
end
end
root to: "home#index"
get '/:locale/about' => 'about#index'
get '/:locale' => 'home#index'
About us page. <%= t(:about) %>
This is the homepage. <%= t(:welcome) %>
app/controllers/application\u controller.rb
# Permitted locales available for the application
I18n.available_locales = [:en, :fr]
class ApplicationController < ActionController::Base
def default_url_options
{ locale: I18n.locale }
end
end
module LanguageApp
class Application < Rails::Application
...
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
config.i18n.default_locale = :en
# I change the default locale here to :fr or :en
end
end
root to: "home#index"
get '/:locale/about' => 'about#index'
get '/:locale' => 'home#index'
About us page. <%= t(:about) %>
This is the homepage. <%= t(:welcome) %>
我将我的yml文件组织如下:
config/locales/views/about/en.yml
en:
about: "This page is about us."
fr:
about: "Cette page est à propos de nous."
en:
welcome: "Hello world"
fr:
welcome: "Bonjour le monde"
config/locales/views/about/fr.yml
en:
about: "This page is about us."
fr:
about: "Cette page est à propos de nous."
en:
welcome: "Hello world"
fr:
welcome: "Bonjour le monde"
config/locales/views/home/en.yml
en:
about: "This page is about us."
fr:
about: "Cette page est à propos de nous."
en:
welcome: "Hello world"
fr:
welcome: "Bonjour le monde"
config/locales/views/home/fr.yml
en:
about: "This page is about us."
fr:
about: "Cette page est à propos de nous."
en:
welcome: "Hello world"
fr:
welcome: "Bonjour le monde"
最后,我的观点是:
app/views/about/index.html.erb
# Permitted locales available for the application
I18n.available_locales = [:en, :fr]
class ApplicationController < ActionController::Base
def default_url_options
{ locale: I18n.locale }
end
end
module LanguageApp
class Application < Rails::Application
...
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
config.i18n.default_locale = :en
# I change the default locale here to :fr or :en
end
end
root to: "home#index"
get '/:locale/about' => 'about#index'
get '/:locale' => 'home#index'
About us page. <%= t(:about) %>
This is the homepage. <%= t(:welcome) %>
这3个URL为我提供了相同的内容,因此:fr语言环境实际上不起作用(它返回与:en相同的翻译)
同样适用于
localhost:3000/en/about
localhost:3000/fr/about
我还在rails控制台中进行了尝试:
> I18n.t(:welcome, :en)
"Hello world"
> I18n.t(:welcome, :fr)
"Hello world"
首先设置请求的区域设置:
class ApplicationController < ActionController::Base
around_action :switch_locale
def switch_locale(&action)
I18n.with_locale(params[:locale] || I18n.default_locale, &action)
end
def default_url_options
{ locale: I18n.locale }
end
end
然后在视图中使用隐式查找:
<h1><%= t(.welcome) %></h1>
这解析了
主页的键。欢迎
当一个新的区域设置来自paramsI时,我看不出您正在设置/更改I18n.locale。我忘了提到我是如何更改它的。我正在编辑我的问题。谢谢,现在可以了。我缺少的是switch_locale方法,之后一切都很好。谢谢