i18n_rails_helpers 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6b2f04d8cc2a83a85df37f146256516f2095dd7a
4
- data.tar.gz: b0c33d11110e084d74062d842822f042d0e59b66
2
+ SHA256:
3
+ metadata.gz: f7cac95e65c93521ee679616d514393c8fb0c7db16dbb284a0e4aa3d8d9f2860
4
+ data.tar.gz: fe5f13d1e2ba4f152e394adf9b1baadb7e4d4a3edc2667b551328d0c92bec359
5
5
  SHA512:
6
- metadata.gz: 4ee7552f09a4a83705014c5b458240be6407332795fa6ed7925f12020f7f0672d8e5954a8326c0d62d7e764db2c5ce60ddb3987033b0190b22b0b35b63a3d3da
7
- data.tar.gz: a273b5cf78607e2cde6cf94323cdc73acb22b8392ca92746ea476480b06f6db285ab58977396682f923ea2437c6e3466b412bde1c9bfa4309d63e65180a8f16a
6
+ metadata.gz: b2896f9666bfecf52e6f22964001a4f0bd9bf002e1ab256913a26ac617592affdfa102958f13a33125b75516f152e4063bad8099d6ed48347e479ad829e0a728
7
+ data.tar.gz: 045aecc3d6f2d2c08b0c454d8b645ef69556a024e01e580d3551f6f4eac383ce3ca844b3d18a2964a6d4a6fbea3377be27cf9cc6426659a560ced922b585fe7f
data/README.md CHANGED
@@ -1,27 +1,182 @@
1
- I18nRailsHelpers
2
- ================
1
+ # I18nRailsHelpers
3
2
 
4
3
  [![Build Status](https://secure.travis-ci.org/huerlisi/i18n_rails_helpers.png)](http://travis-ci.org/huerlisi/i18n_rails_helpers)
5
4
 
6
5
  Rails i18n view helpers for things like crud actions, models and and attributes.
7
6
 
8
- Install
9
- =======
7
+ - [Install](#install)
8
+ - [Examples](#examples)
9
+ - [View Helpers](#view-helpers)
10
+ - [t_attr](#t_attr)
11
+ - [t_title](#t_title)
12
+ - [t_action](#t_action)
13
+ - [t_confirm_delete](#t_confirm_delete)
14
+ - [t_select_prompt](#t_select_prompt)
15
+ - [Controller Helpers](#controller-helpers)
16
+ - [redirect_notice](#redirect_notice)
17
+ - [t_attr](#t_attr-1)
18
+ - [t_title](#t_title-1)
19
+ - [t_action](#t_action-1)
20
+ - [Model helpers](#model-helpers)
21
+ - [enum_attribute_t methods](#enum_attribute_t-methods)
22
+ - [t_enum](#t_enum)
23
+ - [Required translations in locales (`config/locales/zz.yml`)](#required-translations-in-locales-configlocaleszzyml)
24
+ - [License](#license)
10
25
 
11
- In Rails simply add
26
+ ## Install
12
27
 
13
- gem 'i18n_rails_helpers'
28
+ In Rails simply add this to your Gemfile:
14
29
 
15
- Example
16
- =======
30
+ ```ruby
31
+ gem 'i18n_rails_helpers'
32
+ ```
17
33
 
18
- t_attr('first_name') # 'Vorname' # when called in patients_controller views
19
- t_model # 'Konto' # when called in patients_controller views
20
- t_title('delete') # 'Konto löschen' # when called in accounts_controller views
21
- t_action('index') # 'Liste'
22
- t_confirm_delete(@account) # 'Konto Kasse wirklich löschen'
34
+ ## Examples
23
35
 
24
- License
25
- =======
36
+ ### View Helpers
37
+
38
+ #### t_attr
39
+
40
+ ```ruby
41
+ t_attr('first_name', Patient) # en: 'First name' de: 'Vorname' - when called from views of any controller
42
+ t_attr('first_name') # en: 'First name' de: 'Vorname' - when called in patients_controller views
43
+ t_attr(:first_name) # en: 'First name' de: 'Vorname' - can also be called with symbols
44
+ ```
45
+
46
+ #### t_title
47
+
48
+ ```ruby
49
+ t_title(:index) # en: "%{model} index" , de: "%{model} Liste"
50
+ t_title(:edit) # en: "Edit %{model}", de: "%{model} bearbeiten"
51
+ t_title(:update) # en: "Edit %{model}", de: "%{model} bearbeiten"
52
+ t_title(:show) # en: "Show %{model}", de: "%{model} anzeigen"
53
+ t_title(:new) # en: "New %{model}", de: "%{model} erfassen"
54
+ t_title(:create) # en: "New %{model}", de: "%{model} erfassen"
55
+ t_title(:delete) # en: "Delete %{model}", de: "%{model} löschen"
56
+ t_title(:destroy) # en: "Delete %{model}", de: "%{model} löschen"
57
+ ```
58
+
59
+ #### t_action
60
+
61
+ ```ruby
62
+ t_action(:index) # en: "Index", de: "Liste"
63
+ t_action(:edit) # en: "Edit", de: "Bearbeiten"
64
+ t_action(:update) # en: "Edit", de: "Bearbeiten"
65
+ t_action(:show) # en: "Show", de: "Anzeigen"
66
+ t_action(:new) # en: "New", de: "Erfassen"
67
+ t_action(:create) # en: "New", de: "Erfassen"
68
+ t_action(:delete) # en: "Delete", de: "Löschen"
69
+ t_action(:destroy) # en: "Delete", de: "Löschen"
70
+ t_action(:cancel) # en: "Abbrechen", de: "Abbrechen"
71
+ t_action(:back) # en: "Back", de: "Zurück"
72
+ t_action(:previous) # en: "Previous", de: "Zurück"
73
+ t_action(:next) # en: "Next", de: "Weiter"
74
+ ```
75
+
76
+ #### t_confirm_delete
77
+
78
+ ```ruby
79
+ t_confirm_delete(@account) # en: 'Really delete account Kasse?' de: 'Konto Kasse wirklich löschen?'
80
+ ```
81
+
82
+ #### t_select_prompt
83
+
84
+ ```ruby
85
+ t_select_prompt(@account) # en: 'Select Account' de: 'Konto auswählen'
86
+ ```
87
+
88
+ ### Controller Helpers
89
+
90
+ #### redirect_notice
91
+
92
+ ```ruby
93
+ def create
94
+ # ...
95
+ redirect_to some_path, redirect_notice # => 'Klient erstellt.'
96
+ redirect_to some_path, redirect_notice(@client) # => 'Klient Example Client erstellt.'
97
+ end
98
+
99
+ def update
100
+ # ...
101
+ redirect_to some_path, redirect_notice # => 'Klient geändert.'
102
+ redirect_to some_path, redirect_notice(@client) # => 'Klient Example Client geändert.'
103
+ end
104
+
105
+
106
+ def destroy
107
+ # ...
108
+ redirect_to some_path, redirect_notice # => 'Klient gelöscht.'
109
+ redirect_to some_path, redirect_notice(@client) # => 'Klient Example Client gelöscht.'
110
+ end
111
+ ```
112
+
113
+ #### t_attr
114
+
115
+ - [t_attr](#t_attr)
116
+
117
+ #### t_title
118
+
119
+ - [t_title](#t_title)
120
+
121
+ #### t_action
122
+
123
+ - [t_action](#t_action)
124
+
125
+ ### Model helpers
126
+
127
+ #### enum_attribute_t methods
128
+
129
+ Automaticly generated enum attribut translation methods.
130
+
131
+ *[Required translations](#required-translations-in-locales-configlocaleszzyml)*.
132
+
133
+ ```ruby
134
+ # Client model
135
+ enum gender: { undefined: 0, female: 1, male: 2, xy: 3 }
136
+
137
+ # in use
138
+ Client.first.gender # => 'female'
139
+ Client.first.gender_t # de: => 'Frau', en: => 'Woman'
140
+ Client.first.genders_t # => { undefined: 'Nicht definiert', female: 'Frau', male: 'Mann' }
141
+ ```
142
+
143
+ #### t_enum
144
+
145
+ ```ruby
146
+ # Client model
147
+ enum gender: { undefined: 0, female: 1, male: 2, xy: 3 }
148
+
149
+ Client.first.gender # => 'female'
150
+ Client.first.t_enum(:gender) # de: => 'Frau', en: => 'Women'
151
+ Client.first.t_enum(:gender, :male) # de: => 'Mann', en: => 'Men'
152
+ ```
153
+
154
+ ### Required translations in locales (`config/locales/zz.yml`)
155
+
156
+ ```yaml
157
+ ---
158
+ de:
159
+ activerecord:
160
+ attributes:
161
+ client:
162
+ genders:
163
+ undefined: Nicht definiert
164
+ female: Frau
165
+ male: Mann
166
+ ---
167
+ en:
168
+ activerecord:
169
+ attributes:
170
+ client:
171
+ genders:
172
+ undefined: Not defined
173
+ female: Women
174
+ male: Man
175
+ ---
176
+ fr:
177
+ # ...
178
+ ```
179
+
180
+ ## License
26
181
 
27
182
  Released under the MIT license.
@@ -1,10 +1,10 @@
1
1
  module I18nHelpers
2
2
  # Returns translated identifier
3
3
  def t_page_head
4
- if params[:id] and resource
5
- return "%s %s" % [t_title, resource.to_s]
4
+ if params[:id] && resource
5
+ "#{t_title} #{resource}"
6
6
  else
7
- return t_title
7
+ t_title
8
8
  end
9
9
  end
10
10
 
@@ -18,12 +18,8 @@ module I18nHelpers
18
18
  # t_attr('first_name') => 'Vorname' # when called in patients_controller views
19
19
  #
20
20
  def t_attr(attribute, model = nil)
21
- if model.is_a? Class
22
- model_class = model
23
- elsif model.nil?
24
- model_class = controller_name.classify.constantize
25
- end
26
- model_class.human_attribute_name(attribute)
21
+ model ||= controller_name.classify.constantize
22
+ model.human_attribute_name(attribute)
27
23
  end
28
24
 
29
25
  # Returns translated name for the given +model+.
@@ -37,18 +33,16 @@ module I18nHelpers
37
33
  # t_model => 'Konto' # when called in patients_controller views
38
34
  #
39
35
  def t_model(model = nil)
40
- if model.is_a? ActiveModel::Naming
41
- return model.model_name.human
42
- elsif model.class.is_a? ActiveModel::Naming
43
- return model.class.model_name.human
44
- elsif model.is_a? Class
45
- model_name = model.name.underscore
46
- elsif model.nil?
47
- model_name = controller_name.singularize
48
- else
49
- model_name = model.class.name.underscore
50
- end
51
- I18n::translate(model_name, :scope => [:activerecord, :models])
36
+ return model.model_name.human if model.is_a? ActiveModel::Naming
37
+ return model.class.model_name.human if model.class.is_a? ActiveModel::Naming
38
+ model_key = if model.is_a? Class
39
+ model.name.underscore
40
+ elsif model.nil?
41
+ controller_name.singularize
42
+ else
43
+ model.class.name.underscore
44
+ end
45
+ I18n.t("activerecord.models.#{model_key}")
52
46
  end
53
47
 
54
48
  # Returns translated title for current +action+ on +model+.
@@ -67,19 +61,15 @@ module I18nHelpers
67
61
  # #{controller_name}.#{action}.title
68
62
  #
69
63
  # Example:
70
- # t_title('new', Account') => 'Konto anlegen'
71
- # t_title('delete') => 'Konto löschen' # when called in accounts_controller views
72
- # t_title => 'Konto ändern' # when called in accounts_controller edit view
64
+ # t_title('new', Account) => 'Konto anlegen'
65
+ # t_title('delete') => 'Konto löschen' # when called in accounts_controller views
66
+ # t_title => 'Konto ändern' # when called in accounts_controller edit view
73
67
  #
74
68
  def t_title(action = nil, model = nil)
75
- action ||= action_name
76
- if model
77
- context = model.name.pluralize.underscore
78
- else
79
- context = controller_name.underscore
80
- end
81
-
82
- I18n::translate("#{context}.#{action}.title", :default => [:"crud.title.#{action}"], :model => t_model(model))
69
+ model_key = model&.model_name&.i18n_key || model&.class&.model_name&.i18n_key ||
70
+ controller_name.underscore
71
+ I18n.t("#{model_key}.#{action || action_name}.title",
72
+ default: [:"crud.title.#{action || action_name}"], model: t_model(model))
83
73
  end
84
74
  alias :t_crud :t_title
85
75
 
@@ -95,8 +85,7 @@ module I18nHelpers
95
85
  # t_action => 'Ändern' # when called in an edit view
96
86
  #
97
87
  def t_action(action = nil, model = nil)
98
- action ||= action_name
99
- I18n::translate(action, :scope => 'crud.action', :model => t_model(model))
88
+ I18n.t("crud.action.#{action || action_name}", model: t_model(model))
100
89
  end
101
90
 
102
91
  # Returns translated deletion confirmation for +record+.
@@ -107,7 +96,7 @@ module I18nHelpers
107
96
  # t_confirm_delete(@account) => 'Konto Kasse wirklich löschen'
108
97
  #
109
98
  def t_confirm_delete(record)
110
- I18n::translate('messages.confirm_delete', :model => t_model(record), :record => record.to_s)
99
+ I18n.t('messages.confirm_delete', model: t_model(record), record: record.to_s)
111
100
  end
112
101
 
113
102
  # Returns translated drop down field prompt for +model+.
@@ -118,6 +107,6 @@ module I18nHelpers
118
107
  # t_select_prompt(Account) => 'Konto auswählen'
119
108
  #
120
109
  def t_select_prompt(model = nil)
121
- I18n::translate('messages.select_prompt', :model => t_model(model))
110
+ I18n.t('messages.select_prompt', model: t_model(model))
122
111
  end
123
112
  end
@@ -4,6 +4,10 @@ de:
4
4
  select_prompt: "%{model} auswählen"
5
5
 
6
6
  crud:
7
+ notices:
8
+ create: "%{model} %{record}erstellt."
9
+ update: "%{model} %{record}geändert."
10
+ destroy: "%{model} %{record}gelöscht."
7
11
  title:
8
12
  index: "%{model} Liste"
9
13
  edit: "%{model} bearbeiten"
@@ -4,6 +4,10 @@ en:
4
4
  select_prompt: "Select %{model}"
5
5
 
6
6
  crud:
7
+ notices:
8
+ create: "%{model} %{record}created."
9
+ update: "%{model} %{record}changed."
10
+ destroy: "%{model} %{record}deleted."
7
11
  title:
8
12
  index: "%{model} index"
9
13
  edit: "Edit %{model}"
@@ -0,0 +1,17 @@
1
+
2
+ module I18nRailsHelpers
3
+ module ControllerHelpers
4
+ delegate :t_attr, :t_model, :t_action, to: :helpers
5
+
6
+ # returns a redirect notice for create, update and destroy actions
7
+ #
8
+ # Example in ClientsController:
9
+ # redirect_to some_path, redirect_notice # => 'Klient geändert.'
10
+ # redirect_to some_path, redirect_notice(@client) # => 'Klient Example Client geändert.'
11
+ #
12
+ def redirect_notice(record = nil)
13
+ { notice: I18n.t("crud.notices.#{action_name}", model: helpers.t_model,
14
+ record: record.present? ? "#{record} " : '') }
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,6 @@
1
1
  require 'rails'
2
+ require 'i18n_rails_helpers/model_helpers'
3
+ require 'i18n_rails_helpers/controller_helpers'
2
4
 
3
5
  module I18nRailsHelpers
4
6
  class Engine < Rails::Engine
@@ -6,6 +8,12 @@ module I18nRailsHelpers
6
8
  ActionView::Base.send :include, I18nHelpers
7
9
  ActionView::Base.send :include, ContextualLinkHelpers
8
10
  ActionView::Base.send :include, ListLinkHelpers
11
+ ActionController::Base.class_eval { include ControllerHelpers }
12
+
13
+ ActiveRecord::Base.class_eval do
14
+ include ModelHelpers
15
+ after_initialize :define_enum_t_methods
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -0,0 +1,40 @@
1
+ module I18nRailsHelpers
2
+ module ModelHelpers
3
+ # enum attrubute_t and attributes_t return translated enum values
4
+ #
5
+ # Example:
6
+ # in the Client model
7
+ # enum gender: { undefined: 0, female: 1, male: 2 }
8
+ # in use
9
+ # Client.first.gender # => 'female'
10
+ # Client.first.gender_t # => 'Frau'
11
+ # Client.first.genders_t # => { undefined: 'Nicht definiert', female: 'Frau', male: 'Mann' }
12
+ #
13
+ # Requires:
14
+ # locale key: activerecord.attributes.#{model_name}.#{enum}.#{enum_value_key}
15
+ # eg.: activerecord.attributes.client.genders.female # => 'Frau'
16
+ #
17
+ def define_enum_t_methods
18
+ defined_enums.each do |enum_attr, values|
19
+ self.class.send(:define_method, "#{enum_attr}_t") { t_enum(enum_attr) }
20
+ self.class.send(:define_method, "#{enum_attr.pluralize}_t") do
21
+ t_enum_values(enum_attr, values)
22
+ end
23
+ end
24
+ end
25
+
26
+ def t_enum_values(enum_attr, values)
27
+ values.map do |enum_val_key, _|
28
+ [enum_val_key.to_sym, t_enum(enum_attr, enum_val_key)]
29
+ end.to_h
30
+ end
31
+
32
+ # translate enum fields value
33
+ def t_enum(enum_attr, enum_value = nil)
34
+ enum_value ||= public_send(enum_attr)
35
+ I18n.t(
36
+ "activerecord.attributes.#{model_name.i18n_key}.#{enum_attr.to_s.pluralize}.#{enum_value}"
37
+ )
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module I18nRailsHelpers
2
- VERSION = "2.0.1"
2
+ VERSION = '2.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_rails_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Huerlimann (CyT)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-15 00:00:00.000000000 Z
11
+ date: 2018-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
19
+ version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,8 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description: Rails i18n view helpers for things like crud actions, models and and
84
- attributes.
83
+ description: Rails i18n view helpers for things like crud actions, models and attributes.
85
84
  email:
86
85
  - simon.huerlimann@cyt.ch
87
86
  executables: []
@@ -105,7 +104,9 @@ files:
105
104
  - config/locales/en.yml
106
105
  - lib/boot_form_builder.rb
107
106
  - lib/i18n_rails_helpers.rb
107
+ - lib/i18n_rails_helpers/controller_helpers.rb
108
108
  - lib/i18n_rails_helpers/engine.rb
109
+ - lib/i18n_rails_helpers/model_helpers.rb
109
110
  - lib/i18n_rails_helpers/version.rb
110
111
  homepage: https://github.com/huerlisi/i18n_rails_helpers
111
112
  licenses:
@@ -127,9 +128,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  requirements: []
129
130
  rubyforge_project:
130
- rubygems_version: 2.2.2
131
+ rubygems_version: 2.7.6
131
132
  signing_key:
132
133
  specification_version: 4
133
134
  summary: I18n Rails helpers
134
135
  test_files: []
135
- has_rdoc: