hashie-extensions-i18n 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hashie-extensions-i18n.gemspec +27 -0
- data/lib/hashie/extensions/i18n.rb +61 -0
- data/lib/hashie/extensions/i18n/version.rb +7 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1193572059814563f4c48b89a01b0b4874ee6b74
|
4
|
+
data.tar.gz: 0ae44f1b28b36e0ea9df29700ed4cb227552cbaa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9bca6bc48a8db15cdae3f002edb80c91231abe9f5cd7f0ea26c5a7dc37429f262758af5609b79017de331922909888ccef15908372a77093c470c32a3368f5e7
|
7
|
+
data.tar.gz: 35c42d077e2e1534cd16a2cb1e59d99a2f5232b26bb66ba93410561e64055db764d1024d116cec07280ef16446fef04dc3dfe60dbcf11f08d8446f7004ee0090
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Feeligo www.feeligo.com
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Hashie::Extensions::I18n
|
2
|
+
|
3
|
+
Makes Hashie properties localizable
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
class Greeting < Hashie::Dash
|
10
|
+
localizable_property :hello, coerce: String
|
11
|
+
end
|
12
|
+
|
13
|
+
g = Greeting.new(hello: {en: "Hello", it: "Ciao"})
|
14
|
+
g.localized_hello(:en)
|
15
|
+
# => "Hello"
|
16
|
+
g.localized_hello(:it)
|
17
|
+
# => "Ciao"
|
18
|
+
g.localized_hello(:fr)
|
19
|
+
# => nil
|
20
|
+
```
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'hashie-extensions-i18n'
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Feeligo/hashie-extensions-i18n.
|
35
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hashie/extensions/i18n"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hashie/extensions/i18n/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hashie-extensions-i18n"
|
8
|
+
spec.version = Hashie::Extensions::I18n::VERSION
|
9
|
+
spec.authors = ["Davide Bonapersona"]
|
10
|
+
spec.email = ["tech@feeligo.com"]
|
11
|
+
spec.licenses = ["MIT"]
|
12
|
+
|
13
|
+
spec.summary = %q{Makes Hashie properties localizable}
|
14
|
+
spec.homepage = "https://github.com/Feeligo/hashie-extensions-i18n"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "hashie", "~> 3.0"
|
22
|
+
spec.add_dependency "i18n", "~> 0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
27
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "hashie/extensions/i18n/version"
|
2
|
+
|
3
|
+
module Hashie
|
4
|
+
module Extensions
|
5
|
+
module I18n
|
6
|
+
|
7
|
+
module Dash
|
8
|
+
def self.included base
|
9
|
+
# make sure we have coercion
|
10
|
+
base.send :include, Hashie::Extensions::Coercion
|
11
|
+
base.extend ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
def localizable_property property_name, options = {}
|
18
|
+
# remove :coerce option, use custom coercion instead
|
19
|
+
into = options.delete(:coerce)
|
20
|
+
# set the :default option to be an empty Hash
|
21
|
+
options[:default] = ::Hash.new
|
22
|
+
# define the property the usual way
|
23
|
+
property property_name, options
|
24
|
+
# value must be a Hash of {locale: value}
|
25
|
+
# keys are coerced to Symbols
|
26
|
+
# and values are coerced as specified by options[:coerce]
|
27
|
+
if into.nil?
|
28
|
+
coerce_key property_name, Hash[Symbol => Object]
|
29
|
+
else
|
30
|
+
coerce_key property_name, Hash[Symbol => into]
|
31
|
+
end
|
32
|
+
# define a localized accessor
|
33
|
+
define_method "localized_#{property_name}" do |*locales|
|
34
|
+
# get the property value (Hash or nil)
|
35
|
+
res = self.[](property_name)
|
36
|
+
return nil if res.nil? || res.empty?
|
37
|
+
# ensure locales is a flat Array
|
38
|
+
locales = [locales].flatten
|
39
|
+
# if no locale is provided, default to I18n.locale
|
40
|
+
locales = [::I18n.locale] if locales.empty?
|
41
|
+
# add the fallbacks of the provided locales
|
42
|
+
if ::I18n.respond_to?(:fallbacks)
|
43
|
+
locales.concat(locales.map{|l|
|
44
|
+
::I18n.fallbacks[l].drop(1)}.flatten)
|
45
|
+
end
|
46
|
+
# symbolize locales
|
47
|
+
locales.map!{|l| l.to_s.downcase.to_sym}
|
48
|
+
# try the specified locales in order
|
49
|
+
locales.each do |l|
|
50
|
+
return res[l] if res.has_key?(l)
|
51
|
+
end
|
52
|
+
# translation not found: return nil
|
53
|
+
nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashie-extensions-i18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Davide Bonapersona
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.3'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- tech@feeligo.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- hashie-extensions-i18n.gemspec
|
100
|
+
- lib/hashie/extensions/i18n.rb
|
101
|
+
- lib/hashie/extensions/i18n/version.rb
|
102
|
+
homepage: https://github.com/Feeligo/hashie-extensions-i18n
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Makes Hashie properties localizable
|
126
|
+
test_files: []
|