class TripAdvisor::StringsFile

Attributes

path[R]
translations[R]

Public Class Methods

read(path) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 8
def self.read(path)
  self.new.tap { |sf| sf.read(path) }
end

Public Instance Methods

[](key) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 46
def [](key)
  raise "Cannot access translations without reading a Strings file" unless loaded?
  translations.detect { |translation| translation.key == key.to_s }
end
keys() click to toggle source
# File lib/trip_advisor/strings_file.rb, line 66
def keys
  @twine_strings_file.sections.map do |section|
    section.rows.map do |row|
      row.key
    end
  end
end
language_tags() click to toggle source
# File lib/trip_advisor/strings_file.rb, line 62
def language_tags
  @twine_strings_file.language_codes
end
loaded?() click to toggle source
# File lib/trip_advisor/strings_file.rb, line 42
def loaded?
  @twine_strings_file != nil
end
merge_translations(translations) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 12
def merge_translations(translations)
  translations.each do |translation|
    if twine_row = twine_row_for_key(translation['en'].to_s)
      update_twine_row_with_translation(twine_row, translation)
    else
      add_twine_row_for_translation(translation)
    end
  end
end
merge_translations_android(translations) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 32
def merge_translations_android(translations)
  translations.each do |translation|
    if twine_row = twine_row_for_key(translation.key)
      update_twine_row_with_translation(twine_row, translation)
    else
      add_twine_row_for_translation_android(translation)
    end
  end
end
merge_translations_by_key(translations) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 22
def merge_translations_by_key(translations)
  translations.each do |translation|
    if twine_row = twine_row_for_key(translation.key)
      update_twine_row_with_translation(twine_row, translation)
    else
      add_twine_row_for_translation_key(translation)
    end
  end
end
read(path) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 51
def read(path)
  @path = path
  @twine_strings_file = Twine::StringsFile.new
  @twine_strings_file.read(path)
  load_translations_from_twine
end
write(path = self.path) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 58
def write(path = self.path)
  @twine_strings_file.write(path)
end

Private Instance Methods

add_twine_row_for_translation(translation) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 106
def add_twine_row_for_translation(translation)
  # We merge new rows into the first section
  section = @twine_strings_file.sections.first
  english_string = translation['en']
  raise "Cannot add Translation: No English ('en') localization available" unless english_string
  row = Twine::StringsRow.new(english_string).tap do |row|
    row.tags = ["key:#{translation.key}"]
    update_twine_row_with_translation(row, translation)
  end
  section.rows << row
end
add_twine_row_for_translation_android(translation) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 130
def add_twine_row_for_translation_android(translation)
  # We merge new rows into the first section
  section = @twine_strings_file.sections.first
  translation_tool_key = translation.key
  raise "Cannot add Translation: No translation tool key." unless translation_tool_key
  row = Twine::StringsRow.new(translation_tool_key).tap do |row|
    row.tags = ["key:#{translation.key}"]
    update_twine_row_with_translation(row, translation)
  end
  section.rows << row
end
add_twine_row_for_translation_key(translation) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 118
def add_twine_row_for_translation_key(translation)
  # We merge new rows into the first section
  section = @twine_strings_file.sections.first
  translation_tool_key = translation.key
  raise "Cannot add Translation: No translation tool key." unless translation_tool_key
  row = Twine::StringsRow.new(translation_tool_key).tap do |row|
    row.tags = ["key:#{translation.key}"]
    update_twine_row_with_translation(row, translation)
  end
  section.rows << row
end
load_translations_from_twine() click to toggle source
# File lib/trip_advisor/strings_file.rb, line 75
def load_translations_from_twine
  @translations = @twine_strings_file.sections.collect do |section|
    section.rows.map do |row|
      Translation.new(key: row.key, note: row.comment).tap do |translation|
        translation.localizations = row.translations.map do |locale_identifier, string|
          Translation::Localization.new(locale_identifier: locale_identifier, string: string)
        end
      end
    end
  end.flatten
end
twine_row_for_key(key) click to toggle source

NOTE: For Android, the Twine files are keyed by Translation Tool key, not English translation. NOTE: The Twine files are keyed by English translation, not Translation Tool key

# File lib/trip_advisor/strings_file.rb, line 89
def twine_row_for_key(key)
  @twine_strings_file.sections.each do |section|
    if row = section.rows.detect { |row| row.key == key }
      return row
    end
  end
  nil
end
update_twine_row_with_translation(twine_row, translation) click to toggle source
# File lib/trip_advisor/strings_file.rb, line 98
def update_twine_row_with_translation(twine_row, translation)
  twine_row.comment = translation.note
  translation.localizations.each do |localization|
    @twine_strings_file.add_language_code(localization.locale_identifier)
    twine_row.translations[localization.locale_identifier] = localization.string
  end
end