enumlingo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02b5b7d176faeef3781f6469c7ab0d45502346d50810a5b4b2ba2591b9edeb91
4
+ data.tar.gz: cdb9b65d590344cc24e658bbd30e385d39d6b3ec49ba27bb8efa7420347975b5
5
+ SHA512:
6
+ metadata.gz: d7ae82e05e3b4c2ff44a897970baa0aa0a8e6748eb0ad5f986e8dd1004787ea5952420ed183a3d9d3d3276782afad0bbcfdf57c1b5b745f7a29269fff316d254
7
+ data.tar.gz: 33397a7b74154abac5e017e9d9f5187a2ef48e120ed71493f1e4f55066193fce706aab21fd8ff6f26bbafcbc47edd19817ccdf6419828433026e7d8a9abc8874
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright iuhoay
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Enumlingo
2
+
3
+ Enumlingo is a simple gem that helps you translate enum values in your Rails app.
4
+
5
+ ## Usage
6
+
7
+ `extend Enumlingo` in your model and call enumlingo with the enums you want to translate.
8
+
9
+ ```ruby
10
+ class Product < ApplicationRecord
11
+ extend Enumlingo
12
+
13
+ enum status: %i[active inactive]
14
+ enum kind: %i[book food medical other]
15
+
16
+
17
+ enumlingo :status, :kind
18
+ end
19
+ ```
20
+
21
+ define the translations in your locale file
22
+
23
+ ```yaml
24
+ en:
25
+ activerecord:
26
+ attributes:
27
+ product:
28
+ status:
29
+ active: "Active"
30
+ inactive: "Inactive"
31
+ kind:
32
+ book: "Book"
33
+ food: "Food"
34
+ medical: "Medical"
35
+ other: "Other"
36
+
37
+ "zh-CN":
38
+ activerecord:
39
+ attributes:
40
+ product:
41
+ status:
42
+ active: "激活"
43
+ inactive: "未激活"
44
+ kind:
45
+ book: "书籍"
46
+ food: "食品"
47
+ medical: "医疗"
48
+ other: "其他"
49
+ ```
50
+
51
+ ### Value translation
52
+
53
+ ```ruby
54
+ product = Product.new(status: :active, kind: :book)
55
+ product.status_lingo # => "Active"
56
+ product.kind_lingo # => "Book"
57
+
58
+ I18n.locale = "zh-CN"
59
+ product.status_lingo # => "激活"
60
+ product.kind_lingo # => "书籍"
61
+ ```
62
+
63
+ ### Options translation
64
+
65
+ ```ruby
66
+ Product.statuses_lingo # => [["Active", "active"], ["Inactive", "inactive"]]
67
+ Product.kinds_lingo # => [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]]
68
+ ```
69
+
70
+ ### Form translation
71
+
72
+ ```ruby
73
+ <%= form_for @product do |f| %>
74
+ <%= f.select :status, Product.statuses_lingo %>
75
+ <%= f.select :kind, Product.kinds_lingo %>
76
+
77
+ ...
78
+ <% end %>
79
+ ```
80
+
81
+ ## Installation
82
+ Add this line to your application's Gemfile:
83
+
84
+ ```ruby
85
+ gem "enumlingo"
86
+ ```
87
+
88
+ And then execute:
89
+ ```bash
90
+ $ bundle
91
+ ```
92
+
93
+ Or install it yourself as:
94
+ ```bash
95
+ $ gem install enumlingo
96
+ ```
97
+
98
+ ## Contributing
99
+ Contribution directions go here.
100
+
101
+ ## License
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ module Enumlingo
2
+ def enumlingo(*attributes)
3
+ attributes.each do |attribute|
4
+ pluralized_attribute = attribute.to_s.pluralize
5
+
6
+ define_method "#{attribute}_lingo" do
7
+ value = send(attribute)
8
+ # return nil if value.nil?
9
+ I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}")
10
+ end
11
+
12
+ define_singleton_method "#{attribute.to_s.pluralize}_lingo" do
13
+ send(attribute.to_s.pluralize).map do |key, value|
14
+ [I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}"), key]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module Enumlingo
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Enumlingo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/enumlingo.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "enumlingo/version"
2
+ require "enumlingo/railtie"
3
+ require "enumlingo/helper"
4
+
5
+ module Enumlingo
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :enumlingo do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumlingo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - iuhoay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-11 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: '7.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.1'
27
+ description: Enumlingo is a lightweight Ruby gem designed to enhance Rails applications
28
+ by offering straightforward internationalization for enum fields. It simplifies
29
+ the process of translating enum values across different locales, ensuring a user-friendly,
30
+ multilingual experience in Rails applications.
31
+ email:
32
+ - iuhoay@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - MIT-LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/enumlingo.rb
41
+ - lib/enumlingo/helper.rb
42
+ - lib/enumlingo/railtie.rb
43
+ - lib/enumlingo/version.rb
44
+ - lib/tasks/enumlingo_tasks.rake
45
+ homepage: https://github.com/iuhoay/enumlingo
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ allowed_push_host: https://rubygems.org
50
+ homepage_uri: https://github.com/iuhoay/enumlingo
51
+ source_code_uri: https://github.com/iuhoay/enumlingo
52
+ changelog_uri: https://github.com/iuhoay/enumlingo/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.22
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Enumlingo is a lightweight Ruby gem designed to enhance Rails applications
72
+ by offering straightforward internationalization for enum fields. It simplifies
73
+ the process of translating enum values across different locales, ensuring a user-friendly,
74
+ multilingual experience in Rails applications.
75
+ test_files: []