tr8n_client_sdk 4.0.8 → 4.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/app/views/tr8n_client_sdk/tags/_language_selector.html.erb +53 -5
- data/lib/tr8n/cache_adapters/rails.rb +100 -0
- data/lib/tr8n_client_sdk.rb +1 -0
- data/lib/tr8n_client_sdk/extensions/action_controller_extension.rb +47 -16
- data/lib/tr8n_client_sdk/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5f3b0af6635122b50c5d1f5efc4f9e6f344b41f
|
4
|
+
data.tar.gz: 9516613ddc5a415a0a5c73e0cdb6794a441a6bfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb5c52a2ff2269aade2e04c796bca7b1ff54273c199f10a2bebd33ff4f170caadd38bc5932dbd0f199c816438a55e789bb2d9c2a6abfd64bbfa563b16e1fd122
|
7
|
+
data.tar.gz: 1e7728afbc198909f8326b550a31c9fd2cd1fd8c9872f5ea59c0928c54325beaa09e415d6a53b6f44e06908b94cffd1ffb975e0e8ae39db43d8640991d348733
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Tr8n Client SDK for Ruby on Rails
|
|
10
10
|
[![Dependency Status](https://www.versioneye.com/user/projects/53cda6a5225426f1e8000165/badge.svg?style=flat)](https://www.versioneye.com/user/projects/53cda6a5225426f1e8000165)
|
11
11
|
[![Project status](http://stillmaintained.com/tr8n/tr8n_ruby_core.png)](http://stillmaintained.com/tr8n/tr8n_ruby_core.png)
|
12
12
|
|
13
|
-
This Client SDK provides extensions necessary for translating Rails
|
13
|
+
This Client SDK provides extensions necessary for translating Rails 4.0.X based application using the TranslationExchange.com service.
|
14
14
|
|
15
15
|
|
16
16
|
Tr8n Client SDK Sample
|
@@ -57,7 +57,7 @@ To integrate Tr8n SDK into your own app, all you need to do is:
|
|
57
57
|
Add the following gem to your Gemfile:
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
gem 'tr8n_client_sdk'
|
60
|
+
gem 'tr8n_client_sdk', '~> 4.0'
|
61
61
|
```
|
62
62
|
|
63
63
|
Install the gems:
|
@@ -1,5 +1,53 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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 dir='ltr' 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
|
+
|
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 %>
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
|
4
|
+
#
|
5
|
+
# _______ _ _ _ ______ _
|
6
|
+
# |__ __| | | | | (_) | ____| | |
|
7
|
+
# | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
|
8
|
+
# | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
|
9
|
+
# | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
|
10
|
+
# |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
|
11
|
+
# __/ |
|
12
|
+
# |___/
|
13
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
14
|
+
# a copy of this software and associated documentation files (the
|
15
|
+
# "Software"), to deal in the Software without restriction, including
|
16
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
17
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
19
|
+
# the following conditions:
|
20
|
+
#
|
21
|
+
# The above copyright notice and this permission notice shall be
|
22
|
+
# included in all copies or substantial portions of the Software.
|
23
|
+
#
|
24
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
25
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
27
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
28
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
29
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
30
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
+
#++
|
32
|
+
|
33
|
+
require 'dalli' if defined?(Dalli)
|
34
|
+
|
35
|
+
class Tr8n::CacheAdapters::Rails < Tr8n::Cache
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
Tr8n.logger.info("Initializing Rails cache...")
|
39
|
+
@cache = Rails.cache
|
40
|
+
end
|
41
|
+
|
42
|
+
def cache_name
|
43
|
+
@cache.class.name
|
44
|
+
end
|
45
|
+
|
46
|
+
def read_only?
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
def fetch(key, opts = {})
|
51
|
+
miss = false
|
52
|
+
data = @cache.fetch(versioned_key(key, opts)) do
|
53
|
+
info("Cache miss: #{key}")
|
54
|
+
miss = true
|
55
|
+
if block_given?
|
56
|
+
yield
|
57
|
+
else
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
info("Cache hit: #{key}") unless miss
|
62
|
+
data
|
63
|
+
rescue Exception => ex
|
64
|
+
warn("Failed to retrieve data: #{ex.message}")
|
65
|
+
return nil unless block_given?
|
66
|
+
yield
|
67
|
+
end
|
68
|
+
|
69
|
+
def store(key, data, opts = {})
|
70
|
+
info("Cache store: #{key}")
|
71
|
+
@cache.write(versioned_key(key, opts), data)
|
72
|
+
data
|
73
|
+
rescue Exception => ex
|
74
|
+
warn("Failed to store data: #{ex.message}")
|
75
|
+
key
|
76
|
+
end
|
77
|
+
|
78
|
+
def delete(key, opts = {})
|
79
|
+
info("Cache delete: #{key}")
|
80
|
+
@cache.delete(versioned_key(key, opts))
|
81
|
+
key
|
82
|
+
rescue Exception => ex
|
83
|
+
warn("Failed to delete data: #{ex.message}")
|
84
|
+
key
|
85
|
+
end
|
86
|
+
|
87
|
+
def exist?(key, opts = {})
|
88
|
+
data = @cache.fetch(versioned_key(key, opts))
|
89
|
+
not data.nil?
|
90
|
+
rescue Exception => ex
|
91
|
+
warn("Failed to check if key exists: #{ex.message}")
|
92
|
+
false
|
93
|
+
end
|
94
|
+
|
95
|
+
def clear(opts = {})
|
96
|
+
info("Cache clear")
|
97
|
+
rescue Exception => ex
|
98
|
+
warn("Failed to clear cache: #{ex.message}")
|
99
|
+
end
|
100
|
+
end
|
data/lib/tr8n_client_sdk.rb
CHANGED
@@ -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
|
-
|
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
|
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.
|
105
|
-
Tr8n.session.
|
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
|
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: 4.0.
|
4
|
+
version: 4.0.10
|
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-
|
11
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- app/views/tr8n_client_sdk/tags/_scripts.html.erb
|
55
55
|
- config/routes.rb
|
56
56
|
- lib/tasks/tr8n_client_sdk.rake
|
57
|
+
- lib/tr8n/cache_adapters/rails.rb
|
57
58
|
- lib/tr8n_client_sdk.rb
|
58
59
|
- lib/tr8n_client_sdk/engine.rb
|
59
60
|
- lib/tr8n_client_sdk/extensions/action_common_methods.rb
|