jurou 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04c2db979a4a93629b8f6c13408b00f7eaae0210
4
+ data.tar.gz: c908429daec9a6f2899c62a226fc764453026b12
5
+ SHA512:
6
+ metadata.gz: fdf2e26d7af88716b5120084f04ad3fb049b7030bf6be1c4dfdca1a65c769e8723f602771ae2f5cf6ab730b464471db58f0df88eda6facff3e139a582f6cae1e
7
+ data.tar.gz: bb0f1be76859e11460504326da72435a86abf8c09ae6d7f9d3c1a882d2a9f0bf65372a32ecb91043711aa2f17cf18e8de950e5554088ec1ef73afb5adf7979c4
@@ -0,0 +1,218 @@
1
+ # jurou / 翻譯蒟蒻
2
+ [![Gem Version](https://badge.fury.io/rb/jurou.svg)](https://badge.fury.io/rb/jurou)
3
+ [![Code Climate](https://codeclimate.com/github/jodeci/jurou/badges/gpa.svg)](https://codeclimate.com/github/jodeci/jurou)
4
+ [![Test Coverage](https://codeclimate.com/github/jodeci/jurou/badges/coverage.svg)](https://codeclimate.com/github/jodeci/jurou/coverage)
5
+ [![Build Status](https://travis-ci.org/jodeci/jurou.svg?branch=master)](https://travis-ci.org/jodeci/jurou)
6
+
7
+ A collection of i18n related view helpers for Rails. Work in progress.
8
+
9
+ Lets say we have a model `Book`. `Book.genre` is a pre defined list of book genres, such as `fantasy, detective, romance, history, manga`.
10
+
11
+ Most developers stick to English (at least the Latin alphabet) for such values. The thing is, your app more than often requires you to display them as `奇幻, 推理, 羅曼史, 歷史, 漫畫` or whatever your native language.
12
+
13
+ Although there's the handy I18n API for localization, turns out this was still a messier process than I imagined. To keep things DRY I put together *jurou* for myself.
14
+
15
+ ## Installation
16
+
17
+ Tested on Ruby 2.3.1 and Rails 5.
18
+
19
+ ```
20
+ # Gemfile
21
+ gem "jurou"
22
+ ```
23
+
24
+ ```
25
+ $ bundle install
26
+ ```
27
+
28
+ Generate the locale yml file. Defaults to your application's `default_locale` if `--locale` not specified.
29
+
30
+ ```
31
+ $ rails generate jurou:install
32
+ $ rails generate jurou:install --locale ja
33
+ ```
34
+
35
+ ## Examples
36
+
37
+ Fill in the details in the generated locale file:
38
+
39
+ ```
40
+ # config/locale/jurou.zh-TW.yml
41
+ zh-TW:
42
+ jurou:
43
+ book:
44
+ genre:
45
+ fantasy: 奇幻
46
+ detective: 推理
47
+ romance: 羅曼史
48
+ history: 歷史
49
+ manga: 漫畫
50
+
51
+ app_title: 翻譯蒟蒻
52
+
53
+ page_titles:
54
+ books:
55
+ _label: 我的書櫃
56
+ index: 書籍列表
57
+ edit: 修改書籍
58
+ admin/sales:
59
+ index: 銷售紀錄
60
+
61
+ activerecord:
62
+ attributes:
63
+ book:
64
+ title: 書名
65
+ author: 作者
66
+ genre: 類別
67
+ ```
68
+ ### jr\_collection
69
+
70
+ Use `jr_collection` in your form template:
71
+
72
+ ```
73
+ # app/views/books/_form.html.slim
74
+ = simple_form_for @book do |f|
75
+ = f.input :genre, collection: jr_collection(:genre, :book)
76
+ ```
77
+
78
+ *jurou* will then generate the collection hash for the form helper, resulting in the following HTML:
79
+
80
+ ```
81
+ <select>
82
+ <option value="fantasy">奇幻</option>
83
+ <option value="detective">推理</option>
84
+ <option value="romance">羅曼史</option>
85
+ <option value="history">歷史</option>
86
+ <option value="manga">漫畫</option>
87
+ </select>
88
+ ```
89
+
90
+ ### jr\_attribute
91
+
92
+ `jr_attribute` simply outputs the corresponding translation of the attribute. Shorthand `jr_attr` also available.
93
+
94
+ ```
95
+ jr_attribute :author, :book
96
+ => "作者"
97
+ ```
98
+
99
+ ### jr\_value
100
+
101
+ `jr_value` is only useful when you need to get the translation for the attribute value itself.
102
+
103
+ ```
104
+ jr_value :genre, @book.genre, :book
105
+ => "推理"
106
+ ```
107
+
108
+ ### jr\_table\_row, jr\_table\_row\_translate\_value
109
+
110
+ Or, if you're lazy enough like me, there's also `jr_table_row` and `jr_table_row_translate_value` which takes advantage of `jr_attribute` and `jr_value` to make a quick and dirty table display. Shorthand `jr_row` and `jr_row_val` also available.
111
+
112
+ ```
113
+ # app/views/books/show.html.slim
114
+ table
115
+ = jr_table_row :title, @book.title, :book
116
+ = jr_table_row :author, @book.author, :book
117
+ = jr_table_row_translate_value :genre, @book.genre, :book
118
+ ```
119
+
120
+ This will produce the following HTML:
121
+
122
+ ```
123
+ <table>
124
+ <tr>
125
+ <th>書名</th>
126
+ <td>神探伽利略</td>
127
+ </tr>
128
+ <tr>
129
+ <th>作者</th>
130
+ <td>東野圭吾</td>
131
+ </tr>
132
+ <tr>
133
+ <th>類別</th>
134
+ <td>推理</td>
135
+ </tr>
136
+ </table>
137
+ ```
138
+
139
+ #### Be more lazy!
140
+
141
+ For the above helpers, you can omit passing `:book` all together if you are following Rails naming convention:
142
+
143
+ ```
144
+ jr_row :title, @book.title
145
+ => <tr><th>作者</th><td>神探伽利略</td></tr>
146
+
147
+ jr_row_val :genre, @book.genre
148
+ => <tr><th>類別</th><td>推理</td></tr>
149
+
150
+ jr_attr :author
151
+ => 作者
152
+
153
+ jr_value :genre, @book.genre
154
+ => 推理
155
+
156
+ jr_collection :genre
157
+ => { fantasy: "奇幻", detective: "推理", romance: "羅曼史", history: "歷史", manga: "漫畫" }
158
+ ```
159
+
160
+ #### More laziness with shikigami
161
+
162
+ If you are using *[shikigami](https://github.com/jodeci/shikigami)*, *jurou* will fallback to the `current_object` whenever possible, so you can just write this and be done:
163
+
164
+ ```
165
+ jr_row :title
166
+ => <tr><th>作者</th><td>神探伽利略</td></tr>
167
+
168
+ jr_row_val :genre
169
+ => <tr><th>類別</th><td>推理</td></tr>
170
+ ```
171
+
172
+ ### jr\_page\_title
173
+
174
+ `jr_page_title` generates the page title based on the current controller and action. It will fallback to your app title when there is no match.
175
+
176
+ ```
177
+ # app/views/layout/application.html.slim
178
+ title = jr_page_title
179
+
180
+ # BooksController#index
181
+ => "書籍列表 | 翻譯蒟蒻"
182
+
183
+ # MoviesController#index
184
+ => "翻譯蒟蒻"
185
+ ```
186
+ ### jr\_content\_for_page\_title
187
+
188
+ You can further customize the title with `jr_content_for_page_title`, or `jr_title`
189
+ for short.
190
+
191
+ ```
192
+ # app/views/books/edit.html.slim
193
+ = jr_content_for_page_title("神探伽利略")
194
+
195
+ # BooksController#edit
196
+ => "神探伽利略 | 修改書籍 | 翻譯蒟蒻"
197
+ ```
198
+
199
+ ### jr\_simple\_title
200
+ Use `jr_simple_title` when you need to manually get a page title, instead of relying on the black magic of `jr_page_title`. The app title will not be included. Comes in handy when building dropdown menus.
201
+
202
+ ```
203
+ # defaults to the current controller/action
204
+ jr_simple_title
205
+ => "書籍列表"
206
+
207
+ # general label for a controller, same as jr_simple_title(:books, :_label)
208
+ jr_simple_title(:books)
209
+ => "我的書櫃"
210
+
211
+ # specify controller and action
212
+ jr_simple_title(:books, :index)
213
+ => "書籍列表"
214
+
215
+ # controller with namespace
216
+ jr_simple_title("admin/sales", :index)
217
+ => "銷售管理"
218
+ ```
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require "rails/generators/base"
3
+ module Jurou
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("templates", __FILE__)
6
+ desc "Creates a locale file for your application"
7
+ class_option :locale, default: Rails.application.config.i18n.default_locale
8
+
9
+ def self.source_root
10
+ @source_root ||= File.join(File.dirname(__FILE__), "templates")
11
+ end
12
+
13
+ def copy_locale
14
+ copy_file "locale.yml", "config/locales/jurou.#{options[:locale]}.yml"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ #zh-TW:
2
+ # jurou:
3
+ # book:
4
+ # genre:
5
+ # fantasy: 奇幻
6
+ # detective: 推理
7
+ # romance: 羅曼史
8
+ # history: 歷史
9
+ # manga: 漫畫
10
+ #
11
+ # app_title: 翻譯蒟蒻
12
+ #
13
+ # page_titles:
14
+ # books:
15
+ # _label: 我的書櫃
16
+ # index: 書籍列表
17
+ #
18
+ # activerecord:
19
+ # attributes:
20
+ # book:
21
+ # title: 書名
22
+ # author: 作者
23
+ # genre: 類別
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require "jurou/version"
3
+ require "jurou/engine"
4
+ require "jurou/railtie"
5
+ module Jurou
6
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "rails/engine"
3
+ require "shikigami"
4
+ module Jurou
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ require "rails/railtie"
3
+ require "jurou/view_helpers"
4
+ module Jurou
5
+ class Railtie < ::Rails::Railtie
6
+ initializer "jurou.view_helpers" do
7
+ ActiveSupport.on_load(:action_view) { include Jurou::ViewHelpers }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Jurou
3
+ VERSION = "0.2.13"
4
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+ module Jurou
3
+ module ViewHelpers
4
+ def jr_page_title(app_title = "jurou.app_title", divider = "|")
5
+ "#{@jr_title}#{jr_page_title_by_controller_action(app_title, divider)}"
6
+ end
7
+
8
+ def jr_content_for_page_title(text = nil, divider = "|")
9
+ @jr_title = "#{text} #{divider} " if text
10
+ nil
11
+ end
12
+ alias jr_title jr_content_for_page_title
13
+
14
+ def jr_simple_title(controller = nil, action = nil)
15
+ unless action
16
+ if controller
17
+ action = :_label
18
+ else
19
+ controller = controller_path
20
+ action = action_name
21
+ end
22
+ end
23
+ I18n.t(jr_page_title_translation_key(controller, action))
24
+ end
25
+
26
+ def jr_collection(attribute, model = nil)
27
+ jr_init_model(model)
28
+ I18n.t("jurou.#{@_model}.#{attribute}").invert
29
+ end
30
+
31
+ def jr_table_row(attribute, value = nil, model = nil)
32
+ jr_init_model(model)
33
+ content_tag :tr do
34
+ concat content_tag :th, jr_attribute(attribute, @_model)
35
+ concat content_tag :td, jr_init_value(attribute, value)
36
+ end
37
+ end
38
+ alias jr_row jr_table_row
39
+
40
+ def jr_table_row_translate_value(attribute, value = nil, model = nil)
41
+ jr_init_model(model)
42
+ content_tag :tr do
43
+ concat content_tag :th, jr_attribute(attribute, @_model)
44
+ concat content_tag :td, jr_value(attribute, value, @_model)
45
+ end
46
+ end
47
+ alias jr_row_val jr_table_row_translate_value
48
+
49
+ def jr_attribute(attribute, model = nil)
50
+ jr_init_model(model)
51
+ I18n.t("activerecord.attributes.#{@_model}.#{attribute}")
52
+ end
53
+ alias jr_attr jr_attribute
54
+
55
+ def jr_value(attribute, value = nil, model = nil)
56
+ jr_init_model(model)
57
+ I18n.t("jurou.#{@_model}.#{attribute}.#{jr_init_value(attribute, value)}")
58
+ end
59
+
60
+ private
61
+
62
+ def jr_init_model(model)
63
+ @_model =
64
+ if model
65
+ model
66
+ elsif params[:id] and current_object
67
+ current_object.model_name.param_key
68
+ else
69
+ controller_name.singularize
70
+ end
71
+ end
72
+
73
+ def jr_init_value(attribute, value)
74
+ value || current_object.send(attribute)
75
+ end
76
+
77
+ def jr_page_title_by_controller_action(app_title, divider)
78
+ "#{t(jr_page_title_translation_key, raise: true)} #{divider} #{t(app_title)}"
79
+ rescue
80
+ t(app_title)
81
+ end
82
+
83
+ def jr_page_title_translation_key(controller = controller_path, action = action_name)
84
+ :"jurou.page_titles.#{controller}.#{action}"
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jurou
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.13
5
+ platform: ruby
6
+ authors:
7
+ - Tsehau Chao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shikigami
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ description: collection of i18n helpers for rails app
56
+ email:
57
+ - jodeci@5xruby.tw
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/generators/jurou/install_generator.rb
64
+ - lib/generators/jurou/templates/locale.yml
65
+ - lib/jurou.rb
66
+ - lib/jurou/engine.rb
67
+ - lib/jurou/railtie.rb
68
+ - lib/jurou/version.rb
69
+ - lib/jurou/view_helpers.rb
70
+ homepage: https://github.com/jodeci/jurou
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: i18n view helpers
94
+ test_files: []