class PhraseApp::InContextEditor::BackendService
Attributes
blacklisted_keys[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 10 def initialize(args = {}) self end
Public Instance Methods
translate(*args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 14 def translate(*args) if to_be_translated_without_phraseapp?(args) # *Ruby 2.7+ keyword arguments warning* # # This method uses keyword arguments. # There is a breaking change in ruby that produces warning with ruby 2.7 and won't work as expected with ruby 3.0 # The "hash" parameter must be passed as keyword argument. # # Good: # I18n.t(:salutation, :gender => 'w', :name => 'Smith') # I18n.t(:salutation, **{ :gender => 'w', :name => 'Smith' }) # I18n.t(:salutation, **any_hash) # # Bad: # I18n.t(:salutation, { :gender => 'w', :name => 'Smith' }) # I18n.t(:salutation, any_hash) # kw_args = args[1] if kw_args.present? I18n.translate_without_phraseapp(args[0], **kw_args) else I18n.translate_without_phraseapp(args[0]) end else phraseapp_delegate_for(args) end end
Protected Instance Methods
api_wrapper()
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 182 def api_wrapper @api_wrapper ||= PhraseApp::InContextEditor::ApiWrapper.new end
blank?(str)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 155 def blank?(str) raise "blank?(str) can only be given a String or nil" unless str.is_a?(String) or str.nil? str.nil? or str == '' end
calling_template(string)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 151 def calling_template(string) string.match(/(views)(\/.+)(?>:[0-9]+:in)/) end
extract_normalized_key_from_args(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 91 def extract_normalized_key_from_args(args) transformed_args = transform_args(args) normalized_key(transformed_args) end
find_lookup_scope(caller)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 165 def find_lookup_scope(caller) split_path = caller[2][1..-1].split(".")[0].split("/") template_or_partial = remove_underscore_form_partial(split_path[-1]) split_path[-1] = template_or_partial split_path.map!(&:to_sym) end
given_key_from_args(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 61 def given_key_from_args(args) extract_normalized_key_from_args(args) end
has_been_forced_to_resolve_with_phraseapp?(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 57 def has_been_forced_to_resolve_with_phraseapp?(args) (args.last.is_a?(Hash) and args.last[:resolve] == false) end
has_been_given_blacklisted_key?(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 47 def has_been_given_blacklisted_key?(args) key = given_key_from_args(args) has_blacklist_entry_for_key?(key) end
has_been_given_ignored_key?(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 52 def has_been_given_ignored_key?(args) key = given_key_from_args(args) key_is_ignored?(key) end
has_blacklist_entry_for_key?(key)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 65 def has_blacklist_entry_for_key?(key) blacklisted_keys.each do |blacklisted_key| return true if present?(key.to_s[/\A#{blacklisted_key.gsub("*", ".*")}\Z/]) end false end
identify_caller()
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 138 def identify_caller translation_caller = nil send(:caller)[0..6].each do |intermediate_caller| translation_caller = calling_template(intermediate_caller) unless translation_caller end if present?(translation_caller) find_lookup_scope(translation_caller) else nil end end
key_is_ignored?(key)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 72 def key_is_ignored?(key) PhraseApp::InContextEditor.ignored_keys.each do |ignored_key| return true if present?(key.to_s[/\A#{ignored_key.gsub("*", ".*")}\Z/]) end false end
normalized_key(duped_args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 101 def normalized_key(duped_args) splitted_args = split_args(duped_args) key = I18n::Backend::Flatten.normalize_flat_keys(*splitted_args) key.gsub!("..", ".") key.gsub!(/^\./, '') key end
options_from_args(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 116 def options_from_args(args) args.last.is_a?(Hash) ? args.pop : {} end
phraseapp_delegate_for(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 83 def phraseapp_delegate_for(args) key = given_key_from_args(args) return nil unless present?(key) options = args[1].nil? ? {} : args[1] PhraseApp::InContextEditor::Delegate::I18nDelegate.new(key, options, args) end
present?(str)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 160 def present?(str) raise "present?(str) can only be given a String or nil" unless str.is_a?(String) or str.nil? not blank?(str) end
remove_underscore_form_partial(template_or_partial)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 174 def remove_underscore_form_partial(template_or_partial) if template_or_partial.to_s[0,1] == "_" template_or_partial.to_s[1..-1] else template_or_partial.to_s end end
split_args(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 109 def split_args(args) options = options_from_args(args) key ||= args.shift locale = options.delete(:locale) || I18n.locale return [locale, key, options[:scope], nil] end
to_be_translated_without_phraseapp?(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 43 def to_be_translated_without_phraseapp?(args) PhraseApp::InContextEditor.disabled? or has_been_given_blacklisted_key?(args) or has_been_given_ignored_key?(args) or has_been_forced_to_resolve_with_phraseapp?(args) end
transform_args(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 96 def transform_args(args) duped_args = args.map { |item| (item.is_a?(Symbol) or item.nil?) ? item : item.dup } transform_args_based_on_caller(duped_args) end
transform_args_based_on_caller(args)
click to toggle source
# File lib/phraseapp-in-context-editor-ruby/backend_service.rb, line 120 def transform_args_based_on_caller(args) translation_caller = identify_caller if translation_caller and args.first =~ /^\./ options = options_from_args(args) if not present?(options[:scope]) and present?(translation_caller) options[:scope] = translation_caller end args.push(options) parts = args.first.to_s.split(".").select { |e| not blank?(e) } args[0] = parts[0] if parts.size == 1 end args end