tml-rails 4.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +368 -0
- data/Rakefile +61 -0
- data/app/controllers/tml_rails/tools_controller.rb +56 -0
- data/app/views/tml_rails/tags/_language_selector.html.erb +82 -0
- data/app/views/tml_rails/tags/_language_strip.html.erb +20 -0
- data/app/views/tml_rails/tags/_powered_by_trex.html.erb +18 -0
- data/app/views/tml_rails/tags/_scripts.html.erb +44 -0
- data/config/routes.rb +36 -0
- data/lib/i18n/backend/tml.rb +62 -0
- data/lib/tasks/tml.rake +60 -0
- data/lib/tml/cache_adapters/rails.rb +98 -0
- data/lib/tml_rails.rb +41 -0
- data/lib/tml_rails/core/string.rb +44 -0
- data/lib/tml_rails/engine.rb +39 -0
- data/lib/tml_rails/extensions/action_common_methods.rb +131 -0
- data/lib/tml_rails/extensions/action_controller_extension.rb +137 -0
- data/lib/tml_rails/extensions/action_view_extension.rb +188 -0
- data/lib/tml_rails/railtie.rb +52 -0
- data/lib/tml_rails/version.rb +34 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea4cb3ea79b68af9965b9ce880e0dec8ca778d61
|
4
|
+
data.tar.gz: cd95300b3e36dc0480573ab8e53e4f07194ae4ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 974e8ec10757b6f19bd0de26d0162e73ec7c9110e1151705ab61f2044ba4a3a197df1a5f1ad22ced393f12b3ea7a643f809308034decce8d77b91a52041ac57b
|
7
|
+
data.tar.gz: 360816e53ff4246e012cc6e11553c7dc731b88ff1d491af7477926a4996991f2f91fbdbefeb6bf889b9e9929125cd621a1e78b59f9a5cfe4f85a291a7088a8d1
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,368 @@
|
|
1
|
+
<p align="center">
|
2
|
+
<img src="https://avatars0.githubusercontent.com/u/1316274?v=3&s=200">
|
3
|
+
</p>
|
4
|
+
|
5
|
+
Tml for Ruby on Rails
|
6
|
+
===================================
|
7
|
+
[![Build Status](https://travis-ci.org/translationexchange/tml-rails.png?branch=master)](https://travis-ci.org/translationexchange/tml-rails)
|
8
|
+
[![Coverage Status](https://coveralls.io/repos/translationexchange/tml-rails/badge.png)](https://coveralls.io/r/translationexchange/tml-rails)
|
9
|
+
[![Dependency Status](https://www.versioneye.com/user/projects/54c145bb6c0035c5920001c4/badge.svg?style=flat)](https://www.versioneye.com/user/projects/54c145bb6c0035c5920001c4)
|
10
|
+
[![Gem Version](https://badge.fury.io/rb/tml-rails.png)](http://badge.fury.io/rb/tml-rails)
|
11
|
+
|
12
|
+
This Client SDK provides tools for translating Rails applications into any language using the TranslationExchange.com service.
|
13
|
+
|
14
|
+
|
15
|
+
Installation
|
16
|
+
===================================
|
17
|
+
|
18
|
+
To integrate Tml SDK into your own application, add the gem to your app:
|
19
|
+
|
20
|
+
Add the following gem to your Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'tml-rails'
|
24
|
+
```
|
25
|
+
|
26
|
+
Install the gems:
|
27
|
+
|
28
|
+
```sh
|
29
|
+
$ bundle install
|
30
|
+
```
|
31
|
+
|
32
|
+
|
33
|
+
Configuration
|
34
|
+
===================================
|
35
|
+
|
36
|
+
Create a tml initializer file with the following configuration:
|
37
|
+
|
38
|
+
config/initializers/tml.rb
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
Tml.configure do |config|
|
42
|
+
config.application = {
|
43
|
+
:token => YOUR_APPLICATION_TOKEN,
|
44
|
+
}
|
45
|
+
config.cache = {
|
46
|
+
:adapter => 'rails',
|
47
|
+
:version => 1
|
48
|
+
}
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Tml must be used with caching enabled.
|
53
|
+
|
54
|
+
If you are already using Rails caching, you probably already specify it in your production file, like the following:
|
55
|
+
|
56
|
+
config/environments/production.rb
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
config.cache_store = :mem_cache_store, Dalli::Client.new('localhost:11211', {:namespace => 'myapplication'})
|
60
|
+
config.identity_cache_store = :mem_cache_store, Dalli::Client.new('localhost:11211', {:namespace => 'myapplication'})
|
61
|
+
```
|
62
|
+
|
63
|
+
Then all you need to do is specify that you want to use the Rails cache adapter:
|
64
|
+
|
65
|
+
config/initializers/tml.rb
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
config.cache = {
|
69
|
+
:enabled => true,
|
70
|
+
:adapter => 'rails',
|
71
|
+
:version => 1
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
Alternatively, you can provide a separate memcache server to store your translations:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
config.cache = {
|
79
|
+
:enabled => true,
|
80
|
+
:adapter => 'memcache',
|
81
|
+
:host => 'localhost:11211',
|
82
|
+
:version => 1,
|
83
|
+
:timeout => 3600
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
You can also use Redis to persist your translations cache:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
config.cache = {
|
91
|
+
:enabled => true,
|
92
|
+
:adapter => 'redis',
|
93
|
+
:host => 'localhost:6379',
|
94
|
+
:version => 1,
|
95
|
+
:timeout => 3600
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
The above examples use shared caching model - all your Rails processes on all your servers share the same translation cache. This approach
|
100
|
+
will save you memory space, as well as allow you to invalidate/redeploy your translations cache without having to redeploy your application.
|
101
|
+
|
102
|
+
To update the cache, execute the following line of code:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
Tml.cache.upgrade_version
|
106
|
+
```
|
107
|
+
|
108
|
+
Or you can run the rake command from any of your app instances:
|
109
|
+
|
110
|
+
```sh
|
111
|
+
$ bundle exec rake tml:cache:upgrade
|
112
|
+
```
|
113
|
+
|
114
|
+
This will effectively invalidate your current cache and rebuilt it with the latest translations from Translation Exchange CDN.
|
115
|
+
|
116
|
+
An alternative approach is to pre-generate all your cache files when you deploy your application. The translation cache will be loaded and stored in every process on every server,
|
117
|
+
but it will be faster at serving translations and this approach does not require cache warmup.
|
118
|
+
|
119
|
+
To specify in-memory, file-based cache, provide the following configuration:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
config.cache = {
|
123
|
+
:enabled => true,
|
124
|
+
:adapter => 'file',
|
125
|
+
:path => 'config/tml',
|
126
|
+
:version => 'current',
|
127
|
+
:segmented => false
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
If you set ':segmented' to 'true', the cache will be generated for each source in your application. Otherwise, a single cache file will be generated per language for the entire application.
|
132
|
+
|
133
|
+
The file based cache must be generated before you deploy your application using the following command:
|
134
|
+
|
135
|
+
```sh
|
136
|
+
$ bundle exec rake tml:cache:generate
|
137
|
+
```
|
138
|
+
|
139
|
+
You can also rollback to the previous file cache using:
|
140
|
+
|
141
|
+
```sh
|
142
|
+
$ bundle exec rake tml:cache:rollback
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
Integration
|
147
|
+
===================================
|
148
|
+
|
149
|
+
In the HEAD section of your layout, add:
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
<%= tml_scripts_tag %>
|
153
|
+
```
|
154
|
+
|
155
|
+
Now you can simply add the default language selector anywhere on your page using:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
<%= tml_language_selector_tag %>
|
159
|
+
```
|
160
|
+
|
161
|
+
The default language selector is used to enable/disable translation modes. It may be useful on staging or translation
|
162
|
+
server, but it may not be ideal on production. There are a number of alternative language selectors you can use, including your own.
|
163
|
+
|
164
|
+
To use a dropdown language selector that uses locale in the url parameter, use:
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
<%= tml_language_selector_tag(:dropdown, {
|
168
|
+
:style => "margin-top:15px",
|
169
|
+
:language => :english
|
170
|
+
}) %>
|
171
|
+
```
|
172
|
+
|
173
|
+
To use a list language selector that uses locale in the url parameter, use:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
<%= tml_language_selector_tag(:list, {
|
177
|
+
:style => "margin-top:15px",
|
178
|
+
:language => :english
|
179
|
+
}) %>
|
180
|
+
```
|
181
|
+
|
182
|
+
If you want to see just a list of flags, use:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
<%= tml_language_selector_tag(:list, {
|
186
|
+
:flags_only => true
|
187
|
+
}) %>
|
188
|
+
```
|
189
|
+
|
190
|
+
Now you can use Tml's helper methods and TML (Translation Markup Language) to translate your strings:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
<%= tr("Hello World") %>
|
194
|
+
<%= tr("You have {count || message}", count: 5) %>
|
195
|
+
<%= tr("{actor} sent {target} [bold: {count || gift}]", actor: actor_user, target: target_user, count: 5) %>
|
196
|
+
<%= tr("{actor} uploaded [bold: {count || photo}] to {actor | his, her} photo album.", actor: current_user, count: 5) %>
|
197
|
+
...
|
198
|
+
```
|
199
|
+
|
200
|
+
There are some additional methods that can be very useful. For instance, if you want to translate something inside a model,
|
201
|
+
you can simply use:
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
"Hello World".translate
|
205
|
+
```
|
206
|
+
|
207
|
+
Learn more about TML at: http://TranslationExchange.com/docs
|
208
|
+
|
209
|
+
|
210
|
+
I18n fallback
|
211
|
+
===================================
|
212
|
+
|
213
|
+
If your application is already using the standard i18n framework, you can mix in the Tml framework on top of it. To do so,
|
214
|
+
you need to set the i18n backend to Tml. Add the following line to your tml initializer:
|
215
|
+
|
216
|
+
config/initializers/tml.rb
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
I18n.backend = I18n::Backend::Tml.new
|
220
|
+
```
|
221
|
+
|
222
|
+
Now the "t" function will automatically fallback onto "tr" function. If you have the following in your base language file:
|
223
|
+
|
224
|
+
config/locales/en.yml
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
en:
|
228
|
+
hello: "Hello world"
|
229
|
+
my:
|
230
|
+
nested:
|
231
|
+
key: "This is a nested key"
|
232
|
+
```
|
233
|
+
|
234
|
+
Then you can call:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
<%= t(:hello) %>
|
238
|
+
<%= t("my.nested.key") %>
|
239
|
+
```
|
240
|
+
|
241
|
+
And the i18n will use Translation Exchange as the backend for your translations. On top of it, you can continue using Tml's extensions:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
<%= tr("Hello World") %>
|
245
|
+
<%= tr("This is a nested key") %>
|
246
|
+
```
|
247
|
+
|
248
|
+
The above calls are equivalent.
|
249
|
+
|
250
|
+
|
251
|
+
Logging
|
252
|
+
===================================
|
253
|
+
|
254
|
+
Tml comes with its own logger. If you would like to see what the SDK is doing behind the scene, enable the logger and provide the file path for the log file:
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
Tml.configure do |config|
|
258
|
+
|
259
|
+
config.logger = {
|
260
|
+
:enabled => true,
|
261
|
+
:path => "#{Rails.root}/log/tml.log",
|
262
|
+
:level => 'debug'
|
263
|
+
}
|
264
|
+
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
Customization
|
269
|
+
===================================
|
270
|
+
|
271
|
+
Tml comes with default settings for the rules engine. For example, it assumes that you have the following methods in your ApplicationController:
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
def current_user
|
275
|
+
end
|
276
|
+
|
277
|
+
def current_locale
|
278
|
+
end
|
279
|
+
```
|
280
|
+
|
281
|
+
Tml only needs the current_user method if your site needs to use gender based rules for the viewing user.
|
282
|
+
|
283
|
+
Similarly, if you prefer to use your own mechanism for storing and retrieving user's preferred and selected locales, you can provide the current_locale method.
|
284
|
+
|
285
|
+
If you need to adjust those method names, you can set them in the config:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
Tml.configure do |config|
|
289
|
+
|
290
|
+
config.current_user_method = :my_user
|
291
|
+
|
292
|
+
config.current_locale_method = :my_locale
|
293
|
+
|
294
|
+
end
|
295
|
+
```
|
296
|
+
|
297
|
+
|
298
|
+
Tml Client SDK Sample
|
299
|
+
===================================
|
300
|
+
|
301
|
+
The best way to start using the SDK is to run a sample application that comes bundled with this SDK.
|
302
|
+
|
303
|
+
```sh
|
304
|
+
$ git clone https://github.com/tml/tml_rails_clientsdk.git
|
305
|
+
$ cd tml_rails_clientsdk/spec/dummy
|
306
|
+
$ bundle
|
307
|
+
```
|
308
|
+
|
309
|
+
Before you running the application, visit TranslationExchange.com, register as a new user and create a new application.
|
310
|
+
|
311
|
+
Update your key and secret in the following file: config/initializers/tml.rb
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
config.application = {
|
315
|
+
:key => YOUR_KEY,
|
316
|
+
:secret => YOUR_SECRET
|
317
|
+
}
|
318
|
+
```
|
319
|
+
|
320
|
+
Now you can start the application by running:
|
321
|
+
|
322
|
+
```sh
|
323
|
+
$ bundle exec rails s
|
324
|
+
```
|
325
|
+
|
326
|
+
|
327
|
+
This will start the dummy application on port 3000. Open your browser and point to:
|
328
|
+
|
329
|
+
http://localhost:3000
|
330
|
+
|
331
|
+
|
332
|
+
Links
|
333
|
+
==================
|
334
|
+
|
335
|
+
* Register on TranslationExchange.com: http://translationexchange.com
|
336
|
+
|
337
|
+
* Read Translation Exchange documentation: http://translationexchange.com/docs
|
338
|
+
|
339
|
+
* Follow TranslationExchange on Twitter: https://twitter.com/translationx
|
340
|
+
|
341
|
+
* Connect with TranslationExchange on Facebook: https://www.facebook.com/translationexchange
|
342
|
+
|
343
|
+
* If you have any questions or suggestions, contact us: feedback@translationexchange.com
|
344
|
+
|
345
|
+
|
346
|
+
Copyright and license
|
347
|
+
==================
|
348
|
+
|
349
|
+
Copyright (c) 2015 Translation Exchange Inc
|
350
|
+
|
351
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
352
|
+
a copy of this software and associated documentation files (the
|
353
|
+
"Software"), to deal in the Software without restriction, including
|
354
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
355
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
356
|
+
permit persons to whom the Software is furnished to do so, subject to
|
357
|
+
the following conditions:
|
358
|
+
|
359
|
+
The above copyright notice and this permission notice shall be
|
360
|
+
included in all copies or substantial portions of the Software.
|
361
|
+
|
362
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
363
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
364
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
365
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
366
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
367
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
368
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com
|
3
|
+
#
|
4
|
+
# _______ _ _ _ ______ _
|
5
|
+
# |__ __| | | | | (_) | ____| | |
|
6
|
+
# | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
|
7
|
+
# | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
|
8
|
+
# | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
|
9
|
+
# |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
|
10
|
+
# __/ |
|
11
|
+
# |___/
|
12
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
13
|
+
# a copy of this software and associated documentation files (the
|
14
|
+
# "Software"), to deal in the Software without restriction, including
|
15
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
16
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
17
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
18
|
+
# the following conditions:
|
19
|
+
#
|
20
|
+
# The above copyright notice and this permission notice shall be
|
21
|
+
# included in all copies or substantial portions of the Software.
|
22
|
+
#
|
23
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
24
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
25
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
26
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
27
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
28
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
29
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
30
|
+
#++
|
31
|
+
|
32
|
+
#!/usr/bin/env rake
|
33
|
+
#begin
|
34
|
+
# require 'bundler/setup'
|
35
|
+
#rescue LoadError
|
36
|
+
# puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
37
|
+
#end
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'rdoc/task'
|
41
|
+
rescue LoadError
|
42
|
+
require 'rdoc/rdoc'
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
RDoc::Task = Rake::RDocTask
|
45
|
+
end
|
46
|
+
|
47
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = 'Tml Client SDK for Ruby on Rails'
|
50
|
+
rdoc.options << '--line-numbers'
|
51
|
+
rdoc.rdoc_files.include('README.rdoc')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
54
|
+
|
55
|
+
Dir["#{File.dirname(__FILE__)}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
|
56
|
+
|
57
|
+
require 'rspec/core/rake_task'
|
58
|
+
|
59
|
+
RSpec::Core::RakeTask.new(:spec)
|
60
|
+
|
61
|
+
task :default => :spec
|