polyglot-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/lib/polyglot.rb +13 -0
- data/lib/polyglot/helper.rb +12 -0
- data/lib/polyglot/version.rb +3 -0
- data/vendor/assets/javascripts/polyglot.js +289 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 679f4c05ed8ceb4b420e9d76d95d95660e373494
|
4
|
+
data.tar.gz: d86676a97a0f8ca343ab2e9c904664fd7fad6144
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 100f0b9c4b0087467d6b065954634c112b741face597e1307beaa691030c395609235b113c74a13e2c6b9b5115d90b622f876c208d4344578558ffa63a98a74a
|
7
|
+
data.tar.gz: 71ddcd8620c9c713755fa3331bbb3f27d592a93380ee595c853c84becd8d218a963e37f9d73ef38298e4a34107f15ff4ecc1375d9fb82a9fb7758c42bb138e30
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015
|
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.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Polyglot'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
data/lib/polyglot.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Polyglot
|
2
|
+
module Helper
|
3
|
+
def to_polyglot
|
4
|
+
file = YAML::load(File.open("config/locales/#{controller_name}.#{locale}.yml"))
|
5
|
+
trs = file["#{locale}"]['polyglot'].to_json
|
6
|
+
|
7
|
+
content_for(:polyglot) do
|
8
|
+
javascript_tag("var polyglot = new Polyglot({ phrases: #{trs} });")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
// (c) 2012 Airbnb, Inc.
|
2
|
+
//
|
3
|
+
// polyglot.js may be freely distributed under the terms of the BSD
|
4
|
+
// license. For all licensing information, details, and documention:
|
5
|
+
// http://airbnb.github.com/polyglot.js
|
6
|
+
//
|
7
|
+
//
|
8
|
+
// Polyglot.js is an I18n helper library written in JavaScript, made to
|
9
|
+
// work both in the browser and in Node. It provides a simple solution for
|
10
|
+
// interpolation and pluralization, based off of Airbnb's
|
11
|
+
// experience adding I18n functionality to its Backbone.js and Node apps.
|
12
|
+
//
|
13
|
+
// Polylglot is agnostic to your translation backend. It doesn't perform any
|
14
|
+
// translation; it simply gives you a way to manage translated phrases from
|
15
|
+
// your client- or server-side JavaScript application.
|
16
|
+
//
|
17
|
+
|
18
|
+
!function(root) {
|
19
|
+
'use strict';
|
20
|
+
|
21
|
+
// ### Polyglot class constructor
|
22
|
+
function Polyglot(options) {
|
23
|
+
options = options || {};
|
24
|
+
this.phrases = {};
|
25
|
+
this.extend(options.phrases || {});
|
26
|
+
this.currentLocale = options.locale || 'en';
|
27
|
+
this.allowMissing = !!options.allowMissing;
|
28
|
+
this.warn = options.warn || warn;
|
29
|
+
}
|
30
|
+
|
31
|
+
// ### Version
|
32
|
+
Polyglot.VERSION = '0.4.2';
|
33
|
+
|
34
|
+
// ### polyglot.locale([locale])
|
35
|
+
//
|
36
|
+
// Get or set locale. Internally, Polyglot only uses locale for pluralization.
|
37
|
+
Polyglot.prototype.locale = function(newLocale) {
|
38
|
+
if (newLocale) this.currentLocale = newLocale;
|
39
|
+
return this.currentLocale;
|
40
|
+
};
|
41
|
+
|
42
|
+
// ### polyglot.extend(phrases)
|
43
|
+
//
|
44
|
+
// Use `extend` to tell Polyglot how to translate a given key.
|
45
|
+
//
|
46
|
+
// polyglot.extend({
|
47
|
+
// "hello": "Hello",
|
48
|
+
// "hello_name": "Hello, %{name}"
|
49
|
+
// });
|
50
|
+
//
|
51
|
+
// The key can be any string. Feel free to call `extend` multiple times;
|
52
|
+
// it will override any phrases with the same key, but leave existing phrases
|
53
|
+
// untouched.
|
54
|
+
//
|
55
|
+
// It is also possible to pass nested phrase objects, which get flattened
|
56
|
+
// into an object with the nested keys concatenated using dot notation.
|
57
|
+
//
|
58
|
+
// polyglot.extend({
|
59
|
+
// "nav": {
|
60
|
+
// "hello": "Hello",
|
61
|
+
// "hello_name": "Hello, %{name}",
|
62
|
+
// "sidebar": {
|
63
|
+
// "welcome": "Welcome"
|
64
|
+
// }
|
65
|
+
// }
|
66
|
+
// });
|
67
|
+
//
|
68
|
+
// console.log(polyglot.phrases);
|
69
|
+
// // {
|
70
|
+
// // 'nav.hello': 'Hello',
|
71
|
+
// // 'nav.hello_name': 'Hello, %{name}',
|
72
|
+
// // 'nav.sidebar.welcome': 'Welcome'
|
73
|
+
// // }
|
74
|
+
//
|
75
|
+
// `extend` accepts an optional second argument, `prefix`, which can be used
|
76
|
+
// to prefix every key in the phrases object with some string, using dot
|
77
|
+
// notation.
|
78
|
+
//
|
79
|
+
// polyglot.extend({
|
80
|
+
// "hello": "Hello",
|
81
|
+
// "hello_name": "Hello, %{name}"
|
82
|
+
// }, "nav");
|
83
|
+
//
|
84
|
+
// console.log(polyglot.phrases);
|
85
|
+
// // {
|
86
|
+
// // 'nav.hello': 'Hello',
|
87
|
+
// // 'nav.hello_name': 'Hello, %{name}'
|
88
|
+
// // }
|
89
|
+
//
|
90
|
+
// This feature is used internally to support nested phrase objects.
|
91
|
+
Polyglot.prototype.extend = function(morePhrases, prefix) {
|
92
|
+
var phrase;
|
93
|
+
|
94
|
+
for (var key in morePhrases) {
|
95
|
+
if (morePhrases.hasOwnProperty(key)) {
|
96
|
+
phrase = morePhrases[key];
|
97
|
+
if (prefix) key = prefix + '.' + key;
|
98
|
+
if (typeof phrase === 'object') {
|
99
|
+
this.extend(phrase, key);
|
100
|
+
} else {
|
101
|
+
this.phrases[key] = phrase;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
};
|
106
|
+
|
107
|
+
// ### polyglot.clear()
|
108
|
+
//
|
109
|
+
// Clears all phrases. Useful for special cases, such as freeing
|
110
|
+
// up memory if you have lots of phrases but no longer need to
|
111
|
+
// perform any translation. Also used internally by `replace`.
|
112
|
+
Polyglot.prototype.clear = function() {
|
113
|
+
this.phrases = {};
|
114
|
+
};
|
115
|
+
|
116
|
+
// ### polyglot.replace(phrases)
|
117
|
+
//
|
118
|
+
// Completely replace the existing phrases with a new set of phrases.
|
119
|
+
// Normally, just use `extend` to add more phrases, but under certain
|
120
|
+
// circumstances, you may want to make sure no old phrases are lying around.
|
121
|
+
Polyglot.prototype.replace = function(newPhrases) {
|
122
|
+
this.clear();
|
123
|
+
this.extend(newPhrases);
|
124
|
+
};
|
125
|
+
|
126
|
+
|
127
|
+
// ### polyglot.t(key, options)
|
128
|
+
//
|
129
|
+
// The most-used method. Provide a key, and `t` will return the
|
130
|
+
// phrase.
|
131
|
+
//
|
132
|
+
// polyglot.t("hello");
|
133
|
+
// => "Hello"
|
134
|
+
//
|
135
|
+
// The phrase value is provided first by a call to `polyglot.extend()` or
|
136
|
+
// `polyglot.replace()`.
|
137
|
+
//
|
138
|
+
// Pass in an object as the second argument to perform interpolation.
|
139
|
+
//
|
140
|
+
// polyglot.t("hello_name", {name: "Spike"});
|
141
|
+
// => "Hello, Spike"
|
142
|
+
//
|
143
|
+
// If you like, you can provide a default value in case the phrase is missing.
|
144
|
+
// Use the special option key "_" to specify a default.
|
145
|
+
//
|
146
|
+
// polyglot.t("i_like_to_write_in_language", {
|
147
|
+
// _: "I like to write in %{language}.",
|
148
|
+
// language: "JavaScript"
|
149
|
+
// });
|
150
|
+
// => "I like to write in JavaScript."
|
151
|
+
//
|
152
|
+
Polyglot.prototype.t = function(key, options) {
|
153
|
+
var phrase, result;
|
154
|
+
options = options == null ? {} : options;
|
155
|
+
// allow number as a pluralization shortcut
|
156
|
+
if (typeof options === 'number') {
|
157
|
+
options = {smart_count: options};
|
158
|
+
}
|
159
|
+
if (typeof this.phrases[key] === 'string') {
|
160
|
+
phrase = this.phrases[key];
|
161
|
+
} else if (typeof options._ === 'string') {
|
162
|
+
phrase = options._;
|
163
|
+
} else if (this.allowMissing) {
|
164
|
+
phrase = key;
|
165
|
+
} else {
|
166
|
+
this.warn('Missing translation for key: "'+key+'"');
|
167
|
+
result = key;
|
168
|
+
}
|
169
|
+
if (typeof phrase === 'string') {
|
170
|
+
options = clone(options);
|
171
|
+
result = choosePluralForm(phrase, this.currentLocale, options.smart_count);
|
172
|
+
result = interpolate(result, options);
|
173
|
+
}
|
174
|
+
return result;
|
175
|
+
};
|
176
|
+
|
177
|
+
|
178
|
+
// #### Pluralization methods
|
179
|
+
// The string that separates the different phrase possibilities.
|
180
|
+
var delimeter = '||||';
|
181
|
+
|
182
|
+
// Mapping from pluralization group plural logic.
|
183
|
+
var pluralTypes = {
|
184
|
+
chinese: function(n) { return 0; },
|
185
|
+
german: function(n) { return n !== 1 ? 1 : 0; },
|
186
|
+
french: function(n) { return n > 1 ? 1 : 0; },
|
187
|
+
russian: function(n) { return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; },
|
188
|
+
czech: function(n) { return (n === 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2; },
|
189
|
+
polish: function(n) { return (n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2); },
|
190
|
+
icelandic: function(n) { return (n % 10 !== 1 || n % 100 === 11) ? 1 : 0; }
|
191
|
+
};
|
192
|
+
|
193
|
+
// Mapping from pluralization group to individual locales.
|
194
|
+
var pluralTypeToLanguages = {
|
195
|
+
chinese: ['fa', 'id', 'ja', 'ko', 'lo', 'ms', 'th', 'tr', 'zh'],
|
196
|
+
german: ['da', 'de', 'en', 'es', 'fi', 'el', 'he', 'hu', 'it', 'nl', 'no', 'pt', 'sv'],
|
197
|
+
french: ['fr', 'tl', 'pt-br'],
|
198
|
+
russian: ['hr', 'ru'],
|
199
|
+
czech: ['cs'],
|
200
|
+
polish: ['pl'],
|
201
|
+
icelandic: ['is']
|
202
|
+
};
|
203
|
+
|
204
|
+
function langToTypeMap(mapping) {
|
205
|
+
var type, langs, l, ret = {};
|
206
|
+
for (type in mapping) {
|
207
|
+
if (mapping.hasOwnProperty(type)) {
|
208
|
+
langs = mapping[type];
|
209
|
+
for (l in langs) {
|
210
|
+
ret[langs[l]] = type;
|
211
|
+
}
|
212
|
+
}
|
213
|
+
}
|
214
|
+
return ret;
|
215
|
+
}
|
216
|
+
|
217
|
+
// Trim a string.
|
218
|
+
function trim(str){
|
219
|
+
var trimRe = /^\s+|\s+$/g;
|
220
|
+
return str.replace(trimRe, '');
|
221
|
+
}
|
222
|
+
|
223
|
+
// Based on a phrase text that contains `n` plural forms separated
|
224
|
+
// by `delimeter`, a `locale`, and a `count`, choose the correct
|
225
|
+
// plural form, or none if `count` is `null`.
|
226
|
+
function choosePluralForm(text, locale, count){
|
227
|
+
var ret, texts, chosenText;
|
228
|
+
if (count != null && text) {
|
229
|
+
texts = text.split(delimeter);
|
230
|
+
chosenText = texts[pluralTypeIndex(locale, count)] || texts[0];
|
231
|
+
ret = trim(chosenText);
|
232
|
+
} else {
|
233
|
+
ret = text;
|
234
|
+
}
|
235
|
+
return ret;
|
236
|
+
}
|
237
|
+
|
238
|
+
function pluralTypeName(locale) {
|
239
|
+
var langToPluralType = langToTypeMap(pluralTypeToLanguages);
|
240
|
+
return langToPluralType[locale] || langToPluralType.en;
|
241
|
+
}
|
242
|
+
|
243
|
+
function pluralTypeIndex(locale, count) {
|
244
|
+
return pluralTypes[pluralTypeName(locale)](count);
|
245
|
+
}
|
246
|
+
|
247
|
+
// ### interpolate
|
248
|
+
//
|
249
|
+
// Does the dirty work. Creates a `RegExp` object for each
|
250
|
+
// interpolation placeholder.
|
251
|
+
function interpolate(phrase, options) {
|
252
|
+
for (var arg in options) {
|
253
|
+
if (arg !== '_' && options.hasOwnProperty(arg)) {
|
254
|
+
// We create a new `RegExp` each time instead of using a more-efficient
|
255
|
+
// string replace so that the same argument can be replaced multiple times
|
256
|
+
// in the same phrase.
|
257
|
+
phrase = phrase.replace(new RegExp('%\\{'+arg+'\\}', 'g'), options[arg]);
|
258
|
+
}
|
259
|
+
}
|
260
|
+
return phrase;
|
261
|
+
}
|
262
|
+
|
263
|
+
// ### warn
|
264
|
+
//
|
265
|
+
// Provides a warning in the console if a phrase key is missing.
|
266
|
+
function warn(message) {
|
267
|
+
root.console && root.console.warn && root.console.warn('WARNING: ' + message);
|
268
|
+
}
|
269
|
+
|
270
|
+
// ### clone
|
271
|
+
//
|
272
|
+
// Clone an object.
|
273
|
+
function clone(source) {
|
274
|
+
var ret = {};
|
275
|
+
for (var prop in source) {
|
276
|
+
ret[prop] = source[prop];
|
277
|
+
}
|
278
|
+
return ret;
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
// Export for Node, attach to `window` for browser.
|
283
|
+
if (typeof module !== 'undefined' && module.exports) {
|
284
|
+
module.exports = Polyglot;
|
285
|
+
} else {
|
286
|
+
root.Polyglot = Polyglot;
|
287
|
+
}
|
288
|
+
|
289
|
+
}(this);
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polyglot-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juri Kern
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Give your JavaScript the ability to speak many languages
|
28
|
+
email:
|
29
|
+
- juri@sent.at
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- lib/polyglot.rb
|
38
|
+
- lib/polyglot/helper.rb
|
39
|
+
- lib/polyglot/version.rb
|
40
|
+
- vendor/assets/javascripts/polyglot.js
|
41
|
+
homepage: https://github.com/JuriKern/polyglot-rails
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.4.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Give your JavaScript the ability to speak many languages
|
65
|
+
test_files: []
|