l10n 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,40 @@
1
1
  require 'i18n'
2
2
  require 'action_view'
3
+ require 'action_dispatch'
3
4
  require 'active_support'
4
5
 
6
+ require 'l10n/version'
5
7
  require 'l10n/core_extensions'
6
8
  require 'l10n/i18n_extensions'
7
9
  require 'l10n/numeric_column_conversions'
8
10
  require 'l10n/column_translation'
9
11
  require 'l10n/forms'
10
-
12
+ require 'l10n/request'
11
13
 
12
14
  module L10n
13
15
  @number_helper = Object.new.tap { |obj| obj.extend ActionView::Helpers::NumberHelper }
14
16
 
15
17
  class << self
16
18
 
17
- def default?
18
- language_code == default_language_code
19
- end
19
+ #def default?
20
+ # language_code == default_language_code
21
+ #end
20
22
 
21
- def language_code
22
- normalize_language_code(locale) if locale
23
- end
23
+ #def language_code
24
+ # normalize_language_code(locale) if locale
25
+ #end
24
26
 
25
- def default_language_code
26
- normalize_language_code(default_locale) if default_locale
27
- end
27
+ #def default_language_code
28
+ # normalize_language_code(default_locale) if default_locale
29
+ #end
28
30
 
29
31
  #def avaliable_locales
30
32
  #
31
33
  #end
32
34
 
33
- def available_language_codes
34
- i18n_available_locales.map { |i18n_locale| normalize_language_code(i18n_locale) }.uniq
35
- end
35
+ #def available_language_codes
36
+ # i18n_available_locales.map { |i18n_locale| normalize_language_code(i18n_locale) }.uniq
37
+ #end
36
38
 
37
39
  def number_with_precision(number, options = {})
38
40
  #options.symbolize_keys!
@@ -45,17 +47,17 @@ module L10n
45
47
 
46
48
  private
47
49
 
48
- def normalize_language_code(code)
49
- code.to_s[0..1].downcase
50
- end
50
+ #def normalize_language_code(code)
51
+ # code.to_s[0..1].downcase
52
+ #end
51
53
 
52
- def i18n_available_locales
53
- I18n.available_locales
54
- end
54
+ #def i18n_available_locales
55
+ # I18n.available_locales
56
+ #end
55
57
 
56
- def i18n_current_language
57
-
58
- end
58
+ #def i18n_current_language
59
+ #
60
+ #end
59
61
 
60
62
  end
61
63
 
@@ -36,15 +36,23 @@ module L10n
36
36
  end
37
37
  define_method "#{attr_name}_translations" do
38
38
  translations = { I18n.default_language_code => read_attribute(attr_name) }
39
- (I18n.supported_language_codes - [I18n.default_language_code]).each do |language_code|
39
+ I18n.translation_language_codes.each do |language_code|
40
40
  translations[language_code] = read_attribute("#{attr_name}_#{language_code}")
41
41
  end
42
42
  translations
43
43
  end
44
44
  define_method "#{attr_name}_translations=" do |translations|
45
+ translations = translations.dup
46
+ if all = translations.delete(:all)
47
+ send("#{attr_name}=", all)
48
+ I18n.translation_language_codes.each do |language_code|
49
+ method_name = "#{attr_name}_#{language_code}="
50
+ send(method_name, all) if respond_to?(method_name)
51
+ end
52
+ end
45
53
  translations.each do |language_code, value|
46
54
  method_name = "#{attr_name}_#{language_code}="
47
- send("#{attr_name}_#{language_code}=", value) if respond_to?(method_name)
55
+ send(method_name, value) if respond_to?(method_name)
48
56
  end
49
57
  end
50
58
  end
@@ -3,6 +3,7 @@ module L10n
3
3
 
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
6
+ base.mattr_accessor :preferred_locale
6
7
  end
7
8
 
8
9
  module ClassMethods
@@ -10,18 +11,19 @@ module L10n
10
11
  def default?
11
12
  language_code == default_language_code
12
13
  end
13
-
14
+
14
15
  def language_code
15
16
  normalize_language_code(locale) if locale
16
17
  end
17
18
 
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
19
+ ## deprecated
20
+ #def country_code
21
+ # if locale.to_s.match(/\w\w-\w\w/)
22
+ # locale.to_s[-2..-1]
23
+ # else
24
+ # normalize_locale_code(locale)[-2..-1]
25
+ # end
26
+ #end
25
27
 
26
28
  def default_language_code
27
29
  normalize_language_code(default_locale) if default_locale
@@ -30,13 +32,19 @@ module L10n
30
32
  def available_language_codes
31
33
  available_locales.map { |locale| normalize_language_code(locale) }.uniq
32
34
  end
33
-
34
- def supported_language_codes
35
- [:en, :de]
35
+
36
+ def translation_language_codes
37
+ I18n.available_language_codes - [I18n.default_language_code]
38
+ end
39
+
40
+ def available(locale)
41
+ return nil if locale.blank?
42
+ locale = normalize_locale_code(locale)
43
+ available_locales.include?(locale) ? locale : nil
36
44
  end
37
45
 
38
- def preferred_locale
39
- :de
46
+ def available?(locale)
47
+ available(locale) ? true : false
40
48
  end
41
49
 
42
50
  def as(tmp_locale)
@@ -54,11 +62,11 @@ module L10n
54
62
  end
55
63
 
56
64
  def as_each
57
- supported_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { yield(code) }; hash }
65
+ available_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { yield(code) }; hash }
58
66
  end
59
67
 
60
68
  def translations(key)
61
- supported_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { I18n.t(key) }; hash }
69
+ available_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { I18n.t(key) }; hash }
62
70
  end
63
71
 
64
72
  def translation_suffix
@@ -66,11 +74,13 @@ module L10n
66
74
  end
67
75
 
68
76
  private
69
-
77
+
78
+ # ISO 639-1 two-letter language code
70
79
  def normalize_language_code(code)
71
80
  code.to_s[0..1].downcase.to_sym
72
81
  end
73
82
 
83
+ # deprecated
74
84
  def normalize_locale_code(code)
75
85
  normalize_language_code(code)
76
86
  end
@@ -80,4 +90,4 @@ module L10n
80
90
  end
81
91
  end
82
92
 
83
- I18n.extend L10n::I18nExtensions::ClassMethods
93
+ I18n.send :include, L10n::I18nExtensions
@@ -0,0 +1,27 @@
1
+ module L10n
2
+ module Request
3
+
4
+ def accept_locales
5
+ return [] if env['HTTP_ACCEPT_LANGUAGE'].blank?
6
+ locale_groups = env['HTTP_ACCEPT_LANGUAGE'].split(',')
7
+ preferred_locales = []
8
+ for locale_group in locale_groups
9
+ locale = locale_group.split(';')
10
+ language_code, params = locale[0].to_s.strip, locale[1]
11
+ if /\A[A-z]{2}(-[A-z]{2})?\z/.match(language_code)
12
+ if params
13
+ name, value = params.split('=')
14
+ q = (name == 'q' and value.to_f > 0 ? value.to_f : 1.0)
15
+ else
16
+ q = 1.0
17
+ end
18
+ preferred_locales << [language_code, q]
19
+ end
20
+ end
21
+ preferred_locales.sort { |a, b| b.last <=> a.last }.map(&:first)
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ ActionDispatch::Request.class_eval { include L10n::Request }
@@ -0,0 +1,3 @@
1
+ module L10n
2
+ VERSION = '0.0.4'
3
+ end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-09 00:00:00.000000000 Z
12
+ date: 2012-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &2151919420 !ruby/object:Gem::Requirement
16
+ requirement: &2151917000 !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: *2151919420
24
+ version_requirements: *2151917000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &2151918880 !ruby/object:Gem::Requirement
27
+ requirement: &2151916280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2151918880
35
+ version_requirements: *2151916280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &2151918160 !ruby/object:Gem::Requirement
38
+ requirement: &2151915440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 3.0.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2151918160
46
+ version_requirements: *2151915440
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: actionpack
49
- requirement: &2151917520 !ruby/object:Gem::Requirement
49
+ requirement: &2151914740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 3.0.0
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2151917520
57
+ version_requirements: *2151914740
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3
60
- requirement: &2151917000 !ruby/object:Gem::Requirement
60
+ requirement: &2151914220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2151917000
68
+ version_requirements: *2151914220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &2151916300 !ruby/object:Gem::Requirement
71
+ requirement: &2151913500 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2151916300
79
+ version_requirements: *2151913500
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rake
82
- requirement: &2151915400 !ruby/object:Gem::Requirement
82
+ requirement: &2151912880 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: 0.8.7
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2151915400
90
+ version_requirements: *2151912880
91
91
  description: I18n that roarrrs
92
92
  email: mtgrosser@gmx.net
93
93
  executables: []
@@ -105,9 +105,10 @@ files:
105
105
  - lib/l10n/forms.rb
106
106
  - lib/l10n/i18n_extensions.rb
107
107
  - lib/l10n/numeric_column_conversions.rb
108
+ - lib/l10n/request.rb
109
+ - lib/l10n/version.rb
108
110
  - lib/l10n.rb
109
111
  - MIT-LICENSE
110
- - VERSION
111
112
  - README.md
112
113
  - Rakefile
113
114
  homepage: http://rubygems.org/gems/l10n
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.3