l10n 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.md +16 -0
- data/VERSION +1 -0
- data/lib/l10n.rb +64 -0
- data/lib/l10n/core_extensions.rb +82 -0
- data/lib/l10n/numeric_column_conversions.rb +16 -0
- metadata +72 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Matthias Grosser
|
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
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/l10n.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'active_record'
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
require 'l10n/core_extensions'
|
6
|
+
require 'l10n/numeric_column_conversions'
|
7
|
+
|
8
|
+
|
9
|
+
module L10n
|
10
|
+
@number_helper = Object.new.tap { |obj| obj.extend ActionView::Helpers::NumberHelper }
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
def default?
|
15
|
+
language_code == default_language_code
|
16
|
+
end
|
17
|
+
|
18
|
+
def language_code
|
19
|
+
normalize_language_code(locale) if locale
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_language_code
|
23
|
+
normalize_language_code(default_locale) if default_locale
|
24
|
+
end
|
25
|
+
|
26
|
+
#def avaliable_locales
|
27
|
+
#
|
28
|
+
#end
|
29
|
+
|
30
|
+
def available_language_codes
|
31
|
+
i18n_available_locales.map { |i18n_locale| normalize_language_code(i18n_locale) }.uniq
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# TODO: remove, deprecated
|
36
|
+
# def localize(value, options = {})
|
37
|
+
# case value
|
38
|
+
# when Numeric then @number_helper.number_with_precision(value, options)
|
39
|
+
# end
|
40
|
+
#end
|
41
|
+
#alias :l :localize
|
42
|
+
|
43
|
+
def number_with_precision(*args)
|
44
|
+
@number_helper.number_with_precision(*args)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def normalize_language_code(code)
|
50
|
+
code.to_s[0..1].downcase
|
51
|
+
end
|
52
|
+
|
53
|
+
def i18n_available_locales
|
54
|
+
I18n.available_locales
|
55
|
+
end
|
56
|
+
|
57
|
+
def i18n_current_language
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module L10n
|
2
|
+
module CoreExtensions
|
3
|
+
|
4
|
+
module ObjectExt
|
5
|
+
def to_localized_s
|
6
|
+
to_s
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module StringExt
|
11
|
+
def translate(*args)
|
12
|
+
I18n.t(self, *args)
|
13
|
+
end
|
14
|
+
alias :t :translate
|
15
|
+
end
|
16
|
+
|
17
|
+
module SymbolExt
|
18
|
+
def translate(*args)
|
19
|
+
to_s.translate(*args)
|
20
|
+
end
|
21
|
+
alias :t :translate
|
22
|
+
end
|
23
|
+
|
24
|
+
module DateExt
|
25
|
+
def localize(*args)
|
26
|
+
I18n.l(self, *args)
|
27
|
+
end
|
28
|
+
alias :l :localize
|
29
|
+
end
|
30
|
+
|
31
|
+
module NumericExt
|
32
|
+
|
33
|
+
def self.included(base)
|
34
|
+
base.extend ClassMethods
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def delocalize(value)
|
39
|
+
return value unless value.is_a?(String)
|
40
|
+
separator = I18n.t(:'number.format.separator')
|
41
|
+
delimiter = I18n.t(:'number.format.delimiter')
|
42
|
+
value.gsub(delimiter, '').gsub(separator, '.')
|
43
|
+
end
|
44
|
+
|
45
|
+
def localize(value)
|
46
|
+
return value unless value.is_a?(Numeric) or value.is_a?(String)
|
47
|
+
separator = I18n.t(:'number.format.separator')
|
48
|
+
delimiter = I18n.t(:'number.format.delimiter')
|
49
|
+
value.to_s.gsub('.', 's').gsub(',', delimiter).gsub('s', separator)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_localized_s
|
54
|
+
Numeric.localize(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_formatted_s
|
58
|
+
L10n.number_with_precision(self)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module BigDecimalExt
|
63
|
+
def to_formatted_s
|
64
|
+
L10n.number_with_precision(self)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
L10n::CoreExtensions.constants.each do |mod|
|
72
|
+
extension = L10n::CoreExtensions.const_get(mod)
|
73
|
+
klass = mod.to_s.sub(/Ext\z/, '').constantize
|
74
|
+
klass.class_eval { include extension }
|
75
|
+
end
|
76
|
+
|
77
|
+
#Object.class_eval { include Localite::CoreExtensions::ObjectExt }
|
78
|
+
#String.class_ :include, Localite::CoreExtensions::StringExt
|
79
|
+
#Symbol.send :include, Localite::CoreExtensions::SymbolExt
|
80
|
+
#Numeric.send :include, Localite::CoreExtensions::NumericExt
|
81
|
+
#BigDecimal.send :include, Localite::CoreExtensions::BigDecimalExt
|
82
|
+
#Date.send :include, Localite::CoreExtensions::DateExt
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module L10n
|
2
|
+
module NumericColumnConversions
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.alias_method_chain :convert_number_column_value, :l10n
|
6
|
+
end
|
7
|
+
|
8
|
+
def convert_number_column_value_with_l10n(value)
|
9
|
+
Numeric.delocalize(convert_number_column_value_without_l10n(value))
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.class_eval { include L10n::NumericColumnConversions }
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: l10n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Grosser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: i18n
|
16
|
+
requirement: &3582520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.5'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *3582520
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rails
|
27
|
+
requirement: &3582280 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *3582280
|
36
|
+
description: Extensions for Rails I18n
|
37
|
+
email: mtgrosser@gmx.net
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- lib/l10n/core_extensions.rb
|
43
|
+
- lib/l10n/numeric_column_conversions.rb
|
44
|
+
- lib/l10n.rb
|
45
|
+
- MIT-LICENSE
|
46
|
+
- VERSION
|
47
|
+
- README.md
|
48
|
+
homepage: http://rubygems.org/gems/l10n
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.11
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Extensions for Rails I18n
|
72
|
+
test_files: []
|