Rails 4 i18n,如何转换将子域用作语言环境的路由 [英] Rails 4 i18n, how to translate routes where subdomain is used for the locale

查看:109
本文介绍了Rails 4 i18n,如何转换将子域用作语言环境的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用子域来确定Rails 4网站中的语言环境。我已经按照我想要的方式与语言环境切换器一起工作了,但是现在我需要翻译路线,而且我不确定最好的处理方法。

I am using subdomains to determine the locale in a Rails 4 website. I have this working with a locale switcher exactly the way I want it but now I need to translate the routes and I'm not sure about the best way to proceed.

我已经看过 https://github.com/kwi/i18n_routing i18n路由gem,但这不适用于子域,它似乎通过添加/ locale来改变路径,这不是我所需要的。

I have looked the https://github.com/kwi/i18n_routing i18n routing gem but this doesn't work with subdomains, it seems to change the path by adding /locale which is not what I need.

Rails 4中的其他宝石似乎已经过时了。

Other gems seem to be out of date with Rails 4.

编辑

基本上我希望能够使用相同的视图助手,但要更改网址所选子域反映的是哪种语言。这是关于路线翻译的。

Basically I want to be able to use the same view helpers but have the urls change to use whatever language is reflected by the selected sub-domain. this is just about route translation.

我有可以使用的语言特定模板,可以生成特定于语言的导航模板,但我真的不必担心更改erb路径和网址erb标签

I have language specific templates which work and I can produce language spevific navigation templates but I would really like to not have to worry about changing erb path and url erb tags

结束编辑

routes.rb中的示例

A sample from routes.rb

scope module: 'countries', shallow: true do

  get 'south_american', to: 'south_america#index', as: :south_america
  scope module: 'south_america' do
    get 'south-america-weather', to: 'weather#index', as: :south_america_weather
    get 'south-america-gps-maps', to: 'gps#index', as: :south_america_gps
    get 'south-america-accommodation', to: 'hotels#index', as: :south_america_hotels
    get 'south-america-vacations', to: 'vacations#index', as: :south_america_vacations
    get 'south-america-facts', to: 'facts#index', as: :south_america_facts
  end

使用例如,south_america_hotels_path将为

Using south_america_hotels_path as an example will generate a url for


南美住宿

south-america-accommodation

$ b生成一个URL。
$ b

这很好,但是我将如何翻译,以便当我进入西班牙子域名south_america_hotels_path时将生成以下网址

which is great but how would I translate this so that when I am on the Spanish subdomain south_america_hotels_path will generate the following url


hoteles-en-sudamerica

hoteles-en-sudamerica

UPDATE

这对于网址而不是仅路径是如何工作的,以便
south_america_hotels_url会生成

Also how would this work for a url rather than just a path so that south_america_hotels_url will generate


en.some_site / south-america-commodation

en.some_site/south-america-accommodation

,当在西班牙子域上时,我会得到

and when on the spanish subdomain I would get


es.some_site / hoteles-en-sudamerica

es.some_site/hoteles-en-sudamerica

我很乐意使用yml文件进行网址翻译或定义其他路线,在routes.rb文件中,这是一个选项,但是我更愿意在routes.rb中定义URL,但是我无法找到一种方法为基于子域的路径/ URL选项提供相同的特定于语言的URL。

I am happy to use yml files for the translations for the urls or to define the additional routes in the routes.rb file, either is an option but I would prefer to define the url's in the routes.rb but I am unable to find a way to provide language specific urls for the same :as path/url option based on subdomain.

根据先前的答复进行更新和进一步澄清。

更改网址不是一个选择,它们需要匹配现有的URL。我只需要知道如何从视图助手的角度翻译它们,而不必更改视图助手。

Changing the url's is not an option, they need to match existing url's. I just need to know how to translate them from a view helper point of view without having to change the view helpers.

推荐答案

编辑:

此宝石是我推荐的方法。支持多种语言,而无需重新加载路线(我的示例没有)。您只需要做很少的更改,但它比下面的代码更加一致。 路由翻译器

This gem is my recommended way to do it. Supports multiple languages without reloading the routes (Which my example didn't). You have to change very few things but it's more consistent than the code below. route translator

原始答案(无效):

这对我有用,您可以尝试一下,例如在您的路线中输入:

This is what have worked for me, you can try it out, in your routes for example you can put:

AppName::Application.routes.draw do
    get "/#{I18n.t("index")}", :to => "pages#index", :as => "index"
    get "/#{I18n.t("contact")}", :to => "pages#contact", :as => "contact"
end

这将在您运行默认语言环境时为您提供路由该应用程序,但是在运行时中,当您更改语言环境时,您需要重新绘制路由才能获取新语言环境的路由。

this will get you the routes in your default locale when you run the app, however in runtime, when you change your locale you need to redraw your routes in order to get the routes for the new locale.

I18n.locale = :es #different locale
AppName::Application.reload_routes!

假设您的default_locale是:en ,示例将从以下路线出发:

Assuming your default_locale is :en, this example will take the first routes from:

en:
  index: "index-in-english"
  contact: "contact-in-english"

,当应用重新加载路线时,来自新语言环境的翻译,在此示例中,来自

and when the app reload the routes, it will take the translations from your new locale, in this example from

es:
  index: "index-in-spanish"
  contact: "contact-in-spanish"

通过这种方式,您将呈现页面# index和pages#contact,它只会更改路线,您无需触摸代码。希望这对您有帮助

This way you it will render pages#index and pages#contact and it'll only change the route, you don't have to touch your code. Hope this helps you

这篇关于Rails 4 i18n,如何转换将子域用作语言环境的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆