Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domain Name based i18n #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions lib/refinery/i18n-filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
''
Expand All @@ -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}" }
Expand Down
8 changes: 8 additions & 0 deletions lib/refinery/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion lib/refinery/i18n/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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