noosfero | Adds support for domain specific custom locales

Daniela Feitosa gitlab at gitlab.com
Sat Feb 7 11:37:39 BRST 2015


Daniela Feitosa pushed to refs/heads/stable-1.0 at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>

Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/1cbfc9376fbe73615e63e140bb3ca63bfd02b9e9">1cbfc937</a> by Daniela Feitosa
Adds support for domain specific custom locales

Patch for commits:
da3b4c3f71f0197b50524e2a617b547087251c67
ece6c5630ff06d35f466c7fc0cdbb2296a23c587
b6108f3c8d9033375aaa5c5fce0d71bc839dad21

See merge request !376

- - - - -


Changes:

=====================================
INSTALL.locales.md
=====================================
--- /dev/null
+++ b/INSTALL.locales.md
@@ -0,0 +1,29 @@
+Using custom locales
+====================
+
+Personalized translations go into the `config/custom_locales/` directory.
+Custom locales can be identified by the rails environment, schema name in a
+multitenancy setup or domain name until the first dot (e.g env1.coop.br for the
+example below).
+
+Currently, the only filename prefix for the localization file which is
+processed is "environment". For instance, a POT file would be called
+"environment.pot".
+
+The structure of an environment named env1 with custom translations for both
+Portuguese and Spanish and an environment named env2 with custom Russian
+translation would be:
+
+    config/
+      custom_locales/
+        env1/
+          environment.pot
+          pt/
+            environment.po
+          es/
+            environment.po
+        env2/
+          environment.pot
+          ru/
+            environment.po
+

=====================================
app/controllers/application_controller.rb
=====================================
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -127,6 +127,9 @@ class ApplicationController < ActionController::Base
 
   # TODO: move this logic somewhere else (Domain class?)
   def detect_stuff_by_domain
+    # Sets text domain based on request host for custom internationalization
+    FastGettext.text_domain = Domain.custom_locale(request.host)
+
     @domain = Domain.find_by_name(request.host)
     if @domain.nil?
       @environment = Environment.default

=====================================
app/models/domain.rb
=====================================
--- a/app/models/domain.rb
+++ b/app/models/domain.rb
@@ -92,4 +92,11 @@ class Domain < ActiveRecord::Base
     @hosting = {}
   end
 
+  # Detects a domain's custom text domain chain if available based on a domain
+  # served on multitenancy configuration or a registered domain.
+  def self.custom_locale(domainname)
+    domain = Noosfero::MultiTenancy.mapping[domainname] || domainname[/(.*?)\./,1]
+    FastGettext.translation_repositories.keys.include?(domain) ? domain : FastGettext.default_text_domain
+  end
+
 end

=====================================
lib/noosfero/i18n.rb
=====================================
--- a/lib/noosfero/i18n.rb
+++ b/lib/noosfero/i18n.rb
@@ -7,7 +7,8 @@ class Object
 end
 
 
-custom_locale_dir = Rails.root.join('custom_locales', Rails.env)
+# Adds custom locales for a whole environment
+custom_locale_dir = Rails.root.join('config', 'custom_locales', Rails.env)
 repos = []
 if File.exists?(custom_locale_dir)
   repos << FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)
@@ -22,3 +23,15 @@ end
 
 FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos
 FastGettext.default_text_domain = 'noosfero'
+
+# Adds custom locales for specific domains; Domains are identified by the
+# sequence before the first dot, while tenants are identified by schema name
+hosted_environments = Noosfero::MultiTenancy.mapping.values
+hosted_environments += Domain.all.map { |domain| domain.name[/(.*?)\./,1] } if Domain.table_exists?
+
+hosted_environments.uniq.each do |env|
+  custom_locale_dir = Rails.root.join('config', 'custom_locales', env)
+  if File.exists?(custom_locale_dir)
+    FastGettext.add_text_domain(env, :type => :chain, :chain => [FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)] + repos)
+  end
+end

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150207/72ff5cca/attachment-0001.html>


More information about the Noosfero-dev mailing list