tml 4.3.1
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 +7 -0
- data/LICENSE +22 -0
- data/README.md +243 -0
- data/Rakefile +9 -0
- data/lib/tml.rb +56 -0
- data/lib/tml/api/client.rb +206 -0
- data/lib/tml/api/post_office.rb +71 -0
- data/lib/tml/application.rb +254 -0
- data/lib/tml/base.rb +116 -0
- data/lib/tml/cache.rb +143 -0
- data/lib/tml/cache_adapters/file.rb +89 -0
- data/lib/tml/cache_adapters/memcache.rb +104 -0
- data/lib/tml/cache_adapters/memory.rb +85 -0
- data/lib/tml/cache_adapters/redis.rb +108 -0
- data/lib/tml/config.rb +410 -0
- data/lib/tml/decorators/base.rb +52 -0
- data/lib/tml/decorators/default.rb +43 -0
- data/lib/tml/decorators/html.rb +102 -0
- data/lib/tml/exception.rb +35 -0
- data/lib/tml/ext/array.rb +86 -0
- data/lib/tml/ext/date.rb +99 -0
- data/lib/tml/ext/fixnum.rb +47 -0
- data/lib/tml/ext/hash.rb +99 -0
- data/lib/tml/ext/string.rb +56 -0
- data/lib/tml/ext/time.rb +89 -0
- data/lib/tml/generators/cache/base.rb +117 -0
- data/lib/tml/generators/cache/file.rb +159 -0
- data/lib/tml/language.rb +175 -0
- data/lib/tml/language_case.rb +105 -0
- data/lib/tml/language_case_rule.rb +76 -0
- data/lib/tml/language_context.rb +117 -0
- data/lib/tml/language_context_rule.rb +56 -0
- data/lib/tml/languages/en.json +1363 -0
- data/lib/tml/logger.rb +109 -0
- data/lib/tml/rules_engine/evaluator.rb +162 -0
- data/lib/tml/rules_engine/parser.rb +65 -0
- data/lib/tml/session.rb +199 -0
- data/lib/tml/source.rb +106 -0
- data/lib/tml/tokenizers/data.rb +96 -0
- data/lib/tml/tokenizers/decoration.rb +204 -0
- data/lib/tml/tokenizers/dom.rb +346 -0
- data/lib/tml/tokens/data.rb +403 -0
- data/lib/tml/tokens/method.rb +61 -0
- data/lib/tml/tokens/transform.rb +223 -0
- data/lib/tml/translation.rb +67 -0
- data/lib/tml/translation_key.rb +178 -0
- data/lib/tml/translator.rb +47 -0
- data/lib/tml/utils.rb +130 -0
- data/lib/tml/version.rb +34 -0
- metadata +121 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2015 Translation Exchange, Inc
|
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
|
+
class Tml::Decorators::Base < Tml::Base
|
34
|
+
|
35
|
+
def self.decorator
|
36
|
+
# can alternate based on the format in the config
|
37
|
+
Tml.config.decorator_class.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def decorate(translated_label, translation_language, target_language, translation_key, options = {})
|
41
|
+
raise Tml::Exception.new('Must be implemented by the extending class')
|
42
|
+
end
|
43
|
+
|
44
|
+
def inline_mode?
|
45
|
+
Tml.session.current_translator and Tml.session.current_translator.inline?
|
46
|
+
end
|
47
|
+
|
48
|
+
def decorate_language_case(language_case, rule, original, transformed, options = {})
|
49
|
+
raise Tml::Exception.new('Must be implemented by the extending class')
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2015 Translation Exchange, Inc
|
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
|
+
class Tml::Decorators::Default < Tml::Decorators::Base
|
34
|
+
|
35
|
+
def decorate(translated_label, translation_language, target_language, translation_key, options = {})
|
36
|
+
translated_label
|
37
|
+
end
|
38
|
+
|
39
|
+
def decorate_language_case(language_case, rule, original, transformed, options = {})
|
40
|
+
transformed
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2015 Translation Exchange, Inc
|
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
|
+
class Tml::Decorators::Html < Tml::Decorators::Base
|
34
|
+
|
35
|
+
def decorate(translated_label, translation_language, target_language, translation_key, options = {})
|
36
|
+
#Tml.logger.info("Decorating #{translated_label} of #{translation_language.locale} to #{target_language.locale}")
|
37
|
+
|
38
|
+
# skip decoration if instructed so
|
39
|
+
return translated_label if options[:skip_decorations]
|
40
|
+
|
41
|
+
# if translation key language is the same as target language - skip decorations
|
42
|
+
return translated_label if translation_key.language == target_language
|
43
|
+
return translated_label unless inline_mode?
|
44
|
+
return translated_label if translation_key.locked? and not Tml.session.current_translator.manager?
|
45
|
+
|
46
|
+
classes = %w(tml_translatable)
|
47
|
+
|
48
|
+
if translation_key.locked?
|
49
|
+
# must be a manager and enabling locking feature
|
50
|
+
# return translated_label unless Tml.session.current_translator.feature_enabled?(:show_locked_keys)
|
51
|
+
classes << 'tml_locked'
|
52
|
+
elsif translation_language == translation_key.language
|
53
|
+
classes << 'tml_not_translated'
|
54
|
+
elsif translation_language == target_language
|
55
|
+
classes << 'tml_translated'
|
56
|
+
else
|
57
|
+
classes << 'tml_fallback'
|
58
|
+
end
|
59
|
+
|
60
|
+
element = 'span'
|
61
|
+
element = 'div' if options[:use_div]
|
62
|
+
|
63
|
+
html = "<#{element} class='#{classes.join(' ')}' data-translation_key='#{translation_key.key}' data-target_locale='#{target_language.locale}'>"
|
64
|
+
html << translated_label
|
65
|
+
html << "</#{element}>"
|
66
|
+
html
|
67
|
+
end
|
68
|
+
|
69
|
+
def decorate_language_case(language_case, rule, original, transformed, options = {})
|
70
|
+
return transformed if options[:skip_decorations] or language_case.language.default?
|
71
|
+
return transformed unless inline_mode?
|
72
|
+
|
73
|
+
data = {
|
74
|
+
'keyword' => language_case.keyword,
|
75
|
+
'language_name' => language_case.language.english_name,
|
76
|
+
'latin_name' => language_case.latin_name,
|
77
|
+
'native_name' => language_case.native_name,
|
78
|
+
'conditions' => (rule ? rule.conditions : ''),
|
79
|
+
'operations' => (rule ? rule.operations : ''),
|
80
|
+
'original' => original,
|
81
|
+
'transformed' => transformed
|
82
|
+
}
|
83
|
+
|
84
|
+
attrs = []
|
85
|
+
{
|
86
|
+
'class' => 'tml_language_case',
|
87
|
+
'data-locale' => language_case.language.locale,
|
88
|
+
'data-rule' => CGI::escape(Base64.encode64(data.to_json).gsub("\n", ''))
|
89
|
+
}.each do |key, value|
|
90
|
+
attrs << "#{key}=\"#{value.to_s.gsub('"', "\"")}\""
|
91
|
+
end
|
92
|
+
|
93
|
+
element = 'span'
|
94
|
+
element = 'div' if options[:use_div]
|
95
|
+
|
96
|
+
html = "<#{element} #{attrs.join(' ')}>"
|
97
|
+
html << transformed
|
98
|
+
html << "</#{element}>"
|
99
|
+
html
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2015 Translation Exchange, Inc
|
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
|
+
class Tml::Exception < StandardError
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,86 @@
|
|
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 Array
|
33
|
+
|
34
|
+
# translates an array of options for a select tag
|
35
|
+
def tro(description = '', options = {})
|
36
|
+
return [] if empty?
|
37
|
+
|
38
|
+
options = options.merge(:skip_decorations => true)
|
39
|
+
|
40
|
+
collect do |opt|
|
41
|
+
if opt.is_a?(Array) and opt.first.is_a?(String)
|
42
|
+
[opt.first.translate(description, {}, options), opt.last]
|
43
|
+
elsif opt.is_a?(String)
|
44
|
+
[opt.translate(description, {}, options), opt]
|
45
|
+
else
|
46
|
+
opt
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# translates and joins all elements
|
52
|
+
def translated_and_join(separator = '', description = '', options = {})
|
53
|
+
self.translate(description, options).join(separator).tml_translated
|
54
|
+
end
|
55
|
+
|
56
|
+
# translate array values
|
57
|
+
def translate(description = '', options = {})
|
58
|
+
return [] if empty?
|
59
|
+
|
60
|
+
collect do |opt|
|
61
|
+
if opt.is_a?(String)
|
62
|
+
opt.translate(description, {}, options)
|
63
|
+
else
|
64
|
+
opt
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# creates a sentence with tr "and" joiner
|
70
|
+
def translate_sentence(description = '', options = {})
|
71
|
+
return '' if empty?
|
72
|
+
return first if size == 1
|
73
|
+
|
74
|
+
elements = translate(description, options)
|
75
|
+
|
76
|
+
options[:separator] ||= ', '
|
77
|
+
options[:joiner] ||= 'and'
|
78
|
+
|
79
|
+
result = elements[0..-2].join(options[:separator])
|
80
|
+
result << ' ' << options[:joiner].translate('List elements joiner', {}, options) << ' '
|
81
|
+
result << elements.last
|
82
|
+
|
83
|
+
result.tml_translated
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
data/lib/tml/ext/date.rb
ADDED
@@ -0,0 +1,99 @@
|
|
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 Date
|
33
|
+
|
34
|
+
# The translation of the date can use one of the formats:
|
35
|
+
#
|
36
|
+
# Using the Symbol to pull the default format defined in the config:
|
37
|
+
#
|
38
|
+
# date.translate(:verbose)
|
39
|
+
#
|
40
|
+
# Using a string with format with native Date elements
|
41
|
+
#
|
42
|
+
# date.translate("%m/%d/%Y")
|
43
|
+
#
|
44
|
+
# Or using a string with tokens:
|
45
|
+
#
|
46
|
+
# date.translate("{month_name} {days::ord}")
|
47
|
+
#
|
48
|
+
# The last allows you to use language cases to get something like "on March 1st".
|
49
|
+
|
50
|
+
def translate(format = :default, options = {})
|
51
|
+
language = Tml.session.target_language
|
52
|
+
|
53
|
+
label = (format.is_a?(String) ? format.clone : Tml.config.default_date_formats[format].clone)
|
54
|
+
|
55
|
+
selected_tokens = []
|
56
|
+
using_tokens = label.index('{')
|
57
|
+
|
58
|
+
if using_tokens
|
59
|
+
selected_tokens = Tml::Tokens::Data.parse(label).collect{ |token| token.name(:parens => true) }
|
60
|
+
|
61
|
+
else
|
62
|
+
symbols = label.scan(/(%\w)/).flatten.uniq
|
63
|
+
|
64
|
+
symbols.each do |symbol|
|
65
|
+
token = Tml.config.strftime_symbol_to_token(symbol)
|
66
|
+
next unless token
|
67
|
+
selected_tokens << token
|
68
|
+
label.gsub!(symbol, token)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
tokens = {}
|
73
|
+
selected_tokens.each do |token|
|
74
|
+
case token
|
75
|
+
when '{days}' then tokens[:days] = options[:with_leading_zero] ? day.with_leading_zero : day.to_s
|
76
|
+
when '{year_days}' then tokens[:year_days] = options[:with_leading_zero] ? yday.with_leading_zero : yday.to_s
|
77
|
+
when '{months}' then tokens[:months] = options[:with_leading_zero] ? month.with_leading_zero : month.to_s
|
78
|
+
when '{week_num}' then tokens[:week_num] = wday
|
79
|
+
when '{week_days}' then tokens[:week_days] = strftime('%w')
|
80
|
+
when '{short_years}' then tokens[:short_years] = strftime('%y')
|
81
|
+
when '{years}' then tokens[:years] = year
|
82
|
+
when '{short_week_day_name}' then tokens[:short_week_day_name] = language.tr(Tml.config.default_abbr_day_name(wday), 'Short name for a day of a week', {}, options)
|
83
|
+
when '{week_day_name}' then tokens[:week_day_name] = language.tr(Tml.config.default_day_name(wday), 'Day of a week', {}, options)
|
84
|
+
when '{short_month_name}' then tokens[:short_month_name] = language.tr(Tml.config.default_abbr_month_name(month - 1), 'Short month name', {}, options)
|
85
|
+
when '{month_name}' then tokens[:month_name] = language.tr(Tml.config.default_month_name(month - 1), 'Month name', {}, options)
|
86
|
+
when '{day_of_month}' then tokens[:day_of_month] = strftime('%e')
|
87
|
+
else
|
88
|
+
''
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
language.tr(label, nil, tokens, options)
|
93
|
+
end
|
94
|
+
alias :tr :translate
|
95
|
+
|
96
|
+
def trl(format = :default, options = {})
|
97
|
+
tr(format, options.merge!(:skip_decorations => true))
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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 Fixnum
|
33
|
+
|
34
|
+
def with_leading_zero
|
35
|
+
(to_i < 10 ? "0#{to_s}" : to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
def translate(desc = '', tokens = {}, options = {})
|
39
|
+
to_s.translate(desc, tokens, options)
|
40
|
+
end
|
41
|
+
alias tr translate
|
42
|
+
|
43
|
+
def trl(desc = '', tokens = {}, options = {})
|
44
|
+
to_s.trl(desc, tokens, options)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|