l10n 1.0.1 → 1.1.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG +5 -0
- data/README.md +135 -12
- data/app/assets/javascripts/i18n.js +33 -0
- data/lib/l10n.rb +3 -0
- data/lib/l10n/core_extensions.rb +1 -1
- data/lib/l10n/core_extensions/{date_ext.rb → date_time_ext.rb} +3 -2
- data/lib/l10n/engine.rb +7 -0
- data/lib/l10n/javascript_helper.rb +4 -2
- data/lib/l10n/version.rb +1 -1
- metadata +25 -8
- data/Rakefile +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 866b98e1c6683bc7ec991e01d7d2d1ee67311906
|
|
4
|
+
data.tar.gz: 3c5bb6f5e84a1d83839e35d470c9482fb764c9bb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b955a86b74e926412306b7e03b7e81c93a781a7f866c0c92de4b1b46d005158e7b8700c572cc9b449fb49345f7dac135c20c2c3e0f44370de257e6a8ee8cbdb
|
|
7
|
+
data.tar.gz: 71f8806d4025bfb63697d44a0e1b76cd8da2a9fccc2b61378135eb9d512a5cef886d7f13b6564e31307dde6958128009258679b71fc0671935c43d8ce780d8b2
|
data/CHANGELOG
ADDED
data/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
L10n - I18n that roarrrs
|
|
2
|
-
========================
|
|
1
|
+
# L10n - I18n that roarrrs
|
|
3
2
|
|
|
3
|
+
## Installation
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
```ruby
|
|
6
|
+
# Gemfile
|
|
7
|
+
gem 'l10n'
|
|
8
|
+
```
|
|
7
9
|
|
|
10
|
+
## Setting up your app
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
-------------------
|
|
11
|
-
|
|
12
|
-
In your language.yml, make sure to have definitions for
|
|
12
|
+
In your <language>.yml, make sure to have definitions for
|
|
13
13
|
|
|
14
14
|
number:
|
|
15
15
|
precision:
|
|
@@ -20,12 +20,135 @@ number:
|
|
|
20
20
|
|
|
21
21
|
As there is currently a bug in Rails: https://github.com/rails/rails/issues/4420
|
|
22
22
|
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
### ActiveRecord column translations
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class Fruit < ActiveRecord::Base
|
|
29
|
+
# columns: name, name_de, name_fr
|
|
30
|
+
|
|
31
|
+
translates :name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
apple = Fruit.new(name: 'Apple', name_de: 'Apfel', name_fr: 'Pomme')
|
|
35
|
+
|
|
36
|
+
I18n.as(:en) { apple.name_t } => "Apple"
|
|
37
|
+
I18n.as(:de) { apple.name_t } => "Apfel"
|
|
38
|
+
I18n.as(:fr) { apple.name_t } => "Pomme"
|
|
39
|
+
|
|
40
|
+
apple.name_translations => { en: "Apple", de: "Apfel", fr: "Pomme" }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Translated columns also support validation:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class Fruit < ActiveRecord::Base
|
|
47
|
+
translates :name
|
|
48
|
+
|
|
49
|
+
# all translation columns for "name" must be present
|
|
50
|
+
validate :name, translation: true
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Core extensions
|
|
55
|
+
|
|
56
|
+
#### String and Symbol
|
|
57
|
+
|
|
58
|
+
```yaml
|
|
59
|
+
# de.yml
|
|
60
|
+
de:
|
|
61
|
+
words:
|
|
62
|
+
one: Eins
|
|
63
|
+
two: Zwei
|
|
64
|
+
three: Drei
|
|
65
|
+
four: Vier
|
|
66
|
+
five: Fünf
|
|
67
|
+
|
|
68
|
+
hello: "Hallo %{name}!"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
I18n.locale = :de
|
|
73
|
+
|
|
74
|
+
'words.one'.t => 'Eins'
|
|
75
|
+
|
|
76
|
+
'hello'.t(name: 'Otto') => "Hallo Otto!"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This also works with symbols.
|
|
80
|
+
|
|
81
|
+
#### Formatting of numbers
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
I18n.as('de') { 1234.5.to_formatted_s } => "1.234,50"
|
|
85
|
+
I18n.as('en') { 1234.5.to_formatted_s } => "1,234.50"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
This also works with instances of BigDecimal.
|
|
89
|
+
|
|
90
|
+
#### Localization of decimal separator and delimiter for numbers
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
I18n.as('de') { 1234.5.to_localized_s } => "1.234,5"
|
|
94
|
+
I18n.as('en') { 1234.5.to_localized_s } => "1,234.5"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Localization of decimal separator and delimiter for numeric strings
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
I18n.as(:de) { Numeric.localize('1,234.50') } => "1.234,50"
|
|
101
|
+
I18n.as(:en) { Numeric.localize('1,234.50') } => "1,234.50"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Automatic localization of numeric values in Rails forms and models
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
# in your template
|
|
108
|
+
<%= form_for @car do |f| %>
|
|
109
|
+
<% f.amount_field :price, precision: 2 %>
|
|
110
|
+
<% end %>
|
|
111
|
+
|
|
112
|
+
# in your controller, or elsewhere
|
|
113
|
+
# params => { car: { price: "12.000,00" } }
|
|
114
|
+
|
|
115
|
+
I18n.locale = :de
|
|
116
|
+
@car = Car.new(params[:car]).price => 12000
|
|
117
|
+
|
|
118
|
+
I18n.locale = :en
|
|
119
|
+
@car = Car.new(params[:car]).price => 12
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Accept-Language header parsing in ActionDispatch::Request
|
|
123
|
+
|
|
124
|
+
The Accept-Language header will be parsed, and locales will be returned ordered by user preference.
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
# in your controller
|
|
128
|
+
request.accept_locales => ["en-US", "en", "en-GB"]
|
|
129
|
+
```
|
|
23
130
|
|
|
24
|
-
|
|
25
|
-
--------
|
|
131
|
+
### Javascript I18n, interpolation and pluralization
|
|
26
132
|
|
|
133
|
+
```yaml
|
|
134
|
+
# en.yml
|
|
135
|
+
en:
|
|
136
|
+
javascript:
|
|
137
|
+
hello: Hello {name}!
|
|
138
|
+
|
|
139
|
+
apple:
|
|
140
|
+
one: '{count} apple'
|
|
141
|
+
other: '{count} apples'
|
|
142
|
+
```
|
|
27
143
|
|
|
28
|
-
|
|
144
|
+
```ruby
|
|
145
|
+
# in your application layout
|
|
146
|
+
<%= i18n_script_tag %>
|
|
147
|
+
```
|
|
29
148
|
|
|
30
|
-
|
|
149
|
+
```javascript
|
|
150
|
+
// in any javascript
|
|
151
|
+
"hello".t({ name: "JS" }) => "Hello JS!"
|
|
31
152
|
|
|
153
|
+
"apple".t({ count: 4 }) => "5 apples"
|
|
154
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var I18n = I18n || {};
|
|
2
|
+
|
|
3
|
+
I18n.translate = function(str, options) {
|
|
4
|
+
options = options || {};
|
|
5
|
+
var fallback = options['fallback'] || ('Translation missing: ' + str); // default is a JS reserved word
|
|
6
|
+
var translation, matches, method, match, interpolation_value;
|
|
7
|
+
var count = options['count'];
|
|
8
|
+
var keys = str.split('.');
|
|
9
|
+
var obj = this.translations;
|
|
10
|
+
while(obj && keys.length) obj = obj[keys.shift()];
|
|
11
|
+
if (obj) {
|
|
12
|
+
if ($.type(obj) == 'object' && count !== undefined) {
|
|
13
|
+
translation = obj[1 == count ? 'one' : 'other'];
|
|
14
|
+
} else {
|
|
15
|
+
translation = obj;
|
|
16
|
+
}
|
|
17
|
+
if ($.type(translation) == 'string' && (matches = translation.match(/{\w+?}/g))) {
|
|
18
|
+
while(match = matches.shift()) {
|
|
19
|
+
method = match.substr(1, match.length - 2);
|
|
20
|
+
interpolation_value = options[method];
|
|
21
|
+
if (interpolation_value == undefined) interpolation_value = ('undefined interpolation value: ' + method);
|
|
22
|
+
translation = translation.replace(new RegExp(match, 'g'), interpolation_value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return translation || fallback;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
I18n.t = function(str, options) {
|
|
30
|
+
return this.translate(str, options);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
String.prototype.t = function(options){ return(I18n.translate(this, options)); }
|
data/lib/l10n.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'i18n'
|
|
2
|
+
require 'date'
|
|
2
3
|
require 'bigdecimal'
|
|
3
4
|
require 'action_view'
|
|
4
5
|
require 'action_dispatch'
|
|
@@ -12,7 +13,9 @@ require 'l10n/numeric_column_conversions'
|
|
|
12
13
|
require 'l10n/column_translation'
|
|
13
14
|
require 'l10n/translation_validator'
|
|
14
15
|
require 'l10n/forms'
|
|
16
|
+
require 'l10n/javascript_helper'
|
|
15
17
|
require 'l10n/request'
|
|
18
|
+
require 'l10n/engine'
|
|
16
19
|
|
|
17
20
|
files = Dir[File.join(File.dirname(__FILE__), 'locales/*.yml')]
|
|
18
21
|
I18n.load_path.concat(files)
|
data/lib/l10n/core_extensions.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'l10n/core_extensions/object_ext'
|
|
2
2
|
require 'l10n/core_extensions/string_ext'
|
|
3
3
|
require 'l10n/core_extensions/symbol_ext'
|
|
4
|
-
require 'l10n/core_extensions/
|
|
4
|
+
require 'l10n/core_extensions/date_time_ext'
|
|
5
5
|
require 'l10n/core_extensions/numeric_ext'
|
|
6
6
|
require 'l10n/core_extensions/big_decimal_ext'
|
|
7
7
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module L10n
|
|
2
2
|
module CoreExtensions
|
|
3
|
-
module
|
|
3
|
+
module DateTimeExt
|
|
4
4
|
def localize(*args)
|
|
5
5
|
I18n.l(self, *args)
|
|
6
6
|
end
|
|
@@ -9,4 +9,5 @@ module L10n
|
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
Date.send :include, L10n::CoreExtensions::
|
|
12
|
+
Date.send :include, L10n::CoreExtensions::DateTimeExt
|
|
13
|
+
Time.send :include, L10n::CoreExtensions::DateTimeExt
|
data/lib/l10n/engine.rb
ADDED
|
@@ -2,13 +2,15 @@ module L10n
|
|
|
2
2
|
module JavaScriptHelper
|
|
3
3
|
|
|
4
4
|
def i18n_script_tag
|
|
5
|
-
javascript_tag
|
|
5
|
+
javascript_tag %<
|
|
6
6
|
var I18n = I18n || {};
|
|
7
7
|
|
|
8
8
|
I18n.locale = function() {
|
|
9
9
|
return("#{I18n.locale}");
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
I18n.translations = #{I18n.t(:javascript).to_json};
|
|
13
|
+
>
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
end
|
data/lib/l10n/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: l10n
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthias Grosser
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-
|
|
11
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: i18n
|
|
@@ -66,6 +66,20 @@ dependencies:
|
|
|
66
66
|
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: '4.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: railties
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '4.0'
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '4.0'
|
|
69
83
|
- !ruby/object:Gem::Dependency
|
|
70
84
|
name: sqlite3
|
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -122,24 +136,26 @@ dependencies:
|
|
|
122
136
|
- - ">="
|
|
123
137
|
- !ruby/object:Gem::Version
|
|
124
138
|
version: 0.8.7
|
|
125
|
-
description:
|
|
139
|
+
description: Extensions for Rails I18n
|
|
126
140
|
email: mtgrosser@gmx.net
|
|
127
141
|
executables: []
|
|
128
142
|
extensions: []
|
|
129
143
|
extra_rdoc_files: []
|
|
130
144
|
files:
|
|
145
|
+
- CHANGELOG
|
|
131
146
|
- MIT-LICENSE
|
|
132
147
|
- README.md
|
|
133
|
-
-
|
|
148
|
+
- app/assets/javascripts/i18n.js
|
|
134
149
|
- lib/l10n.rb
|
|
135
150
|
- lib/l10n/column_translation.rb
|
|
136
151
|
- lib/l10n/core_extensions.rb
|
|
137
152
|
- lib/l10n/core_extensions/big_decimal_ext.rb
|
|
138
|
-
- lib/l10n/core_extensions/
|
|
153
|
+
- lib/l10n/core_extensions/date_time_ext.rb
|
|
139
154
|
- lib/l10n/core_extensions/numeric_ext.rb
|
|
140
155
|
- lib/l10n/core_extensions/object_ext.rb
|
|
141
156
|
- lib/l10n/core_extensions/string_ext.rb
|
|
142
157
|
- lib/l10n/core_extensions/symbol_ext.rb
|
|
158
|
+
- lib/l10n/engine.rb
|
|
143
159
|
- lib/l10n/forms.rb
|
|
144
160
|
- lib/l10n/i18n_extensions.rb
|
|
145
161
|
- lib/l10n/inflections.rb
|
|
@@ -150,8 +166,9 @@ files:
|
|
|
150
166
|
- lib/l10n/version.rb
|
|
151
167
|
- lib/locales/de.yml
|
|
152
168
|
- lib/locales/en.yml
|
|
153
|
-
homepage:
|
|
154
|
-
licenses:
|
|
169
|
+
homepage: https://github.com/mtgrosser/l10n
|
|
170
|
+
licenses:
|
|
171
|
+
- MIT
|
|
155
172
|
metadata: {}
|
|
156
173
|
post_install_message:
|
|
157
174
|
rdoc_options: []
|
|
@@ -172,5 +189,5 @@ rubyforge_project:
|
|
|
172
189
|
rubygems_version: 2.2.2
|
|
173
190
|
signing_key:
|
|
174
191
|
specification_version: 4
|
|
175
|
-
summary:
|
|
192
|
+
summary: I18n that roarrrs
|
|
176
193
|
test_files: []
|
data/Rakefile
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|