tr8n_client_sdk 3.2.2 → 3.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9802bfd002589491b5e71c353cad0372d5d251c1
4
- data.tar.gz: c073f2d7078cc2183cf7178c46f1b113d33d9e29
3
+ metadata.gz: b1f8b6538d43509ecfd11b6153371c54e153eca5
4
+ data.tar.gz: 32beb12f88d7dd8b16ea9e66dbb4f882f1d5b9f1
5
5
  SHA512:
6
- metadata.gz: 21979fba2779078034813c6ebf2a0ff746c97a497bffc53962e0ac9a4ecf6ad697e28cbe9d53d4f996b584a145dec53a60a5371fe7534c0c09dbe78ff930e5f5
7
- data.tar.gz: 62eb86e095c80117194b1b54990d014262892b77fdaf54e9a99af5a1fd72397c452f82c28d1b1386ae99b4e1a0004b3c7a3cdf057cf73e1af7d38de43d7ce353
6
+ metadata.gz: aadb32a0a3610db0859c5cb049dba6b1d3844986ec5985f789dba37f8c68bd28e811f8d1315860bb4f478ae939e502288ad406a5fbe2c69eb1a1c3bcb5a8966a
7
+ data.tar.gz: 4af105edf554a27952a0079545b020aa2544716a3ac948dbbe5a05e2197fbd740e1b2ceacf5b0fa2916df3c513b1cc9ec55c977c5bbe62724bb2c5c82eed7912
data/README.md CHANGED
@@ -4,49 +4,60 @@
4
4
 
5
5
  Tr8n Client SDK for Ruby on Rails
6
6
  ===================================
7
-
8
- This Client SDK provides tools and extensions necessary for translating any Rails application using the Tr8n service.
9
-
10
- [![Build Status](https://travis-ci.org/tr8n/tr8n_rails_clientsdk.png?branch=master)](https://travis-ci.org/tr8n/tr8n_rails_clientsdk)
7
+ [![Build Status](https://travis-ci.org/tr8n/tr8n_rails_clientsdk.png?branch=rails-3.2.x)](https://travis-ci.org/tr8n/tr8n_rails_clientsdk)
11
8
  [![Coverage Status](https://coveralls.io/repos/tr8n/tr8n_rails_clientsdk/badge.png)](https://coveralls.io/r/tr8n/tr8n_rails_clientsdk)
12
9
  [![Gem Version](https://badge.fury.io/rb/tr8n_client_sdk.png)](http://badge.fury.io/rb/tr8n_client_sdk)
13
- [![Dependency Status](https://www.versioneye.com/user/projects/52e4bc4cec1375b57600000f/badge.png)](https://www.versioneye.com/user/projects/52e4bc4cec1375b57600000f)
10
+ [![Dependency Status](https://www.versioneye.com/user/projects/53cda6a5225426f1e8000165/badge.svg?style=flat)](https://www.versioneye.com/user/projects/53cda6a5225426f1e8000165)
14
11
  [![Project status](http://stillmaintained.com/tr8n/tr8n_ruby_core.png)](http://stillmaintained.com/tr8n/tr8n_ruby_core.png)
15
12
 
13
+ This Client SDK provides extensions necessary for translating Rails 3.2.X based application using the TranslationExchange.com service.
14
+
16
15
 
17
- Runing the Client SDK Sample
16
+ Tr8n Client SDK Sample
18
17
  ===================================
19
18
 
19
+ The best way to start using the SDK is to run a sample application that comes bundled with this SDK.
20
20
 
21
- To run the gem as a stand-alone application follow these:
21
+ ```sh
22
+ $ git clone https://github.com/tr8n/tr8n_rails_clientsdk.git
23
+ $ cd tr8n_rails_clientsdk/spec/dummy
24
+ $ bundle
25
+ ```
26
+
27
+ Before you running the application, visit TranslationExchange.com, register as a new user and create a new application.
28
+
29
+ Update your key and secret in the following file: config/initializers/tr8n.rb
30
+
31
+ ```ruby
32
+ config.application = {
33
+ :key => YOUR_KEY,
34
+ :secret => YOUR_SECRET
35
+ }
36
+ ```
22
37
 
23
- Make sure you edit the config/tr8n/config.yml file and provide the correct application host, key and secret for your application.
38
+ Now you can start the application by running:
24
39
 
25
40
  ```sh
26
- $ git clone https://github.com/tr8n/tr8n_rails_clientsdk.git
27
- $ cd tr8n_rails_clientsdk/test/dummy
28
- $ bundle install
29
- $ rake db:migrate
30
- $ rails s
41
+ $ bundle exec rails s
31
42
  ```
32
43
 
33
- Alternatively, you can see the same sample application as a stand alone app:
34
44
 
35
- https://github.com/tr8n/tr8n_rails_clientsdk_sample
45
+ This will start the dummy application on port 3000. Open your browser and point to:
46
+
47
+ http://localhost:3000
36
48
 
37
- This application is running at:
38
49
 
39
- http://rails.tr8nhub.com
40
50
 
41
- # Integration Instructions
42
51
 
43
- Here are a few points on how to integrate Tr8n into your app:
52
+ Integration
53
+ ===================================
54
+
55
+ To integrate Tr8n SDK into your own app, all you need to do is:
44
56
 
45
- Add the following gems to your Gemfile:
57
+ Add the following gem to your Gemfile:
46
58
 
47
59
  ```ruby
48
- gem 'tr8n_core'
49
- gem 'tr8n_client_sdk'
60
+ gem 'tr8n_client_sdk', '~> 3.2'
50
61
  ```
51
62
 
52
63
  Install the gems:
@@ -55,21 +66,50 @@ Install the gems:
55
66
  $ bundle
56
67
  ```
57
68
 
58
- Generate config file:
69
+ Create an initializer file under config/initializers/tr8n.rb and add the following configuration:
59
70
 
60
- ```sh
61
- $ rails g tr8n_client_sdk
71
+ ```ruby
72
+ Tr8n.configure do |config|
73
+ config.application = {
74
+ :key => YOUR_APP_KEY,
75
+ :secret => YOUR_APP_SECRET
76
+ }
77
+ end
62
78
  ```
63
79
 
64
- Open the config file and provide your application credentials:
80
+ Tr8n is best run with caching enabled. You can specify your caching adapter using the following settings:
81
+
82
+ Memcache example:
65
83
 
84
+ ```ruby
85
+ Tr8n.configure do |config|
86
+ config.cache = {
87
+ :enabled => true,
88
+ :adapter => 'memcache',
89
+ :host => 'localhost:11211',
90
+ :version => 1,
91
+ :timeout => 3600
92
+ }
93
+ end
66
94
  ```
67
- application:
68
- host: http://localhost:3000
69
- key: YOUR_APP_KEY
70
- secret: YOUR_APP_SECRET
95
+
96
+ Redis example:
97
+
98
+ ```ruby
99
+ Tr8n.configure do |config|
100
+ config.cache = {
101
+ :enabled => true,
102
+ :adapter => 'redis',
103
+ :host => 'localhost:6379',
104
+ :version => 1,
105
+ :timeout => 3600
106
+ }
107
+ end
71
108
  ```
72
109
 
110
+ To see a full list of supported cache adapters or to create your own cache adapter, visit TranslationExchange.com/docs
111
+
112
+
73
113
  In the HEAD section of your layout, add:
74
114
 
75
115
  ```ruby
@@ -78,6 +118,129 @@ In the HEAD section of your layout, add:
78
118
 
79
119
  You are done, tr8n is now running in your app.
80
120
 
81
- To read more about what you can do with it, visit the wiki site:
121
+ Now you can simply add the default language selector anywhere on your page using:
122
+
123
+ ```ruby
124
+ <%= tr8n_language_selector_tag %>
125
+ ```
126
+
127
+ And use TML (Translation Markup Language) to translate your strings, using:
128
+
129
+ ```rails
130
+ <%= tr("Hello World") %>
131
+ <%= tr("You have {count||message}", :count => 5) %>
132
+ <%= tr("{actor} sent {target} [bold: {count||gift}]", :actor => actor_user, :target => target_user, :count => 5) %>
133
+ ...
134
+ ```
135
+
136
+ Learn more about TML at: https://TranslationExchange.com/docs
137
+
138
+
139
+ Caching
140
+ ===================================
141
+
142
+ To reset/upgrade your cache, you can simply call
143
+
144
+ ```ruby
145
+ Tr8n.cache.upgrade_version
146
+ ```
147
+
148
+ All the keys stored in memory based cache are versioned. By upgrading the version you will effectively invalidate the old keys and the new keys will be loaded from the translation service.
149
+
150
+
151
+ File adapters require pre-generation. You can pre-generate your cache by running:
152
+
153
+ ```sh
154
+ $ rake tr8n:generate_cache:cdb
155
+ ```
156
+
157
+ You can also do a combination of file-based adapters (for persistent cache) and memory-based adapters for serving the translations.
158
+
159
+
160
+ Logging
161
+ ===================================
162
+
163
+ Tr8n 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:
164
+
165
+ ```ruby
166
+ Tr8n.configure do |config|
167
+
168
+ config.logger = {
169
+ :enabled => true,
170
+ :path => "#{Rails.root}/log/tr8n.log",
171
+ :level => 'debug'
172
+ }
173
+
174
+ end
175
+ ```
176
+
177
+
178
+ Customization
179
+ ===================================
180
+
181
+ Tr8n comes with default settings for the rules engine. For example, it assumes that you have the following methods in your ApplicationController:
182
+
183
+ ```ruby
184
+ def current_user
185
+ end
186
+
187
+ def current_locale
188
+ end
189
+ ```
190
+
191
+ Tr8n only needs the current_user method if your site needs to use gender based rules for the viewing user.
192
+
193
+ 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.
194
+
195
+ If you need to adjust those method names, you can set them in the config:
196
+
197
+ ```ruby
198
+ Tr8n.configure do |config|
199
+
200
+ config.current_user_method = :my_user
201
+
202
+ config.current_locale_method = :my_locale
203
+
204
+ end
205
+ ```
206
+
207
+
208
+ Links
209
+ ==================
210
+
211
+ * Register on TranslationExchange.com: https://translationexchange.com
212
+
213
+ * Read TranslationExchange's documentation: http://wiki.translationexchange.com
214
+
215
+ * Visit TranslationExchange's blog: http://blog.translationexchange.com
216
+
217
+ * Follow TranslationExchange on Twitter: https://twitter.com/translationx
218
+
219
+ * Connect with TranslationExchange on Facebook: https://www.facebook.com/translationexchange
220
+
221
+ * If you have any questions or suggestions, contact us: info@translationexchange.com
222
+
223
+
224
+ Copyright and license
225
+ ==================
226
+
227
+ Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
228
+
229
+ Permission is hereby granted, free of charge, to any person obtaining
230
+ a copy of this software and associated documentation files (the
231
+ "Software"), to deal in the Software without restriction, including
232
+ without limitation the rights to use, copy, modify, merge, publish,
233
+ distribute, sublicense, and/or sell copies of the Software, and to
234
+ permit persons to whom the Software is furnished to do so, subject to
235
+ the following conditions:
236
+
237
+ The above copyright notice and this permission notice shall be
238
+ included in all copies or substantial portions of the Software.
82
239
 
83
- http://wiki.tr8nhub.com
240
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
241
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
242
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
243
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
244
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
245
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
246
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,5 +1,53 @@
1
- <a href="#" onClick="Tr8n.UI.LanguageSelector.show(); return false;">
2
- <%= image_tag(tr8n_current_language.flag_url, :style => "align:middle") %>
3
- &nbsp;
4
- <%= tr8n_current_language.name %>
5
- </a>
1
+ <% if opts[:type] == :dropdown %>
2
+ <script>
3
+ function tr8n_change_locale(selector) {
4
+ var query_parts = window.location.href.split('?');
5
+ var query = query_parts.length > 1 ? query_parts[1] : null;
6
+ var params = {};
7
+ if (query) {
8
+ var vars = query.split('&');
9
+ for (var i = 0; i < vars.length; i++) {
10
+ var pair = vars[i].split('=');
11
+ params[pair[0]] = pair[1];
12
+ }
13
+ }
14
+ params['locale'] = selector.options[selector.selectedIndex].value;
15
+
16
+ query = [];
17
+ var keys = Object.keys(params);
18
+ for (var i = 0; i < keys.length; i++) {
19
+ query.push(encodeURIComponent(keys[i]) + "=" + encodeURIComponent(params[keys[i]]));
20
+ }
21
+
22
+ var destination = query_parts[0] + '?' + query.join("&");
23
+ window.location = destination;
24
+ }
25
+ </script>
26
+ <select id="tr8n_language_selector" onchange="tr8n_change_locale(this)" style="<%=opts[:style]%>" class="<%=opts[:class]%>">
27
+ <% tr8n_application.languages.each do |lang| %>
28
+ <option value="<%=lang.locale%>" <%="selected" if lang.locale == tr8n_current_locale %>>
29
+ <% if opts[:language] == :native %>
30
+ <%= lang.native_name %>
31
+ <% elsif opts[:language] == :english %>
32
+ <%= lang.english_name %>
33
+ <% else %>
34
+ <%= lang.name %>
35
+ <% end %>
36
+ </option>
37
+ <% end %>
38
+ </select>
39
+ <% else %>
40
+ <a href="#" onClick="Tr8n.UI.LanguageSelector.show(); return false;">
41
+ <% unless opts[:hide_flag] %>
42
+ <%= image_tag(tr8n_current_language.flag_url, :style => "align:middle") %>
43
+ &nbsp;
44
+ <% end %>
45
+ <% if opts[:language] == :native %>
46
+ <%= tr8n_current_language.native_name %>
47
+ <% elsif opts[:language] == :english %>
48
+ <%= tr8n_current_language.english_name %>
49
+ <% else %>
50
+ <%= tr8n_current_language.name %>
51
+ <% end %>
52
+ </a>
53
+ <% end %>
@@ -37,6 +37,13 @@ module Tr8nClientSdk
37
37
  base.send(:include, InstanceMethods)
38
38
  base.before_filter :tr8n_init_client_sdk
39
39
  base.after_filter :tr8n_reset_client_sdk
40
+ if defined? base.rescue_from
41
+ base.rescue_from 'Tr8n::Exception' do |e|
42
+ Tr8n.logger.error(e)
43
+ Tr8n.logger.error(e.backtrace)
44
+ raise e
45
+ end
46
+ end
40
47
  end
41
48
 
42
49
  module InstanceMethods
@@ -65,20 +72,55 @@ module Tr8nClientSdk
65
72
  nil
66
73
  end
67
74
 
75
+ def tr8n_cookie_name
76
+ "tr8n_#{tr8n_application.key}"
77
+ end
78
+
79
+ def tr8n_cookie
80
+ request.cookies[tr8n_cookie_name]
81
+ end
82
+
83
+ def tr8n_cookie_params
84
+ @tr8n_cookie_params ||= begin
85
+ if tr8n_cookie
86
+ cookie_params = Tr8n::Utils.decode_and_verify_params(tr8n_cookie, tr8n_application.secret)
87
+ Tr8n.logger.info(cookie_params.inspect)
88
+ cookie_params
89
+ else
90
+ {}
91
+ end
92
+ end
93
+ end
94
+
95
+ def tr8n_translator_locale
96
+ tr8n_cookie_params["locale"]
97
+ end
98
+
68
99
  def tr8n_init_current_locale
69
100
  self.send(Tr8n.config.current_locale_method) if Tr8n.config.current_locale_method
70
101
  rescue
102
+ # fallback onto the cookie params from the service
103
+ return tr8n_translator_locale if tr8n_translator_locale
104
+
71
105
  # fallback to the default session based locale implementation
72
106
  # choose the first language from the accepted languages header
73
107
  session[:locale] = tr8n_user_preferred_locale unless session[:locale]
74
108
  session[:locale] = params[:locale] if params[:locale]
75
- session[:locale]
109
+ session[:locale] || Tr8n.config.default_locale
76
110
  end
77
111
 
78
112
  def tr8n_init_current_user
79
113
  self.send(Tr8n.config.current_user_method) if Tr8n.config.current_user_method
80
114
  rescue
81
- Tr8n.config.default_locale
115
+ nil
116
+ end
117
+
118
+ def tr8n_init_current_translator
119
+ if tr8n_cookie_params["translator"]
120
+ Tr8n::Translator.new(tr8n_cookie_params["translator"].merge(:application => tr8n_application))
121
+ else
122
+ nil
123
+ end
82
124
  end
83
125
 
84
126
  def tr8n_init_client_sdk
@@ -88,21 +130,10 @@ module Tr8nClientSdk
88
130
 
89
131
  return unless Tr8n.session.init
90
132
 
91
- translator = nil
92
-
93
- cookie_name = "tr8n_#{tr8n_application.key}"
94
- if request.cookies[cookie_name]
95
- cookie_params = Tr8n::Utils.decode_and_verify_params(request.cookies[cookie_name], tr8n_application.secret)
96
- Tr8n.logger.info(cookie_params.inspect)
97
- locale = cookie_params["locale"]
98
- translator = Tr8n::Translator.new(cookie_params["translator"].merge(:application => tr8n_application)) unless cookie_params["translator"].nil?
99
- end
100
-
101
- locale ||= tr8n_init_current_locale || Tr8n.config.default_locale
102
-
103
133
  Tr8n.session.current_user = tr8n_init_current_user
104
- Tr8n.session.current_translator = translator
105
- Tr8n.session.current_language = tr8n_application.language(locale || tr8n_init_current_locale || Tr8n.config.default_locale)
134
+ Tr8n.session.current_language = tr8n_application.language(tr8n_init_current_locale)
135
+ Tr8n.session.current_translator = tr8n_init_current_translator
136
+
106
137
  Tr8n.session.current_source = tr8n_source
107
138
  Tr8n.session.current_component = tr8n_component
108
139
  end
@@ -22,5 +22,5 @@
22
22
  #++
23
23
 
24
24
  module Tr8nClientSdk
25
- VERSION = "3.2.2"
25
+ VERSION = "3.2.3"
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tr8n_client_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-25 00:00:00.000000000 Z
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails