Class: Tr8n::Tokens::Transform
- Defined in:
- lib/tr8n/tokens/transform.rb
Overview
Transform Token Form
|| one: message, many: messages || one: сообщение, few: сообщения, many: сообщений, other: много сообщений in other case the number is not displayed#
| message - will not include count, resulting in “messages” with implied count | message, messages
| message, messages
| he, she, he/she
| male: he, female: she, other: he/she
| did, does, will do | all male, all female, mixed genders
|| message, messages - will include count: “5 messages”
Instance Attribute Summary collapse
-
#pipe_separator ⇒ Object
readonly
Returns the value of attribute pipe_separator.
-
#piped_params ⇒ Object
readonly
Returns the value of attribute piped_params.
Attributes inherited from Data
#case_keys, #context_keys, #full_name, #label, #short_name
Class Method Summary collapse
Instance Method Summary collapse
- #displayed_in_translation? ⇒ Boolean
-
#generate_value_map(params, context) ⇒ Object
token: one: message, many: messages results in: “message”, “many”: “messages”.
- #implied? ⇒ Boolean
- #parse_elements ⇒ Object
- #prepare_label_for_suggestion(label, index, language) ⇒ Object
- #substitute(label, context, language, options = {}) ⇒ Object
Methods inherited from Data
#apply_case, #apply_language_cases, #context_for_language, #initialize, #key, #language_cases_enabled?, #name, #name_for_case_keys, parse, #sanitize, #sanitized_name, #to_s, token_object, #token_value, #token_value_from_array, #token_value_from_param_array, #token_value_from_param_hash
Constructor Details
This class inherits a constructor from Tr8n::Tokens::Data
Instance Attribute Details
#pipe_separator ⇒ Object (readonly)
Returns the value of attribute pipe_separator.
49 50 51 |
# File 'lib/tr8n/tokens/transform.rb', line 49 def pipe_separator @pipe_separator end |
#piped_params ⇒ Object (readonly)
Returns the value of attribute piped_params.
49 50 51 |
# File 'lib/tr8n/tokens/transform.rb', line 49 def piped_params @piped_params end |
Class Method Details
.expression ⇒ Object
51 52 53 |
# File 'lib/tr8n/tokens/transform.rb', line 51 def self.expression /(\{[^_:|][\w]*(:[\w]+)*(::[\w]+)*\s*\|\|?[^{^}]+\})/ end |
Instance Method Details
#displayed_in_translation? ⇒ Boolean
68 69 70 |
# File 'lib/tr8n/tokens/transform.rb', line 68 def displayed_in_translation? pipe_separator == "||" end |
#generate_value_map(params, context) ⇒ Object
token: one: message, many: messages results in: “message”, “many”: “messages”
token: message transform: [“{$0”, “other”: “Tr8n::Tokens::Transform.$0$0::plural”}, “{$0”, “other”: “$1”}] results in: “message”, “other”: “messages”
token: message, messages transform: [“{$0”, “other”: “Tr8n::Tokens::Transform.$0$0::plural”}, “{$0”, “other”: “$1”}] results in: “message”, “other”: “messages”
token: Dorogoi, Dorogaya transform: [“unsupported”, “{$0”, “female”: “$1”, “other”: “$0/$1”}] results in: “Dorogoi”, “female”: “Dorogaya”, “other”: “Dorogoi/Dorogaya”
token: likes, like transform: [“unsupported”, “{$0”, “other”: “$1”}] results in: “likes”, “other”: “like”
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/tr8n/tokens/transform.rb', line 101 def generate_value_map(params, context) values = {} if params.first.index(':') params.each do |p| nv = p.split(':') values[nv.first.strip] = nv.last.strip end return values end unless context.token_mapping raise Tr8n::Exception.new("The token context #{context.keyword} does not support transformation for unnamed params: #{full_name}") end token_mapping = context.token_mapping # "unsupported" if token_mapping.is_a?(String) raise Tr8n::Exception.new("The token mapping #{token_mapping} does not support #{params.size} params: #{full_name}") end # ["unsupported", "unsupported", {}] if token_mapping.is_a?(Array) if params.size > token_mapping.size raise Tr8n::Exception.new("The token mapping #{token_mapping} does not support #{params.size} params: #{full_name}") end token_mapping = token_mapping[params.size-1] if token_mapping.is_a?(String) raise Tr8n::Exception.new("The token mapping #{token_mapping} does not support #{params.size} params: #{full_name}") end end # {} token_mapping.each do |key, value| values[key] = value value.scan(/({\$\d(::\w+)*})/).each do |matches| token = matches.first parts = token[1..-2].split('::') index = parts.first.gsub('$', '').to_i if params.size < index raise Tr8n::Exception.new("The index inside #{context.token_mapping} is out of bound: #{full_name}") end # apply settings cases value = params[index] if language_cases_enabled? parts[1..-1].each do |case_key| lcase = context.language.case_by_keyword(case_key) unless lcase raise Tr8n::Exception.new("Language case #{case_key} for context #{context.keyword} is not defined: #{full_name}") end value = lcase.apply(value) end end values[key] = values[key].gsub(token, value) end end values end |
#implied? ⇒ Boolean
72 73 74 |
# File 'lib/tr8n/tokens/transform.rb', line 72 def implied? not displayed_in_translation? end |
#parse_elements ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tr8n/tokens/transform.rb', line 55 def parse_elements name_without_parens = @full_name[1..-2] name_without_pipes = name_without_parens.split('|').first.strip name_without_case_keys = name_without_pipes.split('::').first.strip @short_name = name_without_pipes.split(':').first.strip @case_keys = name_without_pipes.scan(/(::\w+)/).flatten.uniq.collect{|c| c.gsub('::', '')} @context_keys = name_without_case_keys.scan(/(:\w+)/).flatten.uniq.collect{|c| c.gsub(':', '')} @pipe_separator = (full_name.index("||") ? "||" : "|") @piped_params = name_without_parens.split(pipe_separator).last.split(",").collect{|param| param.strip} end |
#prepare_label_for_suggestion(label, index, language) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/tr8n/tokens/transform.rb', line 76 def prepare_label_for_suggestion(label, index, language) context = context_for_language(language) values = generate_value_map(piped_params, context) label.gsub(full_name, values[context.default_rule] || values.values.first) end |
#substitute(label, context, language, options = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/tr8n/tokens/transform.rb', line 164 def substitute(label, context, language, = {}) object = self.class.token_object(context, key) unless object Tr8n::Logger.error("Missing value for a token \"#{key}\" in \"#{label}\"") return label end if piped_params.empty? Tr8n::Logger.error("Piped params may not be empty for token \"#{key}\" in \"#{label}\"") return label end language_context = context_for_language(language) piped_values = generate_value_map(piped_params, language_context) rule = language_context.find_matching_rule(object) return label unless rule value = piped_values[rule.keyword] if value.nil? and language_context.fallback_rule value = piped_values[language_context.fallback_rule.keyword] end return label unless value substitution_value = [] if displayed_in_translation? substitution_value << token_value(Tr8n::Utils.hash_value(context, key), language, ) substitution_value << " " else value = value.gsub("##{short_name}#", token_value(Tr8n::Utils.hash_value(context, key), language, )) end substitution_value << value label.gsub(full_name, substitution_value.join("")) end |