Commit da3b4c3f71f0197b50524e2a617b547087251c67

Authored by Larissa Reis
1 parent b9800000

Adds support for domain specific custom locales

  Support for custom locales for hosted environments using the
  multitenancy support and noosfero domains. Some environments need
  personalized translations and this hopefully offers a clean way to do
  it based on domains as well.

  Related to MR 195

(ActionItem3113)
app/controllers/application_controller.rb
@@ -127,6 +127,9 @@ class ApplicationController < ActionController::Base @@ -127,6 +127,9 @@ class ApplicationController < ActionController::Base
127 127
128 # TODO: move this logic somewhere else (Domain class?) 128 # TODO: move this logic somewhere else (Domain class?)
129 def detect_stuff_by_domain 129 def detect_stuff_by_domain
  130 + # Sets text domain based on request host for custom internationalization
  131 + FastGettext.text_domain = Domain.custom_locale(request.host)
  132 +
130 @domain = Domain.find_by_name(request.host) 133 @domain = Domain.find_by_name(request.host)
131 if @domain.nil? 134 if @domain.nil?
132 @environment = Environment.default 135 @environment = Environment.default
app/models/domain.rb
@@ -92,4 +92,11 @@ class Domain < ActiveRecord::Base @@ -92,4 +92,11 @@ class Domain < ActiveRecord::Base
92 @hosting = {} 92 @hosting = {}
93 end 93 end
94 94
  95 + # Detects a domain's custom text domain chain if available based on a domain
  96 + # served on multitenancy configuration or a registered domain.
  97 + def self.custom_locale(domainname)
  98 + domain = Noosfero::MultiTenancy.mapping[domainname] || domainname[/(.*?)\./,1]
  99 + FastGettext.translation_repositories.keys.include?(domain) ? domain : FastGettext.default_text_domain
  100 + end
  101 +
95 end 102 end
lib/noosfero/i18n.rb
@@ -7,6 +7,7 @@ class Object @@ -7,6 +7,7 @@ class Object
7 end 7 end
8 8
9 9
  10 +# Adds custom locales for a whole environment
10 custom_locale_dir = Rails.root.join('custom_locales', Rails.env) 11 custom_locale_dir = Rails.root.join('custom_locales', Rails.env)
11 repos = [] 12 repos = []
12 if File.exists?(custom_locale_dir) 13 if File.exists?(custom_locale_dir)
@@ -22,3 +23,15 @@ end @@ -22,3 +23,15 @@ end
22 23
23 FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos 24 FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos
24 FastGettext.default_text_domain = 'noosfero' 25 FastGettext.default_text_domain = 'noosfero'
  26 +
  27 +# Adds custom locales for specific domains; Domains are identified by the
  28 +# sequence before the first dot, while tenants are identified by schema name
  29 +hosted_environments = Noosfero::MultiTenancy.mapping.values
  30 +hosted_environments += Domain.all.map { |domain| domain.name[/(.*?)\./,1] } if Domain.table_exists?
  31 +
  32 +hosted_environments.uniq.each do |env|
  33 + custom_locale_dir = Rails.root.join('custom_locales', env)
  34 + if File.exists?(custom_locale_dir)
  35 + FastGettext.add_text_domain(env, :type => :chain, :chain => [FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)] + repos)
  36 + end
  37 +end