Class: LocoStrings::XCStringsEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_strings/encoders/xcstrings_encoder.rb

Overview

The XCStringsEncoder class is responsible for encoding the LocoStrings data to the XCStrings format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings, translations, languages, language) ⇒ XCStringsEncoder

Returns a new instance of XCStringsEncoder.



10
11
12
13
14
15
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 10

def initialize(strings, translations, languages, language)
  @strings = strings
  @translations = translations
  @languages = languages
  @language = language
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



8
9
10
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 8

def language
  @language
end

#languagesObject (readonly)

Returns the value of attribute languages.



8
9
10
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 8

def languages
  @languages
end

#stringsObject (readonly)

Returns the value of attribute strings.



8
9
10
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 8

def strings
  @strings
end

#translationsObject (readonly)

Returns the value of attribute translations.



8
9
10
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 8

def translations
  @translations
end

Instance Method Details

#encodeObject

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/loco_strings/encoders/xcstrings_encoder.rb', line 17

def encode
  raise Error, "The base language is not defined" if @language.nil?

  json = {
    "sourceLanguage" => @language,
    "strings" => {},
    "version" => "1.0"
  }
  generate_keys.each do |key|
    json["strings"][key] = encode_key(key)
  end
  JSON.pretty_generate(json, { space_before: " " })
end