tml 5.6.5 → 5.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -55,6 +55,7 @@ module Tml
55
55
  end
56
56
 
57
57
  def parse_elements
58
+ name_without_parens = @full_name.gsub(/^%/, '')[1..-2]
58
59
  name_without_parens = @full_name.gsub(/^%/, '')[1..-2]
59
60
  name_without_case_keys = name_without_parens.split('::').first.strip
60
61
 
@@ -91,6 +92,7 @@ module Tml
91
92
 
92
93
  # Utility method for errors
93
94
  def error(msg, return_token = true)
95
+ # pp msg
94
96
  Tml.logger.error(msg)
95
97
  return_token ? full_name : label
96
98
  end
@@ -103,7 +105,14 @@ module Tml
103
105
 
104
106
  def self.token_object(token_values, token_name)
105
107
  return nil if token_values.nil?
106
- token_object = Tml::Utils.hash_value(token_values, token_name)
108
+ token_name = token_name.to_s.gsub(':', '')
109
+
110
+ if token_values.is_a?(Array)
111
+ token_object = token_values[token_name.to_i]
112
+ else
113
+ token_object = Tml::Utils.hash_value(token_values, token_name)
114
+ end
115
+
107
116
  return token_object.first if token_object.is_a?(Array)
108
117
  if token_object.is_a?(Hash)
109
118
  object = Tml::Utils.hash_value(token_object, :object)
@@ -112,6 +121,10 @@ module Tml
112
121
  token_object
113
122
  end
114
123
 
124
+ def token_object(token_values)
125
+ self.class.token_object(token_values, short_name)
126
+ end
127
+
115
128
  ##############################################################################
116
129
  #
117
130
  # tr("Hello {user_list}!", "", {:user_list => [[user1, user2, user3], :name]}}
@@ -0,0 +1,112 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2016 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
+ module Tml
34
+ module Tokens
35
+ class Decoration
36
+
37
+ RESERVED_TOKEN = 'tml'
38
+ TOKEN_PLACEHOLDER = '{$0}'
39
+
40
+ attr_reader :label, :full_name, :short_name, :default_name
41
+
42
+ def initialize(label, token)
43
+ @label = label
44
+ @full_name = token.to_s
45
+ @short_name = @full_name
46
+
47
+ # removing the numbers at the end - default tokens don't need them
48
+ @default_name = @short_name.gsub(/(\d)*$/, '')
49
+ end
50
+
51
+ def to_s
52
+ full_name
53
+ end
54
+
55
+ def default_decoration(token_content, decoration_token_values = nil)
56
+ default_decoration = Tml.config.default_token_value(default_name, :decoration)
57
+
58
+ unless default_decoration
59
+ return "<#{short_name}>#{token_content}</#{short_name}>"
60
+ end
61
+
62
+ # {$0} always represents the actual content
63
+ default_decoration = default_decoration.gsub(TOKEN_PLACEHOLDER, token_content.to_s)
64
+
65
+ # substitute the token values with hash elements
66
+ if decoration_token_values.is_a?(Hash)
67
+ decoration_token_values.each do |key, value|
68
+ default_decoration = default_decoration.gsub("{$#{key}}", value.to_s)
69
+ end
70
+ elsif decoration_token_values.is_a?(Array)
71
+ decoration_token_values.each_with_index do |value, index|
72
+ default_decoration = default_decoration.gsub("{$#{index + 1}}", value.to_s)
73
+ end
74
+ end
75
+
76
+ # remove unused attributes
77
+ default_decoration.gsub(/\{\$[^}]*\}/, '')
78
+ end
79
+
80
+ def allowed?(allowed_tokens)
81
+ return true if allowed_tokens.nil?
82
+ allowed_tokens.include?(short_name)
83
+ end
84
+
85
+ def apply(token_values, token_content, allowed_tokens = nil)
86
+ return token_content if short_name == RESERVED_TOKEN
87
+ return token_content unless allowed?(allowed_tokens)
88
+
89
+ method = Tml::Utils.hash_value(token_values, short_name)
90
+
91
+ if method
92
+ if method.is_a?(String)
93
+ return method.to_s.gsub(TOKEN_PLACEHOLDER, token_content)
94
+ end
95
+
96
+ if method.is_a?(Proc)
97
+ return method.call(token_content)
98
+ end
99
+
100
+ if method.is_a?(Array) || method.is_a?(Hash)
101
+ return default_decoration(token_content, method)
102
+ end
103
+
104
+ return token_content
105
+ end
106
+
107
+ default_decoration(token_content)
108
+ end
109
+
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2016 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
+ #######################################################################
34
+ #
35
+ # Map Token Forms
36
+ #
37
+ # tr("{user} likes this {animal @ dog: dog, cat: cat, bird: bird}", user: "Michael", animal: "dog")
38
+ # tr("{user} likes this {animal @ dog, cat, bird}", user: "Michael", animal: 0)
39
+ #
40
+ #######################################################################
41
+
42
+ class Tml::Tokens::Map < Tml::Tokens::Data
43
+
44
+ attr_reader :params
45
+
46
+ def self.expression
47
+ /(%?\{{1,2}\s*[\w]+\s*@\s*[^\{\}\|]+\}{1,2})/
48
+ end
49
+
50
+ def parse_elements
51
+ name_without_parens = @full_name.gsub(/^%/, '')[1..-2]
52
+ @context_keys = []
53
+ @case_keys = []
54
+ @short_name, @params = name_without_parens.split('@')
55
+ @short_name.strip!
56
+ @params = @params.split(',').collect{|param| param.strip}
57
+ if @params.first.index(':')
58
+ hash = {}
59
+ @params.each do |param|
60
+ key, value = param.split(':')
61
+ hash[key.to_s.strip] = value.to_s.strip
62
+ end
63
+ @params = hash
64
+ end
65
+ end
66
+
67
+ def substitute(label, context, language, options = {})
68
+ object = self.class.token_object(context, key)
69
+
70
+ if object.nil?
71
+ return error("Missing value for a token \"#{key}\" in \"#{label}\"", false)
72
+ end
73
+
74
+ if params.empty?
75
+ return error("Params may not be empty for token \"#{key}\" in \"#{label}\"", false)
76
+ end
77
+
78
+ object_value = params[object]
79
+
80
+ label.gsub(full_name, decorate(object_value, options))
81
+ end
82
+
83
+ end
@@ -68,7 +68,6 @@ class Tml::Tokens::Transform < Tml::Tokens::Data
68
68
  @short_name = name_without_pipes.split(':').first.strip
69
69
  @case_keys = name_without_pipes.scan(/(::\w+)/).flatten.uniq.collect{|c| c.gsub('::', '')}
70
70
  @context_keys = name_without_case_keys.scan(/(:\w+)/).flatten.uniq.collect{|c| c.gsub(':', '')}
71
-
72
71
  @pipe_separator = (full_name.index('||') ? '||' : '|')
73
72
  @piped_params = name_without_parens.split(pipe_separator).last.split(",").collect{|param| param.strip}
74
73
  end
@@ -0,0 +1,61 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2016 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
+ #######################################################################
34
+ #
35
+ # Choice Token
36
+ #
37
+ # {0} tagged himself/herself in {1,choice,singular#{1,number} {2,map,photo#photo|video#video}|plural#{1,number} {2,map,photo#photos|video#videos}}.
38
+ #
39
+ #######################################################################
40
+
41
+ class Tml::Tokens::XMessage::Choice < Tml::Tokens::Data
42
+
43
+ attr_accessor :rule_keys
44
+
45
+ def initialize(label, opts)
46
+ @label = label
47
+ @short_name = opts[:index]
48
+ @full_name = "{#{@short_name}}"
49
+ @rule_keys = opts[:styles].collect{|style| style[:key]}
50
+ @case_keys = []
51
+ @context_keys = []
52
+ if @rule_keys.include?('singular') or @rule_keys.include?('plural')
53
+ @context_keys = ['number']
54
+ elsif @rule_keys.include?('male') or @rule_keys.include?('female')
55
+ @context_keys = ['gender']
56
+ elsif @rule_keys.include?('past') or @rule_keys.include?('present') or @rule_keys.include?('future')
57
+ @context_keys = ['date']
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2016 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
+ #######################################################################
34
+ #
35
+ # Param Token
36
+ #
37
+ # {0} tagged himself/herself in {1,choice,singular#{1,number} {2,map,photo#photo|video#video}|plural#{1,number} {2,map,photo#photos|video#videos}}.
38
+ #
39
+ #######################################################################
40
+
41
+ class Tml::Tokens::XMessage::Data < Tml::Tokens::Data
42
+
43
+ def initialize(label, opts)
44
+ @label = label
45
+ @short_name = opts[:index]
46
+ @full_name = "{#{@short_name}}"
47
+ @case_keys = []
48
+ @context_keys = []
49
+ # if opts[:styles]
50
+ # @case_keys = []
51
+ # end
52
+ end
53
+
54
+ end
@@ -0,0 +1,101 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2016 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
+ #######################################################################
34
+ #
35
+ # Param Token
36
+ #
37
+ # {0} tagged himself/herself in {1,choice,singular#{1,number} {2,map,photo#photo|video#video}|plural#{1,number} {2,map,photo#photos|video#videos}}.
38
+ #
39
+ #######################################################################
40
+
41
+ class Tml::Tokens::XMessage::Decoration < Tml::Tokens::Decoration
42
+
43
+ DEFAULT_DECORATION_PLACEHOLDER = '{!yield!}'
44
+
45
+ # {:index => "2",
46
+ # :type => "anchor",
47
+ # :styles => ...
48
+ # }
49
+ def initialize(label, opts)
50
+ @label = label
51
+ @type = opts[:type]
52
+ @short_name = opts[:index].to_s.gsub(':', '')
53
+ @full_name = "#{opts[:index]}}"
54
+ @default_name = @type
55
+ end
56
+
57
+ def token_value(token_object, language)
58
+ token_object
59
+ end
60
+
61
+ def token_object(token_values)
62
+ if token_values.is_a?(Array)
63
+ token_values[@short_name.to_i]
64
+ else
65
+ Tml::Utils.hash_value(token_values, @short_name)
66
+ end
67
+ end
68
+
69
+ def template(method)
70
+ if method
71
+ if method.is_a?(String)
72
+
73
+ # backwards compatibility to legacy code
74
+ if @type == 'anchor'
75
+ return "<a href='#{method}'>#{DEFAULT_DECORATION_PLACEHOLDER}</a>"
76
+ end
77
+
78
+ return method
79
+ end
80
+
81
+ if method.is_a?(Array) or method.is_a?(Hash)
82
+ return default_decoration(DEFAULT_DECORATION_PLACEHOLDER, method)
83
+ end
84
+
85
+ return DEFAULT_DECORATION_PLACEHOLDER
86
+ end
87
+
88
+ default_decoration(DEFAULT_DECORATION_PLACEHOLDER)
89
+ end
90
+
91
+ def open_tag(method)
92
+ @template = template(method)
93
+ # pp label: label, type: @type, template: @template, method: method
94
+ @template.split(DEFAULT_DECORATION_PLACEHOLDER).first
95
+ end
96
+
97
+ def close_tag
98
+ @template.split(DEFAULT_DECORATION_PLACEHOLDER).last
99
+ end
100
+
101
+ end