Class: Tr8n::TranslationKey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tr8n/translation_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_key(key_hash) ⇒ Object



72
73
74
# File 'app/models/tr8n/translation_key.rb', line 72

def self.cache_key(key_hash)
  "translation_key_#{key_hash}"
end

.can_create_from_sync_hash?(tkey_hash, translator, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


627
628
629
630
# File 'app/models/tr8n/translation_key.rb', line 627

def self.can_create_from_sync_hash?(tkey_hash, translator, opts = {})
  return false if tkey_hash["key"].blank? or tkey_hash["label"].blank? or tkey_hash["locale"].blank?
  true
end

.create_from_sync_hash(tkey_hash, default_translator, opts = {}) ⇒ Object

create translation key from API hash



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'app/models/tr8n/translation_key.rb', line 633

def self.create_from_sync_hash(tkey_hash, default_translator, opts = {})
  return unless can_create_from_sync_hash?(tkey_hash, default_translator, opts)
  
  # find or create translation key  
  tkey = Tr8n::TranslationKey.find_or_create(tkey_hash["label"], tkey_hash["description"])

  # we will keep the translations that need to be sent back
  remaining_translations = tkey.transations_sync_hashes(opts).dup

  added_trans = []
  (tkey_hash["translations"] || []).each do |thash|
    remaining_translations.delete(thash)

    # if the translation came from a linked translator, use the translator
    translator = default_translator
    if thash["translator_id"] # incoming translations from the remote server
      translator = Tr8n::Translator.find_by_id(thash["translator_id"]) || default_translator
    elsif thash["translator"]
      translator = Tr8n::Translator.create_from_sync_hash(thash["translator"], opts) || default_translator
    end
    
    # don't insert duplicate translations
    comparible_hash = thash.slice("locale", "label", "rules")
    next if tkey.transations_sync_hashes.include?(comparible_hash)
    
    translation = Tr8n::Translation.create_from_sync_hash(tkey, translator, thash, opts)
    
    translation.mark_as_synced! if translation
  end

  # need to send back translations that have not been added, but exist in the system
  # pp :after, remaining_sync_hashes
  [tkey, remaining_translations]
end

.filter_phrase_lock_optionsObject



714
715
716
717
718
# File 'app/models/tr8n/translation_key.rb', line 714

def self.filter_phrase_lock_options
   [["locked and unlocked", "any"],
    ["locked only", "locked"], 
    ["unlocked only", "unlocked"]]
end

.filter_phrase_status_optionsObject



708
709
710
711
712
# File 'app/models/tr8n/translation_key.rb', line 708

def self.filter_phrase_status_options
   [["any", "any"],
    ["pending approval", "pending"], 
    ["approved", "approved"]]
end

.filter_phrase_type_optionsObject

Search Methods



700
701
702
703
704
705
706
# File 'app/models/tr8n/translation_key.rb', line 700

def self.filter_phrase_type_options
  [["all", "any"], 
   ["without translations", "without"], 
   ["with translations", "with"],
   ["followed by me", "followed"]
  ] 
end

.find_or_create(label, desc = "", options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/tr8n/translation_key.rb', line 80

def self.find_or_create(label, desc = "", options = {})
  key = generate_key(label, desc).to_s
  
  tkey = Tr8n::Cache.fetch(cache_key(key)) do 
    existing_key = where(:key => key).first
    
    unless existing_key
      if options[:api]==:translate and (not Tr8n::Config.api[:allow_key_registration])
        raise Tr8n::KeyRegistrationException.new("Key registration through API is disabled!")  
      end
    end
    
    level = options[:level] || Tr8n::Config.block_options[:level] || Tr8n::Config.default_translation_key_level
    role_key = options[:role] || Tr8n::Config.block_options[:role] 
    if role_key # role overrides level
      level = Tr8n::Config.translator_roles[role_key]
      raise Tr8n::Exception("Unknown translator role: #{role_key}") unless level 
    end
    locale = options[:locale] || Tr8n::Config.block_options[:default_locale] || Tr8n::Config.default_locale
    
    existing_key ||= create(:key => key.to_s, 
                            :label => label, 
                            :description => desc, 
                            :locale => locale,
                            :level => level,
                            :admin => Tr8n::Config.block_options[:admin])

    mark_as_admin(existing_key, options)
    update_default_locale(existing_key, options)
    verify_key(existing_key, options)
    existing_key
  end
  
  track_source(tkey, options)  
  tkey  
end

.for_params(params) ⇒ Object



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'app/models/tr8n/translation_key.rb', line 720

def self.for_params(params)
  results = self.where("tr8n_translation_keys.locale <> ? and (level is null or level <= ?)", Tr8n::Config.current_language.locale, Tr8n::Config.current_user_is_translator? ? Tr8n::Config.current_translator.level : 0)
  
  if Tr8n::Config.enable_caching?
    results = results.where("verified_at is not null")
  end  
  
  unless params[:search].blank?
    results = results.where("(tr8n_translation_keys.label like ? or tr8n_translation_keys.description like ?)", "%#{params[:search]}%", "%#{params[:search]}%")
  end

  # for with and approved, allow user to specify the kinds
  if params[:phrase_type] == "with"
    results = results.where("tr8n_translation_keys.id in (select tr8n_translations.translation_key_id from tr8n_translations where tr8n_translations.language_id = ?)", Tr8n::Config.current_language.id)
    
    # if approved, ensure that translation key is locked
    if params[:phrase_status] == "approved" 
      results = results.where("tr8n_translation_keys.id in (select tr8n_translation_key_locks.translation_key_id from tr8n_translation_key_locks where tr8n_translation_key_locks.language_id = ? and tr8n_translation_key_locks.locked = ?)", Tr8n::Config.current_language.id, true)
    
      # if approved, ensure that translation key does not have a lock or unlocked
    elsif params[:phrase_status] == "pending" 
      results = results.where("tr8n_translation_keys.id not in (select tr8n_translation_key_locks.translation_key_id from tr8n_translation_key_locks where tr8n_translation_key_locks.language_id = ? and tr8n_translation_key_locks.locked = ?)", Tr8n::Config.current_language.id, true)
    end
          
  elsif params[:phrase_type] == "without"
    results = results.where("tr8n_translation_keys.id not in (select tr8n_translations.translation_key_id from tr8n_translations where tr8n_translations.language_id = ?)", Tr8n::Config.current_language.id)
    
  elsif params[:phrase_type] == "followed" and Tr8n::Config.current_user_is_translator?
    results = results.where("tr8n_translation_keys.id in (select tr8n_translator_following.object_id from tr8n_translator_following where tr8n_translator_following.translator_id = ? and tr8n_translator_following.object_type = ?)", Tr8n::Config.current_translator.id, 'Tr8n::TranslationKey')
  end
  
  if params[:phrase_lock] == "locked"
    results = results.where("tr8n_translation_keys.id in (select tr8n_translation_key_locks.translation_key_id from tr8n_translation_key_locks where tr8n_translation_key_locks.language_id = ? and tr8n_translation_key_locks.locked = ?)", Tr8n::Config.current_language.id, true)
    
  elsif params[:phrase_lock] == "unlocked"  
    results = results.where("tr8n_translation_keys.id not in (select tr8n_translation_key_locks.translation_key_id from tr8n_translation_key_locks where tr8n_translation_key_locks.language_id = ? and tr8n_translation_key_locks.locked = ?)", Tr8n::Config.current_language.id, true)
  end
  
  results.order("created_at desc")
end

.generate_key(label, desc = "") ⇒ Object



171
172
173
# File 'app/models/tr8n/translation_key.rb', line 171

def self.generate_key(label, desc = "")
  "#{Digest::MD5.hexdigest("#{label};;;#{desc}")}~"[0..-2]
end

.help_urlObject



676
677
678
# File 'app/models/tr8n/translation_key.rb', line 676

def self.help_url
  '/tr8n/help'
end

.mark_as_admin(tkey, options = {}) ⇒ Object

for backwards compatibility only - new keys will be marked as such



118
119
120
121
122
123
# File 'app/models/tr8n/translation_key.rb', line 118

def self.mark_as_admin(tkey, options = {})
  return if options[:skip_block_options]
  return unless Tr8n::Config.block_options[:admin]
  return if tkey.admin?
  tkey.update_attributes(:admin => true)
end

.randomObject



355
356
357
# File 'app/models/tr8n/translation_key.rb', line 355

def self.random
  self.limit(1).offset(count-1)
end

.substitute_tokens(label, tokens, options = {}, language = Tr8n::Config.default_language) ⇒ Object

this is done when the translations engine is disabled



464
465
466
467
# File 'app/models/tr8n/translation_key.rb', line 464

def self.substitute_tokens(label, tokens, options = {}, language = Tr8n::Config.default_language)
  return label.to_s if options[:skip_substitution] 
  Tr8n::TranslationKey.new(:label => label.to_s).substitute_tokens(label.to_s, tokens, options, language)
end

.titleObject

Feature Methods



672
673
674
# File 'app/models/tr8n/translation_key.rb', line 672

def self.title
  "Original Phrase in {language}".translate(nil, :language => Tr8n::Config.current_language.native_name)
end

.track_source(translation_key, options = {}) ⇒ Object

creates associations between the translation keys and sources used for the site map and javascript support



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/tr8n/translation_key.rb', line 143

def self.track_source(translation_key, options = {})

  # key source tracking must be enabled or request must come from an API (JavaScript) to get it registered with a source
  if Tr8n::Config.enable_key_source_tracking? or options[:api] == :translate

    # source can be passed into an individual key, or as a block or fall back on the controller/action
    source = options[:source] || Tr8n::Config.block_options[:source] || Tr8n::Config.current_source

    # should never be blank
    return if source.blank?

    # each page or component is identified by a translation source
    translation_source = Tr8n::TranslationSource.find_or_create(source, options[:url])

    # each key is associated with one or more sources
    translation_key_source = Tr8n::TranslationKeySource.find_or_create(translation_key, translation_source)

  end

  # for debugging purposes only - this will track the actual location of the key in the source
  if Tr8n::Config.enable_key_caller_tracking?    
    options[:caller] ||= caller
    options[:caller_key] = options[:caller].is_a?(Array) ? options[:caller].join(", ") : options[:caller].to_s
    options[:caller_key] = generate_key(options[:caller_key])
    translation_key_source.update_details!(options)
  end
end

.update_default_locale(tkey, options = {}) ⇒ Object

for backwards compatibility only - if locale is provided update it in the key



126
127
128
129
130
131
# File 'app/models/tr8n/translation_key.rb', line 126

def self.update_default_locale(tkey, options = {})
  return if options[:skip_block_options]
  return unless tkey.locale.blank?
  key_locale = Tr8n::Config.block_options[:default_locale] || Tr8n::Config.default_locale
  tkey.update_attributes(:locale => key_locale)
end

.verify_key(tkey, options) ⇒ Object

mark each key as verified - but only if caching is enabled verification is used to cleanup unused keys



135
136
137
138
139
# File 'app/models/tr8n/translation_key.rb', line 135

def self.verify_key(tkey, options)
  return unless Tr8n::Config.enable_key_verification?
  existing_key.update_attributes(:verified_at => Time.now)

end

Instance Method Details

#add_translation(label, rules = nil, language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator) ⇒ Object

Raises:



266
267
268
269
270
271
272
# File 'app/models/tr8n/translation_key.rb', line 266

def add_translation(label, rules = nil, language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator)
  raise Tr8n::Exception.new("The translator is blocked and cannot submit translations") if translator.blocked?
  raise Tr8n::Exception.new("The sentence contains dirty words") unless language.clean_sentence?(label)
  translation = Tr8n::Translation.create(:translation_key => self, :language => language, :translator => translator, :label => label, :rules => rules)
  translation.vote!(translator, 1)
  translation
end

#cache_keyObject



76
77
78
# File 'app/models/tr8n/translation_key.rb', line 76

def cache_key
  self.class.cache_key(key)
end

#can_be_locked?(translator = nil) ⇒ Boolean

Returns:

  • (Boolean)


520
521
522
523
524
# File 'app/models/tr8n/translation_key.rb', line 520

def can_be_locked?(translator = nil)
  translator ||= (Tr8n::Config.current_user_is_translator? ? Tr8n::Config.current_translator : nil)
  return false unless translator
  translator.admin? or translator.manager?
end

#can_be_translated?(translator = nil) ⇒ Boolean

Returns:

  • (Boolean)


513
514
515
516
517
518
# File 'app/models/tr8n/translation_key.rb', line 513

def can_be_translated?(translator = nil)
  return false if locked?
  translator ||= (Tr8n::Config.current_user_is_translator? ? Tr8n::Config.current_translator : nil)
  translator_level = translator ? translator.level : 0
  translator_level >= level
end

#can_be_unlocked?(translator = nil) ⇒ Boolean

Returns:

  • (Boolean)


526
527
528
529
530
# File 'app/models/tr8n/translation_key.rb', line 526

def can_be_unlocked?(translator = nil)
  translator ||= (Tr8n::Config.current_user_is_translator? ? Tr8n::Config.current_translator : nil)
  return false unless translator
  translator.admin? or translator.manager?
end

#clear_cacheObject



599
600
601
602
# File 'app/models/tr8n/translation_key.rb', line 599

def clear_cache
  # Tr8n::Cache.delete(cache_key)
  touch_sources
end

#clear_translations_cache_for_language(language = Tr8n::Config.current_language) ⇒ Object



293
294
295
# File 'app/models/tr8n/translation_key.rb', line 293

def clear_translations_cache_for_language(language = Tr8n::Config.current_language)
  Tr8n::Cache.delete(translations_cache_key(language)) 
end

#decorate_translation(language, translated_label, translated = true, options = {}) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'app/models/tr8n/translation_key.rb', line 532

def decorate_translation(language, translated_label, translated = true, options = {})
  return translated_label if options[:skip_decorations]
  return translated_label if Tr8n::Config.current_user_is_guest?
  return translated_label unless Tr8n::Config.current_user_is_translator?
  return translated_label unless Tr8n::Config.current_translator.enable_inline_translations?
  return translated_label unless can_be_translated?
  return translated_label if locked?(language)
  return translated_label if self.language == language

  classes = ['tr8n_translatable']
  
  if language.default?
    classes << 'tr8n_not_translated'
  elsif options[:fallback] 
    classes << 'tr8n_fallback'
  else
    classes << (translated ? 'tr8n_translated' : 'tr8n_not_translated')
  end  

  html = "<tr8n class='#{classes.join(' ')}' translation_key_id='#{id}'>"
  html << translated_label
  html << "</tr8n>"
  html
end

#default_decoration(language = Tr8n::Config.current_language, options = {}) ⇒ Object

TODO: move all this stuff out of the model to decorators



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'app/models/tr8n/translation_key.rb', line 488

def default_decoration(language = Tr8n::Config.current_language, options = {})
  return label if Tr8n::Config.current_user_is_guest?
  return label unless Tr8n::Config.current_user_is_translator?
  return label unless can_be_translated?
  return label if locked?(language)

  classes = ['tr8n_translatable']

  if valid_translations_for_language(language).any?
    classes << 'tr8n_translated'
  else
    classes << 'tr8n_not_translated'
  end

  html = "<tr8n class='#{classes.join(' ')}' translation_key_id='#{id}'>"
  html << sanitized_label
  html << "</tr8n>"
  html.html_safe    
end

#dictionary?Boolean

Returns:

  • (Boolean)


688
689
690
# File 'app/models/tr8n/translation_key.rb', line 688

def dictionary?
  true 
end

#find_all_valid_translations(translations) ⇒ Object

returns back grouped by context - used by API



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'app/models/tr8n/translation_key.rb', line 360

def find_all_valid_translations(translations)
  if translations.empty?
    return {:id => self.id, :key => self.key, :label => self.label, :original => true}
  end
  
  # if the first translation does not depend on any of the context rules
  # use it... we don't care about the rest of the rules.
  if translations.first.rules_hash.blank?
    return {:id => self.id, :key => self.key, :label => translations.first.label}
  end
  
  # build a context hash for every kind of context rules combinations
  # only the first one in the list should be used
  context_hash_matches = {}
  valid_translations = []
  translations.each do |translation|
    context_key = translation.rules_hash || ""
    next if context_hash_matches[context_key]
    context_hash_matches[context_key] = true
    if translation.rules_definitions
      valid_translations << {:label => translation.label, :context => translation.rules_definitions.clone}
    else
      valid_translations << {:label => translation.label}
    end
  end

  # always add the default one at the end, so if none of the rules matched, use the english one
  valid_translations << {:label => self.label} unless context_hash_matches[""]
  {:id => self.id, :key => self.key, :labels => valid_translations}
end

#find_first_valid_translation(language, token_values) ⇒ Object



391
392
393
394
395
396
397
398
# File 'app/models/tr8n/translation_key.rb', line 391

def find_first_valid_translation(language, token_values)
  # find the first translation in the order of the rank that matches the rules
  valid_translations_for_language(language).each do |translation|
    return translation if translation.matches_rules?(token_values)
  end
  
  nil
end

#find_first_valid_translation_for_language(language, token_values) ⇒ Object

language fallback approach each language can have a fallback language



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'app/models/tr8n/translation_key.rb', line 402

def find_first_valid_translation_for_language(language, token_values)
  translation = find_first_valid_translation(language, token_values)
  return [language, translation] if translation

  if Tr8n::Config.enable_fallback_languages?
    # recursevily go into the fallback language and look there
    # no need to go to the default language - there obviously won't be any translations for it
    # unless you really won't to keep the keys in the text, and translate the default language
    if language.fallback_language and not language.fallback_language.default?
      return find_first_valid_translation_for_language(language.fallback_language, token_values)
    end
  end  
  
  [language, nil]
end

#find_first_valid_translation_for_translator(language, translator, token_values) ⇒ Object

translator fallback approach each translator can have a fallback language, which may have a fallback language



420
421
422
423
424
425
426
427
428
429
# File 'app/models/tr8n/translation_key.rb', line 420

def find_first_valid_translation_for_translator(language, translator, token_values)
  translation = find_first_valid_translation(language, token_values)
  return [language, translation] if translation
  
  if translator.fallback_language and not translator.fallback_language.default?
    return find_first_valid_translation_for_language(translator.fallback_language, token_values)
  end

  [language, nil]
end

#followed?(translator = nil) ⇒ Boolean

Returns:

  • (Boolean)


260
261
262
263
264
# File 'app/models/tr8n/translation_key.rb', line 260

def followed?(translator = nil)
  translator ||= (Tr8n::Config.current_user_is_translator? ? Tr8n::Config.current_translator : nil)
  return false unless translator
  Tr8n::TranslatorFollowing.following_for(translator, self)
end

#generate_rule_permutations(language, translator, dependencies) ⇒ Object

“actor”=>{“gender”=>“true”, “target”=>“value”=>“true”}



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'app/models/tr8n/translation_key.rb', line 312

def generate_rule_permutations(language, translator, dependencies)
  return if dependencies.blank?
  
  token_rules = {}
  
  dependency_mapping = {}
  
  # make into {"actor"=>[1], "target"=>[1], "target_@1"=>[2]}
  dependencies.each do |dependency, rule_types|
    rule_types.keys.each_with_index do |rule_type, index|
      token_key = dependency + "_@#{index}"
      dependency_mapping[token_key] = dependency
      
      rules = language.default_rules_for(rule_type)
      token_rules[token_key] = [] unless token_rules[token_key]
      token_rules[token_key] << rules
      token_rules[token_key].flatten!
    end
  end
  
  language_translations = translations_for(language)
  
  new_translations = []
  token_rules.combinations.each do |combination|
    rules = []
    rules_hash = {}
    
    combination.each do |token, language_rule|
      token_key = dependency_mapping[token]
      rules_hash[token_key] ||= [] 
      rules_hash[token_key] << language_rule.id.to_s
    end
    
    rules = rules_hash.collect{|token_key, rule_ids| {:token => token_key, :rule_id => rule_ids}}

    # if the user has previously create this particular combination, move on...
    next if translation_with_such_rules_exist?(language_translations, translator, rules_hash)
    new_translations << Tr8n::Translation.create(:translation_key => self, :language => language, :translator => translator, :label => sanitized_label, :rules => rules)
  end
  
  new_translations
end

#glossaryObject



226
227
228
# File 'app/models/tr8n/translation_key.rb', line 226

def glossary
  @glossary ||= Tr8n::Glossary.where("keyword in (?)", words).order("keyword asc")
end

#glossary?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'app/models/tr8n/translation_key.rb', line 230

def glossary?
  not glossary.empty?
end

#inline_translations_for(language) ⇒ Object

used by the inline popup dialog, we don’t want to show blocked translations



285
286
287
# File 'app/models/tr8n/translation_key.rb', line 285

def inline_translations_for(language)
  translations_for(language, -50)
end

#languageObject



181
182
183
# File 'app/models/tr8n/translation_key.rb', line 181

def language
  @language ||= (locale ? Tr8n::Language.for(locale) : Tr8n::Config.default_language)
end

#language_rules_dependant_tokens(language = Tr8n::Config.current_language) ⇒ Object

returns only the tokens that depend on one or more rules of the language, if any defined for the language



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/tr8n/translation_key.rb', line 201

def language_rules_dependant_tokens(language = Tr8n::Config.current_language)
  toks = []
  included_token_hash = {}

  data_tokens.each do |token|
    next unless token.dependant?
    next if included_token_hash[token.name]
    
    token.language_rules.each do |rule_class|
      if language.rule_class_names.include?(rule_class.name)
        toks << token
        included_token_hash[token.name] = token
      end
    end
  end

  toks << Tr8n::Config.viewing_user_token_for(label) if language.gender_rules?
  toks.uniq
end

#levelObject



508
509
510
511
# File 'app/models/tr8n/translation_key.rb', line 508

def level
  return 0 if super.nil?
  super
end

#lock!(language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator) ⇒ Object



238
239
240
# File 'app/models/tr8n/translation_key.rb', line 238

def lock!(language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator)
  lock_for(language).lock!(translator)
end

#lock_for(language) ⇒ Object



234
235
236
# File 'app/models/tr8n/translation_key.rb', line 234

def lock_for(language)
  Tr8n::TranslationKeyLock.for(self, language)
end

#locked?(language = Tr8n::Config.current_language) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
# File 'app/models/tr8n/translation_key.rb', line 252

def locked?(language = Tr8n::Config.current_language)
  lock_for(language).locked?
end

#mark_as_synced!Object

Synchronization Methods



607
608
609
# File 'app/models/tr8n/translation_key.rb', line 607

def mark_as_synced!
  update_attributes(:synced_at => Time.now + 2.seconds)
end

#permutatable?(language = Tr8n::Config.current_language) ⇒ Boolean

determines whether the key can have rules generated for the language

Returns:

  • (Boolean)


222
223
224
# File 'app/models/tr8n/translation_key.rb', line 222

def permutatable?(language = Tr8n::Config.current_language)
  language_rules_dependant_tokens(language).any?
end

#reset_key!Object



175
176
177
178
179
# File 'app/models/tr8n/translation_key.rb', line 175

def reset_key!
  # remove old key from cache
  Tr8n::Cache.delete(cache_key)
  self.update_attributes(:key => self.class.generate_key(label, description))
end

#rules?Boolean

Returns:

  • (Boolean)


684
685
686
# File 'app/models/tr8n/translation_key.rb', line 684

def rules?
  translation_tokens? or Tr8n::Config.current_language.has_rules?
end

#source_mapObject



577
578
579
580
581
582
583
584
585
# File 'app/models/tr8n/translation_key.rb', line 577

def source_map
  @source_map ||= begin
    map = {}
    sources.each do |source|
      (map[source.domain.name] ||= []) << source
    end
    map
  end
end

#sources?Boolean

Returns:

  • (Boolean)


692
693
694
# File 'app/models/tr8n/translation_key.rb', line 692

def sources?
  true
end

#substitute_tokens(translated_label, token_values, options = {}, language = Tr8n::Config.current_language) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'app/models/tr8n/translation_key.rb', line 469

def substitute_tokens(translated_label, token_values, options = {}, language = Tr8n::Config.current_language)
  processed_label = translated_label.to_s.clone

  # substitute all data tokens
  Tr8n::TokenizedLabel.new(processed_label).data_tokens.each do |token|
    next unless tokenized_label.allowed_token?(token)
    processed_label = token.substitute(processed_label, token_values, options, language) 
  end

  # substitute all decoration tokens
  Tr8n::TokenizedLabel.new(processed_label).decoration_tokens.each do |token|
    next unless tokenized_label.allowed_token?(token)
    processed_label = token.substitute(processed_label, token_values, options, language) 
  end
  
  processed_label
end

#suggestions?Boolean

Returns:

  • (Boolean)


680
681
682
# File 'app/models/tr8n/translation_key.rb', line 680

def suggestions?
  true 
end

#to_sync_hash(opts = {}) ⇒ Object



611
612
613
614
615
616
617
618
619
# File 'app/models/tr8n/translation_key.rb', line 611

def to_sync_hash(opts = {})
  { 
    "key" => self.key, 
    "label" => self.label, 
    "description" => self.description, 
    "locale" => (locale || Tr8n::Config.default_locale), 
    "translations" => opts[:translations] || translations_for(opts[:languages], opts[:threshold] || Tr8n::Config.translation_threshold).collect{|t| t.to_sync_hash(opts)}
  }
end

#tokenized_labelObject



185
186
187
# File 'app/models/tr8n/translation_key.rb', line 185

def tokenized_label
  @tokenized_label ||= Tr8n::TokenizedLabel.new(label)
end

#touch_sourcesObject



587
588
589
590
591
# File 'app/models/tr8n/translation_key.rb', line 587

def touch_sources
  sources.each do |source|
    source.touch
  end
end

#transations_sync_hashes(opts = {}) ⇒ Object



621
622
623
624
625
# File 'app/models/tr8n/translation_key.rb', line 621

def transations_sync_hashes(opts = {})
  @transations_sync_hashes ||= begin
    translations.collect{|t| t.to_sync_hash(:comparible => true)}
  end  
end

#translate(language = Tr8n::Config.current_language, token_values = {}, options = {}) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'app/models/tr8n/translation_key.rb', line 431

def translate(language = Tr8n::Config.current_language, token_values = {}, options = {})
  return find_all_valid_translations(valid_translations_for_language(language)) if options[:api]
  
  if Tr8n::Config.disabled? or language.default?
    return substitute_tokens(label, token_values, options.merge(:fallback => false), language).html_safe
  end
  
  if Tr8n::Config.enable_translator_language? and Tr8n::Config.current_user_is_translator?
    translation_language, translation = find_first_valid_translation_for_translator(language, Tr8n::Config.current_translator, token_values)
  else  
    translation_language, translation = find_first_valid_translation_for_language(language, token_values)
  end
  
  # if you want to present the label in it's sanitized form - for the phrase list
  if options[:default_language] 
    return decorate_translation(language, sanitized_label, translation != nil, options).html_safe
  end
  
  if translation
    translated_label = substitute_tokens(translation.label, token_values, options, language)
    return decorate_translation(language, translated_label, translation != nil, options.merge(:fallback => (translation_language != language))).html_safe
  end

  # no translation found  
  translated_label = substitute_tokens(label, token_values, options, Tr8n::Config.default_language)
  decorate_translation(language, translated_label, translation != nil, options).html_safe  
end

#translation_with_such_rules_exist?(language_translations, translator, rules_hash) ⇒ Boolean

Returns:

  • (Boolean)


304
305
306
307
308
309
# File 'app/models/tr8n/translation_key.rb', line 304

def translation_with_such_rules_exist?(language_translations, translator, rules_hash)
  language_translations.each do |translation|
    return true if translation.matches_rule_definitions?(rules_hash)
  end
  false
end

#translations_cache_key(language) ⇒ Object



289
290
291
# File 'app/models/tr8n/translation_key.rb', line 289

def translations_cache_key(language)
  "translations_#{language.locale}_#{key}"
end

#translations_changed!(language = Tr8n::Config.current_language) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
# File 'app/models/tr8n/translation_key.rb', line 561

def translations_changed!(language = Tr8n::Config.current_language)
  clear_translations_cache_for_language(language)
  
  # update timestamp and clear cache
  update_translation_count! 
  
  # notify all language sources that translation has changed
  sources.each do |source|
    Tr8n::TranslationSourceLanguage.touch(source, language)
  end
end

#translations_for(language = nil, rank = nil) ⇒ Object

returns all translations for the key, language and minimal rank



275
276
277
278
279
280
281
282
# File 'app/models/tr8n/translation_key.rb', line 275

def translations_for(language = nil, rank = nil)
  translations = Tr8n::Translation.where("translation_key_id = ?", self.id)
  if language
    translations = translations.where("language_id in (?)", [language].flatten.collect{|lang| lang.id})
  end
  translations = translations.where("rank >= ?", rank) if rank
  translations.order("rank desc").all
end

#unlock!(language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator) ⇒ Object



242
243
244
# File 'app/models/tr8n/translation_key.rb', line 242

def unlock!(language = Tr8n::Config.current_language, translator = Tr8n::Config.current_translator)
  lock_for(language).unlock!(translator)
end

#unlock_all!Object



246
247
248
249
250
# File 'app/models/tr8n/translation_key.rb', line 246

def unlock_all!
  locks.each do |lock|
    lock.unlock!
  end
end

#unlocked?(language = Tr8n::Config.current_language) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'app/models/tr8n/translation_key.rb', line 256

def unlocked?(language = Tr8n::Config.current_language)
  not locked?(language)
end

#update_translation_count!Object



573
574
575
# File 'app/models/tr8n/translation_key.rb', line 573

def update_translation_count!
  update_attributes(:translation_count => Tr8n::Translation.count(:conditions => ["translation_key_id = ?", self.id]))
end

#valid_translations_for_language(language = Tr8n::Config.current_language) ⇒ Object

returns only the translations that meet the minimum rank



298
299
300
301
302
# File 'app/models/tr8n/translation_key.rb', line 298

def valid_translations_for_language(language = Tr8n::Config.current_language)
  Tr8n::Cache.fetch(translations_cache_key(language)) do
    translations_for(language, Tr8n::Config.translation_threshold)
  end
end

#verify!(time = Time.now) ⇒ Object



557
558
559
# File 'app/models/tr8n/translation_key.rb', line 557

def verify!(time = Time.now)
  update_attributes(:verified_at => time)
end