From 8dc066186fad700c4ccab81ac439eea2811a37cb Mon Sep 17 00:00:00 2001 From: florent Date: Mon, 27 May 2013 18:10:15 +0700 Subject: [PATCH 1/2] add domain name based i18n --- .../templates/config/initializers/refinery/i18n.rb.erb | 4 ++++ lib/refinery/i18n-filter.rb | 9 ++++++--- lib/refinery/i18n.rb | 8 ++++++++ lib/refinery/i18n/configuration.rb | 4 +++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb b/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb index 31750b0..1f4b64a 100644 --- a/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb +++ b/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb @@ -12,4 +12,8 @@ Refinery::I18n.configure do |config| # config.frontend_locales = <%= Refinery::I18n.config.frontend_locales.inspect %> # config.locales = <%= Refinery::I18n.config.locales.inspect %> + + # config.domain_name_enabled = <%= Refinery::I18n.config.domain_name_enabled.inspect %> + + # config.domains_locales = <%= Refinery::I18n.domains_locales.inspect %> end diff --git a/lib/refinery/i18n-filter.rb b/lib/refinery/i18n-filter.rb index 2668a7f..f6d78eb 100644 --- a/lib/refinery/i18n-filter.rb +++ b/lib/refinery/i18n-filter.rb @@ -2,8 +2,11 @@ module RoutingFilter class RefineryLocales < Filter def around_recognize(path, env, &block) - if ::Refinery::I18n.url_filter_enabled? - if path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)} + if ::Refinery::I18n.url_filter_enabled? || ::Refinery::I18n.domain_name_enabled? + if ::Refinery::I18n.domain_name_enabled? + hostname = env['HTTP_HOST'].sub(/:\d+$/, '') + ::I18n.locale = ::Refinery::I18n.domains_locales[hostname] || ::Refinery::I18n.default_frontend_locale + elsif path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)} path.sub! %r(^/(([a-zA-Z\-_])*)(?=/|$)) do ::I18n.locale = $1 '' @@ -24,7 +27,7 @@ def around_generate(params, &block) yield.tap do |result| result = result.is_a?(Array) ? result.first : result - if ::Refinery::I18n.url_filter_enabled? and + if ::Refinery::I18n.url_filter_enabled? and !::Refinery::I18n.domain_name_enabled? and locale != ::Refinery::I18n.default_frontend_locale and result !~ %r{^/(#{Refinery::Core.backend_route}|wymiframe)} result.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" } diff --git a/lib/refinery/i18n.rb b/lib/refinery/i18n.rb index d51327d..a042ee4 100644 --- a/lib/refinery/i18n.rb +++ b/lib/refinery/i18n.rb @@ -61,6 +61,14 @@ def url_filter_enabled? def has_locale?(locale) config.locales.has_key?(locale.try(:to_sym)) end + + def domain_name_enabled? + config.domain_name_enabled + end + + def domain_name_for_locale(locale) + config.domains_locales.invert[locale] || false + end end require 'refinery/i18n/engine' diff --git a/lib/refinery/i18n/configuration.rb b/lib/refinery/i18n/configuration.rb index fe39cc8..a551b75 100644 --- a/lib/refinery/i18n/configuration.rb +++ b/lib/refinery/i18n/configuration.rb @@ -4,7 +4,7 @@ module I18n config_accessor :current_locale, :default_locale, :default_frontend_locale, :enabled, :fallbacks_enabled, :frontend_locales, :locales, - :url_filter_enabled + :url_filter_enabled, :domain_name_enabled, :domains_locales self.enabled = true self.default_locale = :en @@ -14,5 +14,7 @@ module I18n self.frontend_locales = [self.default_frontend_locale] self.locales = self.built_in_locales self.url_filter_enabled = true + self.domain_name_enabled = false + self.domains_locales = {"www.mysite.com"=>:en, "www.mon-site.fr"=>:fr} end end From d2b13e1497d3eec4cbd53ab0cc8d47f73baf1add Mon Sep 17 00:00:00 2001 From: benxao Date: Mon, 27 May 2013 18:14:13 +0700 Subject: [PATCH 2/2] add domain name based i18n --- .../templates/config/initializers/refinery/i18n.rb.erb | 4 ++++ lib/refinery/i18n-filter.rb | 9 ++++++--- lib/refinery/i18n.rb | 8 ++++++++ lib/refinery/i18n/configuration.rb | 4 +++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb b/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb index 31750b0..1f4b64a 100644 --- a/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb +++ b/lib/generators/refinery/templates/config/initializers/refinery/i18n.rb.erb @@ -12,4 +12,8 @@ Refinery::I18n.configure do |config| # config.frontend_locales = <%= Refinery::I18n.config.frontend_locales.inspect %> # config.locales = <%= Refinery::I18n.config.locales.inspect %> + + # config.domain_name_enabled = <%= Refinery::I18n.config.domain_name_enabled.inspect %> + + # config.domains_locales = <%= Refinery::I18n.domains_locales.inspect %> end diff --git a/lib/refinery/i18n-filter.rb b/lib/refinery/i18n-filter.rb index 2668a7f..f6d78eb 100644 --- a/lib/refinery/i18n-filter.rb +++ b/lib/refinery/i18n-filter.rb @@ -2,8 +2,11 @@ module RoutingFilter class RefineryLocales < Filter def around_recognize(path, env, &block) - if ::Refinery::I18n.url_filter_enabled? - if path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)} + if ::Refinery::I18n.url_filter_enabled? || ::Refinery::I18n.domain_name_enabled? + if ::Refinery::I18n.domain_name_enabled? + hostname = env['HTTP_HOST'].sub(/:\d+$/, '') + ::I18n.locale = ::Refinery::I18n.domains_locales[hostname] || ::Refinery::I18n.default_frontend_locale + elsif path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)} path.sub! %r(^/(([a-zA-Z\-_])*)(?=/|$)) do ::I18n.locale = $1 '' @@ -24,7 +27,7 @@ def around_generate(params, &block) yield.tap do |result| result = result.is_a?(Array) ? result.first : result - if ::Refinery::I18n.url_filter_enabled? and + if ::Refinery::I18n.url_filter_enabled? and !::Refinery::I18n.domain_name_enabled? and locale != ::Refinery::I18n.default_frontend_locale and result !~ %r{^/(#{Refinery::Core.backend_route}|wymiframe)} result.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" } diff --git a/lib/refinery/i18n.rb b/lib/refinery/i18n.rb index d51327d..a042ee4 100644 --- a/lib/refinery/i18n.rb +++ b/lib/refinery/i18n.rb @@ -61,6 +61,14 @@ def url_filter_enabled? def has_locale?(locale) config.locales.has_key?(locale.try(:to_sym)) end + + def domain_name_enabled? + config.domain_name_enabled + end + + def domain_name_for_locale(locale) + config.domains_locales.invert[locale] || false + end end require 'refinery/i18n/engine' diff --git a/lib/refinery/i18n/configuration.rb b/lib/refinery/i18n/configuration.rb index fe39cc8..a551b75 100644 --- a/lib/refinery/i18n/configuration.rb +++ b/lib/refinery/i18n/configuration.rb @@ -4,7 +4,7 @@ module I18n config_accessor :current_locale, :default_locale, :default_frontend_locale, :enabled, :fallbacks_enabled, :frontend_locales, :locales, - :url_filter_enabled + :url_filter_enabled, :domain_name_enabled, :domains_locales self.enabled = true self.default_locale = :en @@ -14,5 +14,7 @@ module I18n self.frontend_locales = [self.default_frontend_locale] self.locales = self.built_in_locales self.url_filter_enabled = true + self.domain_name_enabled = false + self.domains_locales = {"www.mysite.com"=>:en, "www.mon-site.fr"=>:fr} end end