lazier 2.3.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/Lazier.html +34 -305
- data/doc/Lazier/Boolean.html +1 -1
- data/doc/Lazier/DateTime.html +1 -1
- data/doc/Lazier/DateTime/ClassMethods.html +1 -1
- data/doc/Lazier/Exceptions.html +1 -1
- data/doc/Lazier/Exceptions/Dump.html +1 -1
- data/doc/Lazier/Hash.html +1 -1
- data/doc/Lazier/I18n.html +404 -0
- data/doc/Lazier/Math.html +1 -1
- data/doc/Lazier/Math/ClassMethods.html +1 -1
- data/doc/Lazier/Object.html +1 -1
- data/doc/Lazier/Pathname.html +1 -1
- data/doc/Lazier/Settings.html +242 -77
- data/doc/Lazier/String.html +1 -1
- data/doc/Lazier/TimeZone.html +1 -1
- data/doc/Lazier/TimeZone/ClassMethods.html +1 -1
- data/doc/Lazier/Version.html +1 -1
- data/doc/_index.html +23 -8
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +73 -57
- data/doc/top-level-namespace.html +1 -1
- data/lib/lazier.rb +1 -27
- data/lib/lazier/i18n.rb +42 -0
- data/lib/lazier/settings.rb +25 -6
- data/lib/lazier/version.rb +2 -2
- data/spec/lazier/datetime_spec.rb +4 -4
- data/spec/lazier/i18n_spec.rb +63 -0
- data/spec/lazier/settings_spec.rb +2 -0
- data/spec/lazier_spec.rb +0 -34
- metadata +5 -1
@@ -103,7 +103,7 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on Tue Jan 29
|
106
|
+
Generated on Tue Jan 29 20:37:32 2013 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
108
|
0.8.3 (ruby-1.9.3).
|
109
109
|
</div>
|
data/lib/lazier.rb
CHANGED
@@ -12,6 +12,7 @@ require "r18n-desktop"
|
|
12
12
|
|
13
13
|
require "lazier/version" if !defined?(Lazier::Version)
|
14
14
|
require "lazier/exceptions"
|
15
|
+
require "lazier/i18n"
|
15
16
|
require "lazier/settings"
|
16
17
|
require "lazier/object"
|
17
18
|
require "lazier/boolean"
|
@@ -27,7 +28,6 @@ module Lazier
|
|
27
28
|
#
|
28
29
|
# @return [Settings] The settings for the extensions.
|
29
30
|
def self.settings
|
30
|
-
::Lazier.localize if !Lazier.localized?
|
31
31
|
::Lazier::Settings.instance
|
32
32
|
end
|
33
33
|
|
@@ -44,8 +44,6 @@ module Lazier
|
|
44
44
|
# @option pathname Extensions for path objects.
|
45
45
|
# @return [Settings] The settings for the extensions.
|
46
46
|
def self.load!(*what)
|
47
|
-
::Lazier.localize if !Lazier.localized?
|
48
|
-
|
49
47
|
what = ["object", "boolean", "string", "hash", "datetime", "math", "pathname"] if what.count == 0
|
50
48
|
what.collect! { |w| ::Lazier.send("load_#{w}") }
|
51
49
|
|
@@ -125,28 +123,4 @@ module Lazier
|
|
125
123
|
include ::Lazier::Pathname
|
126
124
|
end
|
127
125
|
end
|
128
|
-
|
129
|
-
# Get the list of available translation for the current locale.
|
130
|
-
#
|
131
|
-
# @return [R18N::Translation] The translation object.
|
132
|
-
def self.i18n
|
133
|
-
Lazier.localize if !Lazier.localized?
|
134
|
-
@i18n
|
135
|
-
end
|
136
|
-
|
137
|
-
# Set the current locale for messages.
|
138
|
-
#
|
139
|
-
# @param locale [String] The new locale. Default is the current system locale.
|
140
|
-
# @return [R18n::Translation] The new translation object.
|
141
|
-
def self.localize(locale = nil)
|
142
|
-
@i18n_locales_path ||= ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../locales/")
|
143
|
-
@i18n = R18n::I18n.new([locale, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path).t.lazier
|
144
|
-
end
|
145
|
-
|
146
|
-
# Check whether the i18n support have been enabled.
|
147
|
-
#
|
148
|
-
# @return [Boolean] Whether the i18n support have been enabled or not.
|
149
|
-
def self.localized?
|
150
|
-
@i18n.present?
|
151
|
-
end
|
152
126
|
end
|
data/lib/lazier/i18n.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Lazier
|
8
|
+
module I18n
|
9
|
+
# Setup all I18n translations.
|
10
|
+
#
|
11
|
+
# @param root [Symbol] The root level of translation.
|
12
|
+
# @param path [String] The path where the translation are stored.
|
13
|
+
def i18n_setup(root, path)
|
14
|
+
@i18n_root = root.to_sym
|
15
|
+
@i18n_locales_path = path
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get the list of available translation for the current locale.
|
19
|
+
#
|
20
|
+
# @return [R18N::Translation] The translation object.
|
21
|
+
def i18n
|
22
|
+
@i18n ||= i18n_load_locale(nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set the current locale for messages.
|
26
|
+
#
|
27
|
+
# @param locale [String] The new locale. Default is the current system locale.
|
28
|
+
# @return [R18n::Translation] The new translation object.
|
29
|
+
def i18n=(locale)
|
30
|
+
@i18n = i18n_load_locale(locale)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
# Loads a locale for messages.
|
35
|
+
#
|
36
|
+
# @param locale [Symbol] The new locale. Default is the current system locale.
|
37
|
+
# @return [R18n::Translation] The new translation object.
|
38
|
+
def i18n_load_locale(locale)
|
39
|
+
R18n::I18n.new([locale, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path.ensure_string).t.send(@i18n_root.ensure_string)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/lazier/settings.rb
CHANGED
@@ -11,12 +11,16 @@ module Lazier
|
|
11
11
|
# @attr [Hash] boolean_names String representations of booleans.
|
12
12
|
# @attr [Hash] date_names String representations of days and months.
|
13
13
|
# @attr [Hash] date_formats Custom date and time formats.
|
14
|
+
# @attr [R18n::Translation] i18n The translation object.
|
15
|
+
|
14
16
|
class Settings
|
15
17
|
attr_reader :format_number
|
16
18
|
attr_reader :boolean_names
|
17
19
|
attr_reader :date_names
|
18
20
|
attr_reader :date_formats
|
19
21
|
|
22
|
+
include Lazier::I18n
|
23
|
+
|
20
24
|
# Returns the singleton instance of the settings.
|
21
25
|
#
|
22
26
|
# @param force [Boolean] If to force recreation of the instance.
|
@@ -28,12 +32,27 @@ module Lazier
|
|
28
32
|
|
29
33
|
# Initializes a new settings object.
|
30
34
|
def initialize
|
35
|
+
self.i18n_setup(:lazier, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
|
36
|
+
self.setup
|
37
|
+
end
|
38
|
+
|
39
|
+
# Setups the current instance.
|
40
|
+
def setup
|
31
41
|
self.setup_format_number
|
32
42
|
self.setup_boolean_names
|
33
43
|
self.setup_date_formats
|
34
44
|
self.setup_date_names
|
35
45
|
end
|
36
46
|
|
47
|
+
# Set the current locale for messages.
|
48
|
+
#
|
49
|
+
# @param locale [String] The new locale. Default is the current system locale.
|
50
|
+
# @return [R18n::Translation] The new translation object.
|
51
|
+
def i18n=(locale)
|
52
|
+
super(locale)
|
53
|
+
self.setup
|
54
|
+
end
|
55
|
+
|
37
56
|
# Setups formatters for a number.
|
38
57
|
# @see Object#format_number
|
39
58
|
#
|
@@ -53,8 +72,8 @@ module Lazier
|
|
53
72
|
# @param false_name [String] The string representation of `false`. Defaults to `No`.
|
54
73
|
# @return [Hash] The new representations.
|
55
74
|
def setup_boolean_names(true_name = nil, false_name = nil)
|
56
|
-
true_name ||=
|
57
|
-
false_name ||=
|
75
|
+
true_name ||= self.i18n.boolean[0]
|
76
|
+
false_name ||= self.i18n.boolean[1]
|
58
77
|
@boolean_names = {true => true_name, false => false_name}
|
59
78
|
end
|
60
79
|
|
@@ -92,10 +111,10 @@ module Lazier
|
|
92
111
|
# @param short_days [Array] The abbreviated string representation of days.
|
93
112
|
# @return [Hash] The new representations.
|
94
113
|
def setup_date_names(long_months = nil, short_months = nil, long_days = nil, short_days = nil)
|
95
|
-
long_months =
|
96
|
-
short_months =
|
97
|
-
long_days =
|
98
|
-
short_days =
|
114
|
+
long_months = self.i18n.date.long_months if long_months.blank?
|
115
|
+
short_months = self.i18n.date.short_months if short_months.blank?
|
116
|
+
long_days = self.i18n.date.long_days if long_days.blank?
|
117
|
+
short_days = self.i18n.date.short_days if short_days.blank?
|
99
118
|
|
100
119
|
@date_names = { long_months: long_months, short_months: short_months, long_days: long_days, short_days: short_days }
|
101
120
|
end
|
data/lib/lazier/version.rb
CHANGED
@@ -13,9 +13,8 @@ describe Lazier::DateTime do
|
|
13
13
|
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
14
14
|
|
15
15
|
before(:all) do
|
16
|
-
|
17
|
-
::Lazier::Settings.instance
|
18
|
-
::Lazier.load!
|
16
|
+
Lazier.load!
|
17
|
+
::Lazier::Settings.instance.i18n = :en
|
19
18
|
end
|
20
19
|
|
21
20
|
describe ".days" do
|
@@ -206,8 +205,9 @@ describe Lazier::TimeZone do
|
|
206
205
|
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
207
206
|
|
208
207
|
before(:all) do
|
208
|
+
::Lazier::Settings.instance(true)
|
209
|
+
::Lazier::Settings.instance.i18n = :en
|
209
210
|
::Lazier.load!
|
210
|
-
::Lazier.localize(:en)
|
211
211
|
end
|
212
212
|
|
213
213
|
describe ".rationalize_offset" do
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
class Container
|
10
|
+
include Lazier::I18n
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Lazier::I18n do
|
14
|
+
let(:object) { Container.new }
|
15
|
+
|
16
|
+
describe "#i18n_setup" do
|
17
|
+
it "should set the root and the path" do
|
18
|
+
object.i18n_setup("ROOT", "PATH")
|
19
|
+
expect(object.instance_variable_get(:@i18n_root)).to eq(:ROOT)
|
20
|
+
expect(object.instance_variable_get(:@i18n_locales_path)).to eq("PATH")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#i18n" do
|
25
|
+
it "should call the private method if nothing is set" do
|
26
|
+
object.instance_variable_set(:@i18n, nil)
|
27
|
+
object.should_receive(:i18n_load_locale)
|
28
|
+
object.i18n
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#i18n=" do
|
33
|
+
it "should call the private method if nothing is set" do
|
34
|
+
object.should_receive(:i18n_load_locale).and_return("LOCALE")
|
35
|
+
object.i18n = :en
|
36
|
+
expect(object.instance_variable_get(:@i18n)).to eq("LOCALE")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#i18n_load_locale" do
|
42
|
+
it "should set using system locale if called without arguments" do
|
43
|
+
object.i18n_setup("ROOT", "PATH")
|
44
|
+
R18n::I18n.should_receive(:new).with([ENV["LANG"], R18n::I18n.system_locale].compact, "PATH").and_call_original
|
45
|
+
object.i18n = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should set the requested locale" do
|
49
|
+
object.i18n_setup("ROOT", "PATH")
|
50
|
+
R18n::I18n.should_receive(:new).with([:it, ENV["LANG"], R18n::I18n.system_locale].compact, "PATH").and_call_original
|
51
|
+
object.i18n = :it
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should call the root" do
|
55
|
+
Lazier.load!
|
56
|
+
t = Object.new
|
57
|
+
object.i18n_setup("ROOT", ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
|
58
|
+
R18n::I18n.any_instance.should_receive(:t).and_return(t)
|
59
|
+
t.should_receive("ROOT")
|
60
|
+
object.i18n = :it
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -13,6 +13,7 @@ describe Lazier::Settings do
|
|
13
13
|
|
14
14
|
before(:all) do
|
15
15
|
Lazier.load!
|
16
|
+
::Lazier::Settings.instance.i18n = :en
|
16
17
|
end
|
17
18
|
|
18
19
|
describe ".instance" do
|
@@ -108,6 +109,7 @@ describe Lazier::Settings do
|
|
108
109
|
|
109
110
|
describe "#setup_date_names" do
|
110
111
|
it "should save names for days and months" do
|
112
|
+
reference.i18n = :en
|
111
113
|
reference.setup_date_names
|
112
114
|
reference.setup_date_formats({sdn: "%B %b %A %a"})
|
113
115
|
|
data/spec/lazier_spec.rb
CHANGED
@@ -42,38 +42,4 @@ describe Lazier do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
46
|
-
describe ".i18n" do
|
47
|
-
it "should run localize if needed" do
|
48
|
-
Lazier.instance_variable_set(:@i18n, nil)
|
49
|
-
Lazier.should_receive(:localize)
|
50
|
-
Lazier.i18n
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe ".localize" do
|
55
|
-
it "should set the right locale path" do
|
56
|
-
Lazier.localize
|
57
|
-
expect(Lazier.instance_variable_get(:@i18n_locales_path)).to eq(File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../locales/"))
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should set using system locale if called without arguments" do
|
61
|
-
R18n::I18n.should_receive(:new).with([ENV["LANG"], R18n::I18n.system_locale].compact, File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../locales/")).and_call_original
|
62
|
-
Lazier.localize
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should set the requested locale" do
|
66
|
-
R18n::I18n.should_receive(:new).with([:it, ENV["LANG"], R18n::I18n.system_locale].compact, File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../locales/")).and_call_original
|
67
|
-
Lazier.localize(:it)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe ".localize?" do
|
72
|
-
it "should respect the value of the internal variable" do
|
73
|
-
Lazier.instance_variable_set(:@i18n, nil)
|
74
|
-
expect(Lazier.localized?).to be_false
|
75
|
-
Lazier.localize(:en)
|
76
|
-
expect(Lazier.localized?).to be_true
|
77
|
-
end
|
78
|
-
end
|
79
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -208,6 +208,7 @@ files:
|
|
208
208
|
- doc/Lazier/Exceptions.html
|
209
209
|
- doc/Lazier/Exceptions/Dump.html
|
210
210
|
- doc/Lazier/Hash.html
|
211
|
+
- doc/Lazier/I18n.html
|
211
212
|
- doc/Lazier/Math.html
|
212
213
|
- doc/Lazier/Math/ClassMethods.html
|
213
214
|
- doc/Lazier/Object.html
|
@@ -237,6 +238,7 @@ files:
|
|
237
238
|
- lib/lazier/datetime.rb
|
238
239
|
- lib/lazier/exceptions.rb
|
239
240
|
- lib/lazier/hash.rb
|
241
|
+
- lib/lazier/i18n.rb
|
240
242
|
- lib/lazier/math.rb
|
241
243
|
- lib/lazier/object.rb
|
242
244
|
- lib/lazier/pathname.rb
|
@@ -249,6 +251,7 @@ files:
|
|
249
251
|
- spec/lazier/boolean_spec.rb
|
250
252
|
- spec/lazier/datetime_spec.rb
|
251
253
|
- spec/lazier/hash_spec.rb
|
254
|
+
- spec/lazier/i18n_spec.rb
|
252
255
|
- spec/lazier/math_spec.rb
|
253
256
|
- spec/lazier/object_spec.rb
|
254
257
|
- spec/lazier/pathname_spec.rb
|
@@ -285,6 +288,7 @@ test_files:
|
|
285
288
|
- spec/lazier/boolean_spec.rb
|
286
289
|
- spec/lazier/datetime_spec.rb
|
287
290
|
- spec/lazier/hash_spec.rb
|
291
|
+
- spec/lazier/i18n_spec.rb
|
288
292
|
- spec/lazier/math_spec.rb
|
289
293
|
- spec/lazier/object_spec.rb
|
290
294
|
- spec/lazier/pathname_spec.rb
|