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
data/lib/tml_rails.rb
ADDED
@@ -0,0 +1,41 @@
|
|
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
|
+
require 'tml'
|
33
|
+
require 'tml_rails/core/string'
|
34
|
+
require 'tml_rails/railtie'
|
35
|
+
require 'tml_rails/engine'
|
36
|
+
require 'tml/cache_adapters/rails'
|
37
|
+
require 'i18n/backend/tml'
|
38
|
+
|
39
|
+
module TmlClientSdk
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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
|
+
class String
|
33
|
+
|
34
|
+
def tml_translated
|
35
|
+
return self if frozen?
|
36
|
+
@tml_translated = true
|
37
|
+
self.html_safe
|
38
|
+
end
|
39
|
+
|
40
|
+
def tml_translated?
|
41
|
+
@tml_translated
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
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
|
+
module TmlRails
|
33
|
+
class Engine < ::Rails::Engine
|
34
|
+
isolate_namespace TmlRails
|
35
|
+
config.generators do |g|
|
36
|
+
g.test_framework :rspec, :view_specs => false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,131 @@
|
|
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
|
+
module TmlClientSdk
|
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
|
+
############################################################
|
43
|
+
def tr(label, description = '', tokens = {}, options = {})
|
44
|
+
params = Tml::Utils.normalize_tr_params(label, description, tokens, options)
|
45
|
+
return params[:label].html_safe if params[:label].tml_translated?
|
46
|
+
|
47
|
+
params[:options][:caller] = caller
|
48
|
+
|
49
|
+
if request
|
50
|
+
params[:options][:url] = request.url
|
51
|
+
params[:options][:host] = request.env['HTTP_HOST']
|
52
|
+
end
|
53
|
+
|
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_by_sentence(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
|
74
|
+
end
|
75
|
+
|
76
|
+
# for translating labels
|
77
|
+
def trl(label, description = '', tokens = {}, options = {})
|
78
|
+
params = Tml::Utils.normalize_tr_params(label, description, tokens, options)
|
79
|
+
params[:options][:skip_decorations] = true
|
80
|
+
tr(params)
|
81
|
+
end
|
82
|
+
|
83
|
+
# flash notice
|
84
|
+
def trfn(label, desc = '', tokens = {}, options = {})
|
85
|
+
flash[:trfn] = tr(Tml::Utils.normalize_tr_params(label, desc, tokens, options))
|
86
|
+
end
|
87
|
+
|
88
|
+
# flash error
|
89
|
+
def trfe(label, desc = '', tokens = {}, options = {})
|
90
|
+
flash[:trfe] = tr(Tml::Utils.normalize_tr_params(label, desc, tokens, options))
|
91
|
+
end
|
92
|
+
|
93
|
+
# flash warning
|
94
|
+
def trfw(label, desc = '', tokens = {}, options = {})
|
95
|
+
flash[:trfw] = tr(Tml::Utils.normalize_tr_params(label, desc, tokens, options))
|
96
|
+
end
|
97
|
+
|
98
|
+
######################################################################
|
99
|
+
## Common methods - wrappers
|
100
|
+
######################################################################
|
101
|
+
|
102
|
+
def tml_session
|
103
|
+
Tml.session
|
104
|
+
end
|
105
|
+
|
106
|
+
def tml_application
|
107
|
+
tml_session.application
|
108
|
+
end
|
109
|
+
|
110
|
+
def tml_current_user
|
111
|
+
tml_session.current_user
|
112
|
+
end
|
113
|
+
|
114
|
+
def tml_current_translator
|
115
|
+
tml_session.current_translator
|
116
|
+
end
|
117
|
+
|
118
|
+
def tml_current_locale
|
119
|
+
tml_session.current_language.locale
|
120
|
+
end
|
121
|
+
|
122
|
+
def tml_current_language
|
123
|
+
tml_session.current_language
|
124
|
+
end
|
125
|
+
|
126
|
+
def tml_language_dir
|
127
|
+
tml_current_language.dir
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,137 @@
|
|
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
|
+
module TmlClientSdk
|
33
|
+
module ActionControllerExtension
|
34
|
+
|
35
|
+
def self.included(base)
|
36
|
+
base.send(:include, TmlClientSdk::ActionCommonMethods)
|
37
|
+
base.send(:include, InstanceMethods)
|
38
|
+
base.before_filter :tml_init_client_sdk
|
39
|
+
base.after_filter :tml_reset_client_sdk
|
40
|
+
if defined? base.rescue_from
|
41
|
+
base.rescue_from 'Tml::Exception' do |e|
|
42
|
+
Tml.logger.error(e)
|
43
|
+
Tml.logger.error(e.backtrace)
|
44
|
+
Tml.session.reset
|
45
|
+
raise e
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module InstanceMethods
|
51
|
+
|
52
|
+
def tml_browser_accepted_locales
|
53
|
+
@tml_browser_accepted_locales ||= Tml::Utils.browser_accepted_locales(request)
|
54
|
+
end
|
55
|
+
|
56
|
+
def tml_user_preferred_locale
|
57
|
+
tml_browser_accepted_locales.each do |locale|
|
58
|
+
next unless Tml.session.application.locales.include?(locale)
|
59
|
+
return locale
|
60
|
+
end
|
61
|
+
Tml.config.default_locale
|
62
|
+
end
|
63
|
+
|
64
|
+
# Overwrite this method in a controller to assign a custom source for all views
|
65
|
+
def tml_source
|
66
|
+
"/#{controller_name}/#{action_name}"
|
67
|
+
rescue
|
68
|
+
self.class.name
|
69
|
+
end
|
70
|
+
|
71
|
+
# Overwrite this method in a controller to assign a custom component for all views
|
72
|
+
def tml_component
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def tml_toggle_tools(flag)
|
77
|
+
session[:tml_tools_disabled] = !flag
|
78
|
+
end
|
79
|
+
|
80
|
+
def tml_tools_enabled?
|
81
|
+
not session[:tml_tools_disabled]
|
82
|
+
end
|
83
|
+
|
84
|
+
def tml_locale
|
85
|
+
self.send(Tml.config.current_locale_method) if Tml.config.current_locale_method
|
86
|
+
rescue
|
87
|
+
session[:locale] = tml_user_preferred_locale unless session[:locale]
|
88
|
+
session[:locale] = params[:locale] if params[:locale]
|
89
|
+
session[:locale] || Tml.config.default_locale
|
90
|
+
end
|
91
|
+
|
92
|
+
def tml_init_client_sdk
|
93
|
+
return if Tml.config.disabled?
|
94
|
+
|
95
|
+
@tml_started_at = Time.now
|
96
|
+
|
97
|
+
if params[:tml]
|
98
|
+
tml_toggle_tools(params[:tml] == 'on')
|
99
|
+
end
|
100
|
+
|
101
|
+
tml_session_params = {
|
102
|
+
:tools_enabled => tml_tools_enabled?,
|
103
|
+
:source => tml_source,
|
104
|
+
:component => tml_component
|
105
|
+
}
|
106
|
+
|
107
|
+
if Tml.config.current_user_method
|
108
|
+
begin
|
109
|
+
tml_session_params.merge!(:user => self.send(Tml.config.current_user_method))
|
110
|
+
rescue
|
111
|
+
# Tml.logger.error('Current user method is specified but not provided')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
if tml_tools_enabled? # gets translator and locale from the cookie
|
116
|
+
tml_session_params.merge!(:cookies => request.cookies)
|
117
|
+
else # uses default locale
|
118
|
+
tml_session_params.merge!(:locale => tml_locale)
|
119
|
+
end
|
120
|
+
|
121
|
+
Tml.session.init(tml_session_params)
|
122
|
+
|
123
|
+
I18n.enforce_available_locales = false
|
124
|
+
I18n.locale = Tml.session.current_language.locale
|
125
|
+
end
|
126
|
+
|
127
|
+
def tml_reset_client_sdk
|
128
|
+
return if Tml.config.disabled?
|
129
|
+
@tml_finished_at = Time.now
|
130
|
+
tml_application.submit_missing_keys
|
131
|
+
Tml.session.reset
|
132
|
+
Tml.logger.info("Request took #{@tml_finished_at - @tml_started_at} mls") if @tml_started_at
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,188 @@
|
|
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
|
+
module TmlClientSdk
|
33
|
+
module ActionViewExtension
|
34
|
+
extend ActiveSupport::Concern
|
35
|
+
|
36
|
+
def trh(tokens = {}, options = {}, &block)
|
37
|
+
return '' unless block_given?
|
38
|
+
|
39
|
+
label = capture(&block)
|
40
|
+
|
41
|
+
tokenizer = Tml::Tokenizers::Dom.new(tokens, options)
|
42
|
+
tokenizer.translate(label).html_safe
|
43
|
+
end
|
44
|
+
|
45
|
+
def tml_options_for_select(options, selected = nil, description = nil, lang = Tml.session.current_language)
|
46
|
+
options_for_select(options.tro(description), selected)
|
47
|
+
end
|
48
|
+
|
49
|
+
def tml_phrases_link_tag(search = "", phrase_type = :without, phrase_status = :any)
|
50
|
+
return unless Tml.config.enabled?
|
51
|
+
return if tml_current_language.default?
|
52
|
+
return unless tml_current_translator.inline?
|
53
|
+
|
54
|
+
link_to(image_tag(tml_application.url_for("/assets/tml/translate_icn.gif"), :style => "vertical-align:middle; border: 0px;", :title => search),
|
55
|
+
tml_application.url_for("/tml/app/phrases/index?search=#{search}")).html_safe
|
56
|
+
end
|
57
|
+
|
58
|
+
def tml_language_flag_tag(lang = tml_current_language, opts = {})
|
59
|
+
return "" unless tml_application.feature_enabled?(:language_flags)
|
60
|
+
html = image_tag(lang.flag_url, :style => "vertical-align:middle;", :title => lang.native_name)
|
61
|
+
html << " ".html_safe
|
62
|
+
html.html_safe
|
63
|
+
end
|
64
|
+
|
65
|
+
def tml_language_name_tag(lang = tml_current_language, opts = {})
|
66
|
+
show_flag = opts[:flag].nil? ? true : opts[:flag]
|
67
|
+
name_type = opts[:name].nil? ? :full : opts[:name] # :full, :native, :english, :locale
|
68
|
+
linked = opts[:linked].nil? ? true : opts[:linked]
|
69
|
+
|
70
|
+
html = "<span style='white-space: nowrap'>"
|
71
|
+
html << tml_language_flag_tag(lang, opts) if show_flag
|
72
|
+
html << "<span dir='ltr'>"
|
73
|
+
|
74
|
+
name = case name_type
|
75
|
+
when :native then lang.native_name
|
76
|
+
when :english then lang.english_name
|
77
|
+
when :locale then lang.locale
|
78
|
+
else lang.full_name
|
79
|
+
end
|
80
|
+
|
81
|
+
html << name
|
82
|
+
html << "</span></span>"
|
83
|
+
html.html_safe
|
84
|
+
end
|
85
|
+
|
86
|
+
def tml_language_selector_tag(type = nil, opts = {})
|
87
|
+
return unless Tml.config.enabled?
|
88
|
+
render(:partial => '/tml_rails/tags/language_selector', :locals => {:type => type, :opts => opts})
|
89
|
+
end
|
90
|
+
|
91
|
+
def tml_language_strip_tag(opts = {})
|
92
|
+
opts[:flag] = opts[:flag].nil? ? false : opts[:flag]
|
93
|
+
opts[:name] = opts[:name].nil? ? :native : opts[:name]
|
94
|
+
opts[:linked] = opts[:linked].nil? ? true : opts[:linked]
|
95
|
+
opts[:javascript] = opts[:javascript].nil? ? false : opts[:javascript]
|
96
|
+
|
97
|
+
render(:partial => '/tml_rails/tags/language_strip', :locals => {:opts => opts})
|
98
|
+
end
|
99
|
+
|
100
|
+
def tml_flashes_tag(opts = {})
|
101
|
+
render(:partial => '/tml_rails/tags/flashes', :locals => {:opts => opts})
|
102
|
+
end
|
103
|
+
|
104
|
+
def tml_scripts_tag(opts = {})
|
105
|
+
render(:partial => '/tml_rails/tags/scripts', :locals => {:opts => opts})
|
106
|
+
end
|
107
|
+
|
108
|
+
def tml_select_month(date, options = {}, html_options = {})
|
109
|
+
month_names = options[:use_short_month] ? Tml.config.default_abbr_month_names : Tml.config.default_month_names
|
110
|
+
select_month(date, options.merge(
|
111
|
+
:use_month_names => month_names.collect{|month_name| tml_current_language.translate(month_name, options[:description] || "Month name")}
|
112
|
+
), html_options)
|
113
|
+
end
|
114
|
+
|
115
|
+
def tml_with_options_tag(opts, &block)
|
116
|
+
if Tml.config.disabled?
|
117
|
+
return capture(&block) if block_given?
|
118
|
+
return ""
|
119
|
+
end
|
120
|
+
|
121
|
+
Tml.session.push_block_options(opts)
|
122
|
+
|
123
|
+
if block_given?
|
124
|
+
ret = capture(&block)
|
125
|
+
end
|
126
|
+
|
127
|
+
Tml.session.pop_block_options
|
128
|
+
ret
|
129
|
+
end
|
130
|
+
alias_method :tml_block, :tml_with_options_tag
|
131
|
+
|
132
|
+
def tml_when_string_tag(time, opts = {})
|
133
|
+
elapsed_seconds = Time.now - time
|
134
|
+
if elapsed_seconds < 0
|
135
|
+
tr('In the future, Marty!', 'Time reference')
|
136
|
+
elsif elapsed_seconds < 2.minutes
|
137
|
+
tr('a moment ago', 'Time reference')
|
138
|
+
elsif elapsed_seconds < 55.minutes
|
139
|
+
elapsed_minutes = (elapsed_seconds / 1.minute).to_i
|
140
|
+
tr('{minutes || minute} ago', 'Time reference', :minutes => elapsed_minutes)
|
141
|
+
elsif elapsed_seconds < 1.75.hours
|
142
|
+
tr('about an hour ago', 'Time reference')
|
143
|
+
elsif elapsed_seconds < 12.hours
|
144
|
+
elapsed_hours = (elapsed_seconds / 1.hour).to_i
|
145
|
+
tr('{hours || hour} ago', 'Time reference', :hours => elapsed_hours)
|
146
|
+
elsif time.today_in_time_zone?
|
147
|
+
display_time(time, :time_am_pm)
|
148
|
+
elsif time.yesterday_in_time_zone?
|
149
|
+
tr("Yesterday at {time}", 'Time reference', :time => time.tr(:time_am_pm).gsub('/ ', '/').sub(/^[0:]*/,""))
|
150
|
+
elsif elapsed_seconds < 5.days
|
151
|
+
time.tr(:day_time).gsub('/ ', '/').sub(/^[0:]*/,"")
|
152
|
+
elsif time.same_year_in_time_zone?
|
153
|
+
time.tr(:monthname_abbr_time).gsub('/ ', '/').sub(/^[0:]*/, '')
|
154
|
+
else
|
155
|
+
time.tr(:monthname_abbr_year_time).gsub('/ ', '/').sub(/^[0:]*/, '')
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def tml_url_tag(path)
|
160
|
+
tml_application.url_for(path)
|
161
|
+
end
|
162
|
+
|
163
|
+
######################################################################
|
164
|
+
## Language Direction Support
|
165
|
+
######################################################################
|
166
|
+
|
167
|
+
def tml_style_attribute_tag(attr_name = 'float', default = 'right', lang = tml_current_language)
|
168
|
+
return "#{attr_name}:#{default}".html_safe if Tml.config.disabled?
|
169
|
+
"#{attr_name}:#{lang.align(default)}".html_safe
|
170
|
+
end
|
171
|
+
|
172
|
+
def tml_style_directional_attribute_tag(attr_name = 'padding', default = 'right', value = '5px', lang = tml_current_language)
|
173
|
+
return "#{attr_name}-#{default}:#{value}".html_safe if Tml.config.disabled?
|
174
|
+
"#{attr_name}-#{lang.align(default)}:#{value}".html_safe
|
175
|
+
end
|
176
|
+
|
177
|
+
def tml_dir_attribute_tag(lang = tml_current_language)
|
178
|
+
return "dir='ltr'" if Tml.config.disabled?
|
179
|
+
"dir='#{lang.dir}'".html_safe
|
180
|
+
end
|
181
|
+
|
182
|
+
def tml_lang_attribute_tag(lang = tml_current_language)
|
183
|
+
return "lang='en-US'" if Tml.config.disabled?
|
184
|
+
"lang='#{lang.locale}'".html_safe
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
end
|