tml-rails 5.4.6 → 5.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf3a772c2d881f6862aa2fe6dae70c021d65ed66
4
- data.tar.gz: 8dc31a645a9f092905cfb4b0ca8d39db18eccc78
3
+ metadata.gz: a0301a87bd78a66f93bad4a3e824fbb00d5a513b
4
+ data.tar.gz: 04845d65cebb7ec8edb394e9ba904408d8f896b1
5
5
  SHA512:
6
- metadata.gz: d6c56a78acfd2c5144cf3085252ca68182981ad7ea3794ffc4661e8346a6ddd114d633aea3aa0b25f6805d16423572fa67a841212c5b5fe88e30e917ba191608
7
- data.tar.gz: 0be0458b0969a2381e1941f00d4a951a9f3653b1c0326f991760b4bdfb97c1db9586c7019a42a4ce03986d5d11942b7ae37161af19d97136eeadbf9ce2f2a0ad
6
+ metadata.gz: 6cef4b611ae3f1d13d971e51d54388ca55f1abaf715134d51336ef4ff433f56fdc3e1c53924fd8c28d68bc1fbb1c8a26fbad434487ee380a63fa4889ead73e67
7
+ data.tar.gz: 9dc77cee4e5cb87c3fa637e4bbb3849c52d102041544d07538042dd3df97fd2bec2a7a817c20a81ddb4d94662dfae89c4f012126c388fe5da396575834ba8797
@@ -31,15 +31,7 @@
31
31
 
32
32
  module TmlRails
33
33
  module ActionCommonMethods
34
- ############################################################
35
- # There are three ways to call the tr method
36
- #
37
- # tr(label, desc = '', tokens = {}, options = {})
38
- # or
39
- # tr(label, tokens = {}, options = {})
40
- # or
41
- # tr(:label => label, :description => '', :tokens => {}, :options => {})
42
- ############################################################
34
+
43
35
  def tr(label, description = '', tokens = {}, options = {})
44
36
  params = Tml::Utils.normalize_tr_params(label, description, tokens, options)
45
37
  return params[:label].html_safe if params[:label].tml_translated?
@@ -51,26 +43,7 @@ module TmlRails
51
43
  params[:options][:host] = request.env['HTTP_HOST']
52
44
  end
53
45
 
54
- if Tml.config.disabled?
55
- return Tml.config.default_language.translate(params[:label], params[:tokens], params[:options]).tml_translated.html_safe
56
- end
57
-
58
- # Translate individual sentences
59
- if params[:options][:split]
60
- text = params[:label]
61
- sentences = Tml::Utils.split_sentences(text)
62
- sentences.each do |sentence|
63
- text = text.gsub(sentence, tml_current_language.translate(sentence, params[:description], params[:tokens], params[:options]))
64
- end
65
- return text.tml_translated.html_safe
66
- end
67
-
68
- Tml.session.target_language.translate(params).tml_translated.html_safe
69
- rescue Tml::Exception => ex
70
- #pp ex, ex.backtrace
71
- Tml.logger.error(ex.message)
72
- #Tml.logger.error(ex.message + "\n=> " + ex.backtrace.join("\n=> "))
73
- label
46
+ Tml.translate(params)
74
47
  end
75
48
 
76
49
  # for translating labels
@@ -119,6 +92,10 @@ module TmlRails
119
92
  tml_session.current_translator
120
93
  end
121
94
 
95
+ def tml_default_locale
96
+ tml_session.application.default_locale
97
+ end
98
+
122
99
  def tml_current_locale
123
100
  tml_session.current_language.locale
124
101
  end
@@ -52,18 +52,6 @@ module TmlRails
52
52
 
53
53
  module InstanceMethods
54
54
 
55
- # Returns all browser accepted locales
56
- def tml_browser_accepted_locales
57
- @tml_browser_accepted_locales ||= Tml::Utils.browser_accepted_locales(request.env['HTTP_ACCEPT_LANGUAGE']).join(',')
58
- end
59
-
60
- # Overwrite this method in a controller to assign a custom source for all views
61
- def tml_source
62
- "/#{controller_name}/#{action_name}"
63
- rescue
64
- self.class.name
65
- end
66
-
67
55
  # Returns data from cookie set by the agent
68
56
  def tml_cookie
69
57
  @tml_cookie ||= begin
@@ -79,61 +67,57 @@ module TmlRails
79
67
  {}
80
68
  end
81
69
 
70
+ # Overwrite this method in a controller to assign a custom source for all views
71
+ def tml_source
72
+ "/#{controller_name}/#{action_name}"
73
+ rescue
74
+ "/classes/#{self.class.name}"
75
+ end
76
+
82
77
  # Locale is retrieved from method => params => cookie => subdomain => browser accepted locales
83
78
  # Alternatively, this method can be overwritten
84
79
  def tml_locale
85
- @tml_locale ||= begin
86
- locale = nil
87
-
88
- unless Tml.config.current_locale_method.blank?
89
- begin
90
- locale = self.send(Tml.config.current_locale_method)
91
- rescue
92
- locale = nil
93
- end
94
- end
80
+ # if locale has been passed by a param, it will be in the params hash
81
+ if Tml.config.locale_strategy == 'param'
82
+ return params[Tml.config.locale_param] # will be nil without ?locale=:locale
83
+ end
95
84
 
96
- if locale.nil?
97
- if params[:locale].blank?
98
- locale = tml_cookie[:locale]
99
- if locale.nil?
100
- if Tml.config.locale[:subdomain]
101
- locale = request.subdomains.first
102
- elsif Tml.config.locale[:tld]
103
- locale = request.host.split('.').last
104
- else
105
- locale = tml_browser_accepted_locales
106
- end
107
- end
108
- else
109
- locale = tml_cookie[:locale] = params[:locale]
110
- cookies[Tml::Utils.cookie_name(Tml.config.application[:key])] = Tml::Utils.encode(tml_cookie, Tml.config.application[:token])
111
- end
85
+ if Tml.config.locale_strategy == 'pre-path'
86
+ return params[Tml.config.locale_param] # will be nil without /:locale
87
+ end
88
+
89
+ if Tml.config.locale_strategy == 'pre-domain'
90
+ locale = request.subdomains.first
91
+ if locale.nil? or not locale.match(Tml.config.locale_expression)
92
+ locale = Tml.config.locale[:default]
112
93
  end
94
+ return locale
95
+ end
113
96
 
114
- locale
97
+ if Tml.config.locale_strategy == 'custom-domain'
98
+ host = "#{request.host}#{[80, 443].include?(request.port) ? '' : ":#{request.port}"}"
99
+ locale = Tml.config.locale[:mapping].invert[host] # will be nil if host is wrong
100
+ return locale
115
101
  end
102
+
103
+ nil
116
104
  end
117
105
 
118
106
  def tml_viewing_user
119
- @tml_viewing_user ||= begin
120
- unless Tml.config.current_user_method.blank?
121
- begin
122
- self.send(Tml.config.current_user_method)
123
- rescue
124
- {}
125
- end
107
+ unless Tml.config.current_user_method.blank?
108
+ begin
109
+ self.send(Tml.config.current_user_method)
110
+ rescue
111
+ {}
126
112
  end
127
113
  end
128
114
  end
129
115
 
130
116
  def tml_translator
131
- @tml_translator ||= begin
132
- if tml_cookie[:translator]
133
- Tml::Translator.new(tml_cookie[:translator])
134
- else
135
- nil
136
- end
117
+ if tml_cookie[:translator]
118
+ Tml::Translator.new(tml_cookie[:translator])
119
+ else
120
+ nil
137
121
  end
138
122
  end
139
123
 
@@ -148,9 +132,30 @@ module TmlRails
148
132
 
149
133
  @tml_started_at = Time.now
150
134
 
135
+ requested_locale = desired_locale = tml_locale
136
+
137
+ # if user has a custom method for providing the locale, use it
138
+ if Tml.config.current_locale_method
139
+ begin
140
+ desired_locale = self.send(Tml.config.current_locale_method)
141
+ rescue
142
+ desired_locale = requested_locale
143
+ end
144
+ end
145
+ # check if locale was previously stored in a cookie
146
+ desired_locale ||= Tml.config.locale_cookie_enabled? ? tml_cookie[:locale] : nil
147
+ # fallback onto the browser locale
148
+ desired_locale ||= Tml.config.locale_browser_enabled? ? Tml::Utils.browser_accepted_locales(
149
+ request.env['HTTP_ACCEPT_LANGUAGE']
150
+ ).join(',') : nil
151
+
152
+ # pp requested_locale: requested_locale, desired_locale: desired_locale
153
+ # pp cookie: tml_cookie
154
+
155
+ # init SDK with desired locale and get the actual locale supported in the app
151
156
  Tml.session.init(
152
157
  :source => tml_source,
153
- :locale => tml_locale,
158
+ :locale => desired_locale,
154
159
  :user => tml_viewing_user,
155
160
  :translator => tml_translator,
156
161
  :access_token => tml_access_token
@@ -159,7 +164,69 @@ module TmlRails
159
164
  if defined? I18n.enforce_available_locales
160
165
  I18n.enforce_available_locales = false
161
166
  end
162
- I18n.locale = Tml.session.current_language.locale
167
+ I18n.locale = tml_current_locale
168
+
169
+ # pp current_locale: tml_current_locale
170
+
171
+ # check if we want to store the last selected locale in the cookie
172
+ if requested_locale == tml_current_locale and Tml.config.locale_cookie_enabled?
173
+ tml_cookie[:locale] = tml_current_locale
174
+ cookies[Tml::Utils.cookie_name(Tml.config.application[:key])] = {
175
+ :value => Tml::Utils.encode(tml_cookie),
176
+ :expires => 1.year.from_now,
177
+ :domain => Tml.config.locale[:domain]
178
+ }
179
+ end
180
+
181
+ # pp cookie: tml_cookie
182
+ # pp redirect: Tml.config.locale_redirect_enabled?
183
+
184
+ if Tml.config.locale_redirect_enabled?
185
+ if Tml.config.locale[:skip_default] and tml_current_locale == tml_default_locale
186
+ # first lets see if we are in default locale and user doesn't want to show locale in url
187
+ if Tml.config.locale_strategy == 'pre-path' and not requested_locale.nil?
188
+ return redirect_to(Tml.config.locale_param => nil)
189
+ end
190
+
191
+ if Tml.config.locale_strategy == 'pre-domain' and request.subdomains.any?
192
+ fragments = request.host.split('.')
193
+ if fragments.first.match(Tml.config.locale_expression)
194
+ if Tml.config.locale[:default_subdomain]
195
+ fragments[0] = Tml.config.locale[:default_subdomain]
196
+ else
197
+ fragments.shift
198
+ end
199
+ end
200
+ return redirect_to(host: fragments.join('.'))
201
+ end
202
+
203
+ if Tml.config.locale_strategy == 'custom-domain'
204
+ host = Tml.config.locale[:mapping][tml_default_locale]
205
+ return redirect_to(host: host)
206
+ end
207
+ elsif requested_locale != tml_current_locale
208
+ # otherwise, the locale is not the same as what was requested, deal with it
209
+ if Tml.config.locale_strategy == 'pre-path'
210
+ return redirect_to(Tml.config.locale_param => tml_current_locale)
211
+ end
212
+
213
+ if Tml.config.locale_strategy == 'pre-domain'
214
+ fragments = request.host.split('.')
215
+ if request.subdomains.any?
216
+ fragments[0] = tml_current_locale
217
+ else
218
+ fragments.unshift(tml_current_locale)
219
+ end
220
+ return redirect_to(host: fragments.join('.'))
221
+ end
222
+
223
+ if Tml.config.locale_strategy == 'custom-domain'
224
+ host = Tml.config.locale[:mapping][tml_current_locale]
225
+ host ||= Tml.config.locale[:mapping][tml_default_locale]
226
+ return redirect_to(host: host)
227
+ end
228
+ end
229
+ end
163
230
 
164
231
  if tml_current_translator and tml_current_translator.inline?
165
232
  I18n.reload!
@@ -128,10 +128,12 @@ module TmlRails
128
128
  agent_host += "?ts=#{t.to_i}"
129
129
  end
130
130
 
131
+ agent_config[:domain] = Tml.config.domain if Tml.config.domain
131
132
  agent_config[:locale] = tml_current_locale
132
133
  agent_config[:source] = tml_current_source
133
134
  agent_config[:css] = tml_application.css
134
135
  agent_config[:sdk] = Tml.respond_to?(:full_version) ? Tml.full_version : Tml::VERSION
136
+ agent_config[:locale_strategy] = Tml.config.agent_locale_strategy
135
137
  agent_config[:languages] = []
136
138
 
137
139
  tml_application.languages.each do |lang|
@@ -30,5 +30,5 @@
30
30
  #++
31
31
 
32
32
  module TmlRails
33
- VERSION = '5.4.6'
33
+ VERSION = '5.5.0'
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tml-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.6
4
+ version: 5.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-16 00:00:00.000000000 Z
11
+ date: 2016-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.5'
33
+ version: '5.6'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.5'
40
+ version: '5.6'
41
41
  description: Translation Markup Language from Translation Exchange.
42
42
  email:
43
43
  - support@translationexchange.com
@@ -85,4 +85,3 @@ signing_key:
85
85
  specification_version: 4
86
86
  summary: Tml Client SDK for Ruby on Rails
87
87
  test_files: []
88
- has_rdoc: