tml-rails 4.4.3 → 5.0.1

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: 8315876d08b00acf7d136a653d4b4532f2354742
4
- data.tar.gz: 2a5b78a61e089c7acb94744f9ec4901e8658d67a
3
+ metadata.gz: 2088405b5d3e687320118711c6d8af1da0a6e512
4
+ data.tar.gz: 056a6c51fb7ccfb609eb8c303dd937aaffa517ef
5
5
  SHA512:
6
- metadata.gz: 4e00d95c6f7594f2bd2c46f383894b6232ec9fab1ee472f4c72215a51f0211976b518a5b779b7a8a0c86ed11b126b1886a60129f4c144709d573a4bd154448d2
7
- data.tar.gz: 5edcd87ae7c437239e971a06051ca83cdfbe34522da699978d85b3b68ec77e568c72fade41702fc543726750cecb2b15a29db576441d1a17316a8ae1e96d9724
6
+ metadata.gz: daf41e9d10c5e54cf2a78be03c65de63ea3cd844b4fa1554a6c02556ce0d4e1f383c13789c65518413a2d45441b8f029ee2afd25f618b1312e01ecef1e99b725
7
+ data.tar.gz: 1d26cb1bd823edcbcd39072ebf0f5561313e1b561786f00d16c4f9499ca73ac1866e1508032c569504a0f94ba9cf43c5346ae23ca8bd2d6b21d30f8eb4219242
@@ -65,7 +65,7 @@ class Tml::CacheAdapters::Rails < Tml::Cache
65
65
 
66
66
  def store(key, data, opts = {})
67
67
  info("Cache store: #{key}")
68
- @cache.write(versioned_key(key, opts), data)
68
+ @cache.write(versioned_key(key, opts), strip_extensions(data))
69
69
  data
70
70
  rescue Exception => ex
71
71
  warn("Failed to store data: #{ex.message}")
@@ -58,7 +58,7 @@ module TmlRails
58
58
  # Translate individual sentences
59
59
  if params[:options][:split]
60
60
  text = params[:label]
61
- sentences = Tml::Utils.split_by_sentence(text)
61
+ sentences = Tml::Utils.split_sentences(text)
62
62
  sentences.each do |sentence|
63
63
  text = text.gsub(sentence, tml_current_language.translate(sentence, params[:description], params[:tokens], params[:options]))
64
64
  end
@@ -37,8 +37,8 @@ module TmlRails
37
37
  base.send(:include, InstanceMethods)
38
38
 
39
39
  if Tml.config.auto_init
40
- base.before_filter :tml_init
41
- base.after_filter :tml_reset
40
+ base.before_filter :tml_init
41
+ base.after_filter :tml_reset
42
42
  end
43
43
 
44
44
  if defined? base.rescue_from
@@ -53,18 +53,11 @@ module TmlRails
53
53
 
54
54
  module InstanceMethods
55
55
 
56
+ # Returns all browser accepted locales
56
57
  def tml_browser_accepted_locales
57
- @tml_browser_accepted_locales ||= Tml::Utils.browser_accepted_locales(request)
58
+ @tml_browser_accepted_locales ||= Tml::Utils.browser_accepted_locales(request.env['HTTP_ACCEPT_LANGUAGE']).join(',')
58
59
  end
59
60
 
60
- def tml_user_preferred_locale
61
- tml_browser_accepted_locales.each do |locale|
62
- next unless Tml.session.application and Tml.session.application.locales.include?(locale)
63
- return locale
64
- end
65
- Tml.config.default_locale
66
- end
67
-
68
61
  # Overwrite this method in a controller to assign a custom source for all views
69
62
  def tml_source
70
63
  "/#{controller_name}/#{action_name}"
@@ -72,44 +65,90 @@ module TmlRails
72
65
  self.class.name
73
66
  end
74
67
 
75
- def tml_init
76
- return if Tml.config.disabled?
77
-
78
- @tml_started_at = Time.now
68
+ # Returns data from cookie set by the agent
69
+ def tml_cookie
70
+ @tml_cookie ||= begin
71
+ cookie = cookies[Tml::Utils.cookie_name(Tml.config.application[:key])]
72
+ if cookie.blank?
73
+ {}
74
+ else
75
+ HashWithIndifferentAccess.new(Tml::Utils.decode(cookie, Tml.config.application[:token]))
76
+ end
77
+ end
78
+ rescue Exception => ex
79
+ Tml.logger.error("Failed to parse tml cookie: #{ex.message}")
80
+ {}
81
+ end
79
82
 
80
- tml_session_params = {
81
- :source => tml_source
82
- }
83
+ # Locale is retrieved from method => params => cookie => subdomain => browser accepted locales
84
+ # Alternatively, this method can be overwritten
85
+ def tml_locale
86
+ @tml_locale ||= begin
87
+ locale = nil
88
+
89
+ unless Tml.config.current_locale_method.blank?
90
+ begin
91
+ locale = self.send(Tml.config.current_locale_method)
92
+ rescue
93
+ locale = nil
94
+ end
95
+ end
83
96
 
84
- if Tml.config.current_user_method
85
- begin
86
- tml_session_params.merge!(:user => self.send(Tml.config.current_user_method))
87
- rescue
88
- # Tml.logger.error('Current user method is specified but not provided')
97
+ if locale.nil?
98
+ if params[:locale].blank?
99
+ locale = tml_cookie[:locale]
100
+ if locale.nil?
101
+ if Tml.config.locale[:subdomain]
102
+ locale = request.subdomain
103
+ else
104
+ locale = tml_browser_accepted_locales
105
+ end
106
+ end
107
+ else
108
+ locale = tml_cookie[:locale] = params[:locale]
109
+ cookies[Tml::Utils.cookie_name(Tml.config.application[:key])] = Tml::Utils.encode(tml_cookie, Tml.config.application[:token])
110
+ end
89
111
  end
112
+
113
+ locale
90
114
  end
115
+ end
91
116
 
92
- if Tml.config.current_locale_method
93
- begin
94
- tml_session_params.merge!(:locale => self.send(Tml.config.current_locale_method))
95
- rescue
96
- # Tml.logger.error('Current locale method is specified but not provided')
117
+ def tml_viewing_user
118
+ @tml_viewing_user ||= begin
119
+ unless Tml.config.current_user_method.blank?
120
+ begin
121
+ self.send(Tml.config.current_user_method)
122
+ rescue
123
+ {}
124
+ end
97
125
  end
98
126
  end
127
+ end
99
128
 
100
- unless tml_session_params[:locale]
101
- tml_session_params.merge!(:cookies => cookies)
102
- params[:locale] = request.subdomain if Tml.config.locale[:subdomain]
103
- tml_session_params.merge!(:change_locale => true) if params[:locale]
104
- tml_session_params.merge!(:locale => params[:locale] || tml_user_preferred_locale)
129
+ def tml_translator
130
+ @tml_translator ||= begin
131
+ if tml_cookie[:translator]
132
+ Tml::Translator.new(tml_cookie[:translator])
133
+ else
134
+ nil
135
+ end
105
136
  end
137
+ end
138
+
139
+ def tml_init
140
+ return if Tml.config.disabled?
106
141
 
107
- Tml.session.init(tml_session_params)
142
+ # Tml.logger.info(tml_cookie.inspect)
108
143
 
109
- # if user sets locale manually, update the cookie for future use
110
- if tml_session_params[:change_locale]
111
- cookies[Tml.session.cookie_name] = Tml::Utils.encode(Tml.session.cookie_params)
112
- end
144
+ @tml_started_at = Time.now
145
+
146
+ Tml.session.init({
147
+ :source => tml_source,
148
+ :locale => tml_locale,
149
+ :user => tml_viewing_user,
150
+ :translator => tml_translator
151
+ })
113
152
 
114
153
  if I18n.backend.class.name == 'I18n::Backend::Tml'
115
154
  if defined? I18n.enforce_available_locales
@@ -33,6 +33,7 @@ module TmlRails
33
33
  module ActionViewExtension
34
34
  extend ActiveSupport::Concern
35
35
 
36
+ # Translates HTML block
36
37
  def trh(tokens = {}, options = {}, &block)
37
38
  return '' unless block_given?
38
39
 
@@ -42,10 +43,12 @@ module TmlRails
42
43
  tokenizer.translate(label).html_safe
43
44
  end
44
45
 
46
+ # Translates <select><option></option></select>
45
47
  def tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language)
46
48
  options_for_select(options.tro(description), selected)
47
49
  end
48
50
 
51
+ # Returns language flag
49
52
  def tml_language_flag_tag(lang = tml_current_language, opts = {})
50
53
  return '' unless tml_application.feature_enabled?(:language_flags)
51
54
  html = image_tag(lang.flag_url, :style => (opts[:style] || 'vertical-align:middle;'), :title => lang.native_name)
@@ -53,6 +56,7 @@ module TmlRails
53
56
  html.html_safe
54
57
  end
55
58
 
59
+ # Returns language name
56
60
  def tml_language_name_tag(lang = tml_current_language, opts = {})
57
61
  show_flag = opts[:flag].nil? ? true : opts[:flag]
58
62
  name_type = opts[:language].nil? ? :english : opts[:language].to_sym # :full, :native, :english, :locale, :both
@@ -81,6 +85,7 @@ module TmlRails
81
85
  html.join.html_safe
82
86
  end
83
87
 
88
+ # Returns language selector UI
84
89
  def tml_language_selector_tag(type = nil, opts = {})
85
90
  return unless Tml.config.enabled?
86
91
 
@@ -94,6 +99,7 @@ module TmlRails
94
99
  render(:partial => "/tml_rails/tags/language_selector_#{type}", :locals => {:opts => opts})
95
100
  end
96
101
 
102
+ # Returns language strip UI
97
103
  def tml_language_strip_tag(opts = {})
98
104
  opts[:flag] = opts[:flag].nil? ? false : opts[:flag]
99
105
  opts[:name] = opts[:name].nil? ? :native : opts[:name]
@@ -103,6 +109,7 @@ module TmlRails
103
109
  render(:partial => '/tml_rails/tags/language_strip', :locals => {:opts => opts})
104
110
  end
105
111
 
112
+ # Translates flashes
106
113
  def tml_flashes_tag(opts = {})
107
114
  render(:partial => '/tml_rails/tags/flashes', :locals => {:opts => opts})
108
115
  end
@@ -191,8 +198,9 @@ module TmlRails
191
198
 
192
199
  # provides the locale and direction of the language
193
200
  def tml_html_attributes_tag(lang = tml_current_language)
194
- "#{tml_lang_attribute_tag(lang)} #{tml_dir_attribute_tag(lang)}"
201
+ "xml:lang='#{lang.locale}' #{tml_lang_attribute_tag(lang)} #{tml_dir_attribute_tag(lang)}".html_safe
195
202
  end
203
+ alias_method :tml_lang_attributes_tag, :tml_html_attributes_tag
196
204
 
197
205
  # providers the direction of the language
198
206
  def tml_dir_attribute_tag(lang = tml_current_language)
@@ -206,10 +214,6 @@ module TmlRails
206
214
  "lang='#{lang.locale}'".html_safe
207
215
  end
208
216
 
209
- def tml_lang_attributes_tag(lang = tml_current_language)
210
- "#{tml_lang_attribute_tag(lang)} #{tml_dir_attribute_tag(lang)}".html_safe
211
- end
212
-
213
217
  def tml_stylesheet_link_tag(ltr, rtl, attrs = {})
214
218
  if tml_current_language.right_to_left?
215
219
  stylesheet_link_tag(rtl, attrs)
@@ -30,5 +30,5 @@
30
30
  #++
31
31
 
32
32
  module TmlRails
33
- VERSION = '4.4.3'
33
+ VERSION = '5.0.1'
34
34
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tml-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.3
4
+ version: 5.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>'
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>'
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: tml
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '4.4'
33
+ version: '5.0'
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: '4.4'
40
+ version: '5.0'
41
41
  description: Translation Markup Language from Translation Exchange.
42
42
  email:
43
43
  - michael@translationexchange.com
@@ -82,17 +82,17 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - '>='
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.1
95
+ rubygems_version: 2.4.5
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Tml Client SDK for Ruby on Rails