l10n 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,6 +6,21 @@ Installation
6
6
  ------------
7
7
 
8
8
 
9
+ Setting up your app
10
+ -------------------
11
+
12
+ In your language.yml, make sure to have definitions for
13
+
14
+ number:
15
+ precision:
16
+ format:
17
+ separator: ","
18
+ delimiter: "."
19
+ precision: 2
20
+
21
+ As there is currently a bug in Rails: https://github.com/rails/rails/issues/4420
22
+
23
+
9
24
  Features
10
25
  --------
11
26
 
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Run all tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.test_files = FileList['test/cases/*_test.rb']
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'L10n'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/l10n.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require 'i18n'
2
- require 'active_record'
3
2
  require 'action_view'
3
+ require 'active_support'
4
4
 
5
5
  require 'l10n/core_extensions'
6
+ require 'l10n/i18n_extensions'
6
7
  require 'l10n/numeric_column_conversions'
8
+ require 'l10n/column_translation'
9
+ require 'l10n/forms'
7
10
 
8
11
 
9
12
  module L10n
@@ -31,19 +34,15 @@ module L10n
31
34
  i18n_available_locales.map { |i18n_locale| normalize_language_code(i18n_locale) }.uniq
32
35
  end
33
36
 
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)
37
+ def number_with_precision(number, options = {})
38
+ #options.symbolize_keys!
39
+ #defaults = { :separator => I18n.t('number.format.separator'),
40
+ # :delimiter => I18n.t('number.format.delimiter'),
41
+ # :precision => I18n.t('number.format.precision') }
42
+ #options.reverse_merge!(defaults)
43
+ @number_helper.number_with_precision(number, options)
45
44
  end
46
-
45
+
47
46
  private
48
47
 
49
48
  def normalize_language_code(code)
@@ -0,0 +1,74 @@
1
+ module L10n
2
+ module ColumnTranslation
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_attribute :translated_attributes, :translation_options
7
+ base.translated_attributes = []
8
+ base.translation_options = {}
9
+ end
10
+
11
+ module ClassMethods
12
+ def translates(*attrs)
13
+ self.translation_options = attrs.last.is_a?(Hash) ? attrs.pop : {}
14
+ self.translated_attributes = attrs
15
+
16
+ definition_context = Proc.new do |attr_name|
17
+ define_method "#{attr_name}_t" do
18
+ read_attribute("#{attr_name}#{I18n.translation_suffix}")
19
+ end
20
+ define_method "#{attr_name}_t_with_fallback" do
21
+ default_value = read_attribute(attr_name)
22
+ if I18n.default?
23
+ localized_value = default_value
24
+ else
25
+ localized_value = read_attribute("#{attr_name}_#{I18n.language_code}")
26
+ localized_value = default_value if localized_value.blank?
27
+ end
28
+ localized_value
29
+ end
30
+ define_method "#{attr_name}_t=" do |value|
31
+ write_attribute "#{attr_name}#{I18n.translation_suffix}", value
32
+ end
33
+ define_method "#{attr_name}_#{I18n.default_language_code}" do
34
+ read_attribute attr_name
35
+ end
36
+ define_method "#{attr_name}_#{I18n.default_language_code}=" do |attr_value|
37
+ write_attribute attr_name, attr_value
38
+ end
39
+ define_method "#{attr_name}_translations" do
40
+ translations = { I18n.default_language_code => read_attribute(attr_name) }
41
+ (I18n.supported_language_codes - [I18n.default_language_code]).each do |language_code|
42
+ translations[language_code] = read_attribute("#{attr_name}_#{language_code}")
43
+ end
44
+ translations
45
+ end
46
+ define_method "#{attr_name}_translations=" do |translations|
47
+ translations.each do |language_code, value|
48
+ method_name = "#{attr_name}_#{language_code}="
49
+ send("#{attr_name}_#{language_code}=", value) if respond_to?(method_name)
50
+ end
51
+ end
52
+ end
53
+ self.translated_attributes.each { |attr_name| definition_context.call attr_name }
54
+ end
55
+
56
+ def translates?(attr_name)
57
+ translated_attributes.map(&:to_s).include?(attr_name.to_s)
58
+ end
59
+
60
+ def translate_column_name(column_name_t)
61
+ column_name_t = column_name_t.to_s
62
+ if column_name_t.ends_with?('_t') and translates?(column_name_t[0..-3])
63
+ "#{column_name_t[0..-3]}#{I18n.translation_suffix}"
64
+ else
65
+ column_name_t
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
73
+
74
+ ActiveRecord::Base.class_eval { include L10n::ColumnTranslation }
@@ -1,82 +1,12 @@
1
- module L10n
2
- module CoreExtensions
3
-
4
- module ObjectExt
5
- def to_localized_s
6
- to_s
7
- end
8
- end
1
+ require 'l10n/core_extensions/object_ext'
2
+ require 'l10n/core_extensions/string_ext'
3
+ require 'l10n/core_extensions/symbol_ext'
4
+ require 'l10n/core_extensions/date_ext'
5
+ require 'l10n/core_extensions/numeric_ext'
6
+ require 'l10n/core_extensions/big_decimal_ext'
9
7
 
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
8
 
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
9
 
33
- def self.included(base)
34
- base.extend ClassMethods
35
- end
36
10
 
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
11
 
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
12
 
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,24 @@
1
+ require 'bigdecimal'
2
+ require 'active_support'
3
+
4
+ module L10n
5
+ module CoreExtensions
6
+ module BigDecimalExt
7
+
8
+ def self.included(base)
9
+ base.class_eval do
10
+ alias_method :_active_support_to_formatted_s, :to_formatted_s
11
+ alias_method :to_s, :_active_support_to_formatted_s
12
+ alias_method :to_formatted_s, :_l10n_to_formatted_s
13
+ end
14
+ end
15
+
16
+ def _l10n_to_formatted_s(*args)
17
+ L10n.number_with_precision(self)
18
+ end
19
+ #
20
+ end
21
+ end
22
+ end
23
+
24
+ BigDecimal.send :include, L10n::CoreExtensions::BigDecimalExt
@@ -0,0 +1,12 @@
1
+ module L10n
2
+ module CoreExtensions
3
+ module DateExt
4
+ def localize(*args)
5
+ I18n.l(self, *args)
6
+ end
7
+ alias :l :localize
8
+ end
9
+ end
10
+ end
11
+
12
+ Date.send :include, L10n::CoreExtensions::DateExt
@@ -0,0 +1,37 @@
1
+ module L10n
2
+ module CoreExtensions
3
+ module NumericExt
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def delocalize(value)
11
+ return value unless value.is_a?(String)
12
+ separator = I18n.t(:'number.format.separator')
13
+ delimiter = I18n.t(:'number.format.delimiter')
14
+ value.gsub(delimiter, '').gsub(separator, '.')
15
+ end
16
+
17
+ def localize(value)
18
+ return value unless value.is_a?(Numeric) or value.is_a?(String)
19
+ separator = I18n.t(:'number.format.separator')
20
+ delimiter = I18n.t(:'number.format.delimiter')
21
+ value.to_s.gsub('.', 's').gsub(',', delimiter).gsub('s', separator)
22
+ end
23
+ end
24
+
25
+ def to_localized_s
26
+ Numeric.localize(self)
27
+ end
28
+
29
+ def to_formatted_s(*args)
30
+ L10n.number_with_precision(self)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+ Numeric.send :include, L10n::CoreExtensions::NumericExt
@@ -0,0 +1,11 @@
1
+ module L10n
2
+ module CoreExtensions
3
+ module ObjectExt
4
+ def to_localized_s
5
+ to_s
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ Object.send :include, L10n::CoreExtensions::ObjectExt
@@ -0,0 +1,12 @@
1
+ module L10n
2
+ module CoreExtensions
3
+ module StringExt
4
+ def translate(*args)
5
+ I18n.t(self, *args)
6
+ end
7
+ alias :t :translate
8
+ end
9
+ end
10
+ end
11
+
12
+ String.send :include, L10n::CoreExtensions::StringExt
@@ -0,0 +1,12 @@
1
+ module L10n
2
+ module CoreExtensions
3
+ module SymbolExt
4
+ def translate(*args)
5
+ I18n.t(self, *args)
6
+ end
7
+ alias :t :translate
8
+ end
9
+ end
10
+ end
11
+
12
+ Symbol.send :include, L10n::CoreExtensions::SymbolExt
data/lib/l10n/forms.rb ADDED
@@ -0,0 +1,19 @@
1
+ module L10n
2
+ module Forms
3
+ module FormBuilder
4
+
5
+ def amount_field(field, options = {})
6
+ value = L10n.number_with_precision(object.send(field))
7
+ options[:value] = value
8
+ options[:class] = "#{options[:class]} amount_field"
9
+ text_field(field, options)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+ ActionView::Helpers::FormBuilder.class_eval do
18
+ include L10n::Forms::FormBuilder
19
+ end
@@ -0,0 +1,83 @@
1
+ module L10n
2
+ module I18nExtensions
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def default?
11
+ language_code == default_language_code
12
+ end
13
+
14
+ def language_code
15
+ normalize_language_code(locale) if locale
16
+ end
17
+
18
+ def country_code
19
+ if locale.to_s.match(/\w\w-\w\w/)
20
+ locale.to_s[-2..-1]
21
+ else
22
+ normalize_locale_code(locale)[-2..-1]
23
+ end
24
+ end
25
+
26
+ def default_language_code
27
+ normalize_language_code(default_locale) if default_locale
28
+ end
29
+
30
+ def available_language_codes
31
+ available_locales.map { |locale| normalize_language_code(locale) }.uniq
32
+ end
33
+
34
+ def supported_language_codes
35
+ [:en, :de]
36
+ end
37
+
38
+ def preferred_locale
39
+ :de
40
+ end
41
+
42
+ def as(tmp_locale)
43
+ if normalized_code = normalize_locale_code(tmp_locale)
44
+ previous_locale = self.locale
45
+ self.locale = normalized_code
46
+ end
47
+ yield
48
+ ensure
49
+ self.locale = previous_locale if normalized_code
50
+ end
51
+
52
+ def as_default
53
+ I18n.as(I18n.default_locale) { yield }
54
+ end
55
+
56
+ def as_each
57
+ supported_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { yield(code) }; hash }
58
+ end
59
+
60
+ def translations(key)
61
+ supported_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { I18n.t(key) }; hash }
62
+ end
63
+
64
+ def translation_suffix
65
+ default? ? '' : "_#{I18n.language_code}"
66
+ end
67
+
68
+ private
69
+
70
+ def normalize_language_code(code)
71
+ code.to_s[0..1].downcase.to_sym
72
+ end
73
+
74
+ def normalize_locale_code(code)
75
+ normalize_language_code(code)
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ I18n.extend L10n::I18nExtensions::ClassMethods
@@ -1,3 +1,5 @@
1
+ require 'active_record'
2
+
1
3
  module L10n
2
4
  module NumericColumnConversions
3
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l10n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &3582520 !ruby/object:Gem::Requirement
16
+ requirement: &3672640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.5'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *3582520
24
+ version_requirements: *3672640
25
25
  - !ruby/object:Gem::Dependency
26
- name: rails
27
- requirement: &3582280 !ruby/object:Gem::Requirement
26
+ name: activerecord
27
+ requirement: &3672400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,19 +32,84 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *3582280
36
- description: Extensions for Rails I18n
35
+ version_requirements: *3672400
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &3672170 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *3672170
47
+ - !ruby/object:Gem::Dependency
48
+ name: actionpack
49
+ requirement: &3671940 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *3671940
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &3671750 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *3671750
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &3671520 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *3671520
80
+ - !ruby/object:Gem::Dependency
81
+ name: rake
82
+ requirement: &3671270 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 0.8.7
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *3671270
91
+ description: I18n that roarrrs
37
92
  email: mtgrosser@gmx.net
38
93
  executables: []
39
94
  extensions: []
40
95
  extra_rdoc_files: []
41
96
  files:
97
+ - lib/l10n/column_translation.rb
98
+ - lib/l10n/core_extensions/big_decimal_ext.rb
99
+ - lib/l10n/core_extensions/date_ext.rb
100
+ - lib/l10n/core_extensions/numeric_ext.rb
101
+ - lib/l10n/core_extensions/object_ext.rb
102
+ - lib/l10n/core_extensions/string_ext.rb
103
+ - lib/l10n/core_extensions/symbol_ext.rb
42
104
  - lib/l10n/core_extensions.rb
105
+ - lib/l10n/forms.rb
106
+ - lib/l10n/i18n_extensions.rb
43
107
  - lib/l10n/numeric_column_conversions.rb
44
108
  - lib/l10n.rb
45
109
  - MIT-LICENSE
46
110
  - VERSION
47
111
  - README.md
112
+ - Rakefile
48
113
  homepage: http://rubygems.org/gems/l10n
49
114
  licenses: []
50
115
  post_install_message:
@@ -70,3 +135,4 @@ signing_key:
70
135
  specification_version: 3
71
136
  summary: Extensions for Rails I18n
72
137
  test_files: []
138
+ has_rdoc: