l10n 1.1.0 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +45 -13
- data/lib/l10n/request.rb +3 -3
- data/lib/l10n/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d7f3cc16fbc921b6681758f6c4ff7f3d2f783052
|
|
4
|
+
data.tar.gz: 617e2a3795ac781d6ba43c0d66288aa2f872afb8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 701003456bee2225f62ac1951c5a2432303ec43b671f6fdf1f00fbab1b97783b84eff05ef0d9f21faf3892a8e867562b5c63e64bbe326a5c95b6bad9361dbe70
|
|
7
|
+
data.tar.gz: c19dc9321a710cc84dd2f0a5b087b8995c995e01aabb703d13fe9381431a68a2644eea6c64c144f97ba2c96e89de4453fb601fd972d3ef28cfc62b96febe2149
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# L10n - I18n that roarrrs
|
|
2
2
|
|
|
3
|
+
L10n provides some useful extensions for Rails I18n, including column translations, localization of numeric form fields and JavaScript translation support.
|
|
4
|
+
|
|
3
5
|
## Installation
|
|
4
6
|
|
|
5
7
|
```ruby
|
|
@@ -9,20 +11,20 @@ gem 'l10n'
|
|
|
9
11
|
|
|
10
12
|
## Setting up your app
|
|
11
13
|
|
|
12
|
-
In your
|
|
14
|
+
In your `<locale>.yml`, make sure to have definitions for
|
|
13
15
|
|
|
16
|
+
```yaml
|
|
14
17
|
number:
|
|
15
18
|
precision:
|
|
16
19
|
format:
|
|
17
|
-
separator: ","
|
|
18
20
|
delimiter: "."
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
As there is currently a bug in Rails: https://github.com/rails/rails/issues/4420
|
|
21
|
+
```
|
|
22
22
|
|
|
23
23
|
## Features
|
|
24
24
|
|
|
25
|
-
###
|
|
25
|
+
### Active Record attribute translations
|
|
26
|
+
|
|
27
|
+
Translated attributes provide an `<attr>_t` suffix, which maps to the column determined by the current locale. There is no whatsoever "magic" remapping of actual attributes. The `<attr>_t` accessor is used exclusively for mapping to the column referred to by the current locale.
|
|
26
28
|
|
|
27
29
|
```ruby
|
|
28
30
|
class Fruit < ActiveRecord::Base
|
|
@@ -40,8 +42,23 @@ I18n.as(:fr) { apple.name_t } => "Pomme"
|
|
|
40
42
|
apple.name_translations => { en: "Apple", de: "Apfel", fr: "Pomme" }
|
|
41
43
|
```
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
The `<attr>_t` and `<attr>_translations` setters map to the current locale:
|
|
46
|
+
```ruby
|
|
47
|
+
pear = Fruit.new
|
|
48
|
+
pear.name_translations = { en: 'Pear', de: 'Birne', fr: 'Poire' }
|
|
44
49
|
|
|
50
|
+
I18n.locale = :fr
|
|
51
|
+
pear.name => "Pear"
|
|
52
|
+
pear.name_t => "Poire"
|
|
53
|
+
|
|
54
|
+
I18n.locale = :en
|
|
55
|
+
pear.name => "Pear"
|
|
56
|
+
pear.name_t => "Pear"
|
|
57
|
+
pear.name_t = 'Williams Christ'
|
|
58
|
+
pear.name => "Williams Christ"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Translated columns also support validation:
|
|
45
62
|
```ruby
|
|
46
63
|
class Fruit < ActiveRecord::Base
|
|
47
64
|
translates :name
|
|
@@ -55,6 +72,8 @@ end
|
|
|
55
72
|
|
|
56
73
|
#### String and Symbol
|
|
57
74
|
|
|
75
|
+
Strings and symbols provide a `translate` method, aliased as `t` which maps to `I18n.t`.
|
|
76
|
+
|
|
58
77
|
```yaml
|
|
59
78
|
# de.yml
|
|
60
79
|
de:
|
|
@@ -76,19 +95,21 @@ I18n.locale = :de
|
|
|
76
95
|
'hello'.t(name: 'Otto') => "Hallo Otto!"
|
|
77
96
|
```
|
|
78
97
|
|
|
79
|
-
This also works with symbols.
|
|
80
|
-
|
|
81
98
|
#### Formatting of numbers
|
|
82
99
|
|
|
100
|
+
Calling `to_formatted_s` on `Numeric`s returns the number as a formatted string. The format is defined by the current locale and respects the decimal delimiters and separators defined in your `<locale>.yml`.
|
|
101
|
+
|
|
83
102
|
```ruby
|
|
84
103
|
I18n.as('de') { 1234.5.to_formatted_s } => "1.234,50"
|
|
85
104
|
I18n.as('en') { 1234.5.to_formatted_s } => "1,234.50"
|
|
86
105
|
```
|
|
87
106
|
|
|
88
|
-
This also works with
|
|
107
|
+
This also works with `BigDecimal`s.
|
|
89
108
|
|
|
90
109
|
#### Localization of decimal separator and delimiter for numbers
|
|
91
110
|
|
|
111
|
+
Localization converts decimal separators and delimiters between locales without re-formatting strings. `to_localized_s` can be called on any object.
|
|
112
|
+
|
|
92
113
|
```ruby
|
|
93
114
|
I18n.as('de') { 1234.5.to_localized_s } => "1.234,5"
|
|
94
115
|
I18n.as('en') { 1234.5.to_localized_s } => "1,234.5"
|
|
@@ -103,10 +124,12 @@ I18n.as(:en) { Numeric.localize('1,234.50') } => "1,234.50"
|
|
|
103
124
|
|
|
104
125
|
### Automatic localization of numeric values in Rails forms and models
|
|
105
126
|
|
|
127
|
+
The `amount_field` form helper automatically formats numbers in the current locale. Numeric columns automatically convert the localized strings into numbers, respecting decimal delimiters and separators.
|
|
128
|
+
|
|
106
129
|
```ruby
|
|
107
130
|
# in your template
|
|
108
131
|
<%= form_for @car do |f| %>
|
|
109
|
-
|
|
132
|
+
<%= f.amount_field :price, precision: 2 %>
|
|
110
133
|
<% end %>
|
|
111
134
|
|
|
112
135
|
# in your controller, or elsewhere
|
|
@@ -121,7 +144,7 @@ I18n.locale = :en
|
|
|
121
144
|
|
|
122
145
|
### Accept-Language header parsing in ActionDispatch::Request
|
|
123
146
|
|
|
124
|
-
The Accept-Language header will be parsed, and locales will be returned ordered by user preference.
|
|
147
|
+
The `Accept-Language` HTTP header will be parsed, and locales will be returned ordered by user preference. This comes in handy when setting the current locale in a `before_action`.
|
|
125
148
|
|
|
126
149
|
```ruby
|
|
127
150
|
# in your controller
|
|
@@ -130,6 +153,15 @@ request.accept_locales => ["en-US", "en", "en-GB"]
|
|
|
130
153
|
|
|
131
154
|
### Javascript I18n, interpolation and pluralization
|
|
132
155
|
|
|
156
|
+
If you need I18n support in your javascripts, require the `i18n` javascript from your `application.js`:
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
/* application.js */
|
|
160
|
+
//= require i18n
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The JS `String` prototype is extended with a `t()` function, supporting translation, pluralization and interpolation:
|
|
164
|
+
|
|
133
165
|
```yaml
|
|
134
166
|
# en.yml
|
|
135
167
|
en:
|
|
@@ -150,5 +182,5 @@ en:
|
|
|
150
182
|
// in any javascript
|
|
151
183
|
"hello".t({ name: "JS" }) => "Hello JS!"
|
|
152
184
|
|
|
153
|
-
"apple".t({ count:
|
|
185
|
+
"apple".t({ count: 5 }) => "5 apples"
|
|
154
186
|
```
|
data/lib/l10n/request.rb
CHANGED
|
@@ -7,11 +7,11 @@ module L10n
|
|
|
7
7
|
preferred_locales = []
|
|
8
8
|
for locale_group in locale_groups
|
|
9
9
|
locale = locale_group.split(';')
|
|
10
|
-
language_code, params = locale[0].to_s.strip, locale[1]
|
|
10
|
+
language_code, params = locale[0].to_s.strip, locale[1].to_s.strip
|
|
11
11
|
if /\A[A-z]{2}(-[A-z]{2})?\z/.match(language_code)
|
|
12
|
-
if params
|
|
12
|
+
if params.present?
|
|
13
13
|
name, value = params.split('=')
|
|
14
|
-
q = (name == 'q'
|
|
14
|
+
q = (name == 'q' && value.to_f > 0 ? value.to_f : 1.0)
|
|
15
15
|
else
|
|
16
16
|
q = 1.0
|
|
17
17
|
end
|
data/lib/l10n/version.rb
CHANGED
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: 1.1.
|
|
4
|
+
version: 1.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthias Grosser
|
|
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
186
186
|
version: '0'
|
|
187
187
|
requirements: []
|
|
188
188
|
rubyforge_project:
|
|
189
|
-
rubygems_version: 2.
|
|
189
|
+
rubygems_version: 2.5.1
|
|
190
190
|
signing_key:
|
|
191
191
|
specification_version: 4
|
|
192
192
|
summary: I18n that roarrrs
|