Commit 342be5f0702f4f297b2a4894ad51fba1452328a3

Authored by Antonio Terceiro
2 parents 124a7468 b6108f3c

Merge branch 'patch-custom-locales' into 'master'

Custom locales for personalized internationalization

Support for custom locales for hosted environments using the
multitenancy support and noosfero domains. There was a previous
commit for custom locales based on environment (see AI3113 and MR195),
but we also need it to work with a multitenancy system.

See merge request !376
INSTALL.locales.md 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +Using custom locales
  2 +====================
  3 +
  4 +Personalized translations go into the `config/custom_locales/` directory.
  5 +Custom locales can be identified by the rails environment, schema name in a
  6 +multitenancy setup or domain name until the first dot (e.g env1.coop.br for the
  7 +example below).
  8 +
  9 +Currently, the only filename prefix for the localization file which is
  10 +processed is "environment". For instance, a POT file would be called
  11 +"environment.pot".
  12 +
  13 +The structure of an environment named env1 with custom translations for both
  14 +Portuguese and Spanish and an environment named env2 with custom Russian
  15 +translation would be:
  16 +
  17 + config/
  18 + custom_locales/
  19 + env1/
  20 + environment.pot
  21 + pt/
  22 + environment.po
  23 + es/
  24 + environment.po
  25 + env2/
  26 + environment.pot
  27 + ru/
  28 + environment.po
  29 +
... ...
app/controllers/application_controller.rb
... ... @@ -127,6 +127,9 @@ class ApplicationController < ActionController::Base
127 127  
128 128 # TODO: move this logic somewhere else (Domain class?)
129 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 133 @domain = Domain.find_by_name(request.host)
131 134 if @domain.nil?
132 135 @environment = Environment.default
... ...
app/models/domain.rb
... ... @@ -92,4 +92,11 @@ class Domain < ActiveRecord::Base
92 92 @hosting = {}
93 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 102 end
... ...
lib/noosfero/i18n.rb
... ... @@ -9,7 +9,8 @@ class Object
9 9 end
10 10  
11 11  
12   -custom_locale_dir = Rails.root.join('custom_locales', Rails.env)
  12 +# Adds custom locales for a whole environment
  13 +custom_locale_dir = Rails.root.join('config', 'custom_locales', Rails.env)
13 14 repos = []
14 15 if File.exists?(custom_locale_dir)
15 16 repos << FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)
... ... @@ -29,3 +30,15 @@ end
29 30  
30 31 FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos
31 32 FastGettext.default_text_domain = 'noosfero'
  33 +
  34 +# Adds custom locales for specific domains; Domains are identified by the
  35 +# sequence before the first dot, while tenants are identified by schema name
  36 +hosted_environments = Noosfero::MultiTenancy.mapping.values
  37 +hosted_environments += Domain.all.map { |domain| domain.name[/(.*?)\./,1] } if Domain.table_exists?
  38 +
  39 +hosted_environments.uniq.each do |env|
  40 + custom_locale_dir = Rails.root.join('config', 'custom_locales', env)
  41 + if File.exists?(custom_locale_dir)
  42 + FastGettext.add_text_domain(env, :type => :chain, :chain => [FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)] + repos)
  43 + end
  44 +end
... ...