aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Donnet2018-03-22 12:25:59 +0100
committerGitHub2018-03-22 12:25:59 +0100
commit9ae51de7d821aa2d0277fdab3aaeb5f32642a0ca (patch)
treee52a0279a387cce531d892d9c3d1c5c3c654558f
parent7be83f4b8c0af5950400e00aa77851e98cc778f9 (diff)
parentac69f1295251a62bf617f8e31dfd772d44854cb3 (diff)
downloadchouette-core-9ae51de7d821aa2d0277fdab3aaeb5f32642a0ca.tar.bz2
Merge pull request #256 from af83/enhanced-i18n
Proposed i18n enhancements
-rw-r--r--app/views/layouts/navigation/_page_header.html.slim2
-rw-r--r--config/initializers/i18n.rb159
-rw-r--r--config/locales/en.yml1
-rw-r--r--config/locales/fr.yml1
4 files changed, 162 insertions, 1 deletions
diff --git a/app/views/layouts/navigation/_page_header.html.slim b/app/views/layouts/navigation/_page_header.html.slim
index 49f56544b..bdc4bdfed 100644
--- a/app/views/layouts/navigation/_page_header.html.slim
+++ b/app/views/layouts/navigation/_page_header.html.slim
@@ -12,7 +12,7 @@
h1 = yield :page_header_title
- else
- if defined?(resource_class)
- h1 = t("#{resource_class.model_name.name.underscore.gsub('/', '.').pluralize}.#{params[:action]}.title")
+ h1 = resource_class.t_action(params[:action])
.col-lg-3.col-md-4.col-sm-5.col-xs-5.text-right
.page-action
diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb
new file mode 100644
index 000000000..02d42f899
--- /dev/null
+++ b/config/initializers/i18n.rb
@@ -0,0 +1,159 @@
+module I18n
+ class << self
+ def translate_with_fallback key, options={}, original=nil
+ options[:locale] ||= I18n.locale
+ begin
+ self.translate_without_fallback(key, {raise: true}.update(options))
+ rescue => e
+ split = key.to_s.split('.')
+ if split.size <= 2
+ translate_without_fallback original || key, options
+ else
+ v = split.pop
+ v2 = split.pop
+ split.pop if v2 == "default"
+ split << "default" << v
+ new_key = split.join('.')
+ translate_with_fallback new_key, options, original || key
+ end
+ end
+ end
+ alias_method_chain :translate, :fallback
+ alias_method :t, :translate
+ end
+
+ def self.tc(key, params={})
+ self.t('label_with_colon', label: key.t(params)).html_safe
+ end
+
+ def self.tmf(key, params={})
+ model, col = key.split "."
+ begin
+ self.t "activerecord.attributes.#{key}", {raise: true}.update(params)
+ rescue
+ begin
+ self.t "activerecord.attributes.common.#{col}", {raise: true}.update(params)
+ rescue
+ begin
+ self.t "simple_form.labels.#{key}", {raise: true}.update(params)
+ rescue
+ "activerecord.attributes.#{key}".t params
+ end
+ end
+ end
+ end
+
+ def self.tmfc(key, params={})
+ self.t('label_with_colon', label: self.tmf(key, params)).html_safe
+ end
+
+ def self.missing_keys_logger
+ @@my_logger ||= Logger.new("#{Rails.root}/log/missing_keys.log")
+ end
+
+ def self.log_missing_key key, params={}
+ missing_keys_logger.info "key: '#{key}', locale: '#{I18n.locale}', params: #{params}"
+ end
+
+ def self.t_with_default(key, params={})
+ begin
+ self.t(key, {raise: true}.update(params))
+ rescue
+ if Rails.env.development?
+ log_missing_key key, params
+ "<span class='label label-danger' title='#{self.t(key, params)}'>!</span>#{key.split('.').last}".html_safe
+ else
+ key.split('.').last
+ end
+ end
+ end
+end
+
+module EnhancedI18n
+ def t(params={})
+ I18n.t_with_default(self, params)
+ end
+
+ def tc(params={})
+ I18n.tc(self, params)
+ end
+
+ def tmf(params={})
+ I18n.tmf(self, params)
+ end
+
+ def tmfc(params={})
+ I18n.tmfc(self, params)
+ end
+end
+
+module EnhancedTimeI18n
+ def l(params={})
+ I18n.l(self, params)
+ end
+end
+
+class Symbol
+ include EnhancedI18n
+end
+
+class String
+ include EnhancedI18n
+end
+
+class Time
+ include EnhancedTimeI18n
+end
+
+class DateTime
+ include EnhancedTimeI18n
+end
+
+class Date
+ include EnhancedTimeI18n
+end
+
+class ActiveRecord::Base
+ # Human name of the class (plural)
+ def self.t opts={}
+ "activerecord.models.#{i18n_key}".t({count: 2}.update(opts))
+ end
+
+ # Human name of the class (singular)
+ def self.ts opts={}
+ self.t({count: 1}.update(opts))
+ end
+
+ # Human name of the class (with comma)
+ def self.tc(params={})
+ I18n.tc(i18n_key, params)
+ end
+
+ # Human name of the attribute
+ def self.tmf(attribute, params={})
+ I18n.tmf "#{i18n_key}.#{attribute}", params
+ end
+
+ # Translate the given action on the model, with default
+ def self.t_action(action, params={})
+ key = case action.to_sym
+ when :create
+ :new
+ when :update
+ :edit
+ else
+ action
+ end
+
+ begin
+ I18n.translate_without_fallback "#{i18n_key.pluralize}.actions.#{key}", ({raise: true}.update(params))
+ rescue
+ I18n.translate_without_fallback "actions.#{key}", params
+ end
+ end
+
+ private
+ def self.i18n_key
+ model_name.to_s.underscore.gsub('/', '_')
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8af8067db..05d21a1f2 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2,6 +2,7 @@ en:
"true": "Yes"
"false": "No"
"unknown": "Unknown"
+ label_with_colon: "%{label}: "
time:
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index e1f52ff55..733ba05b9 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -2,6 +2,7 @@ fr:
"true": "Oui"
"false": "Non"
"unknown": "Non précisé"
+ label_with_colon: "%{label} : "
time:
formats: