Class: LocoStrings::XCStringsFile

Inherits:
LocoFile
  • Object
show all
Defined in:
lib/loco_strings/parsers/xcstrings_file.rb

Overview

The XCStringsFile class is responsible for reading and writing the XCStrings file format.

Instance Attribute Summary

Attributes inherited from LocoFile

#file_path

Instance Method Summary collapse

Methods inherited from LocoFile

#delete, #initialize, #update_file_path

Constructor Details

This class inherits a constructor from LocoStrings::LocoFile

Instance Method Details

#cleanObject



93
94
95
96
97
98
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 93

def clean
  @strings = {}
  @translations = {}
  @languages = []
  @language = nil
end

#readObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 11

def read
  clean
  return @strings unless File.exist?(@file_path)

  decoder = XCStringsDecoder.new(@file_path)
  decoder.decode

  @language = decoder.language
  @strings = decoder.strings
  @translations = decoder.translations
  @languages = decoder.languages
  @strings
end

#select_language(language) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 55

def select_language(language)
  raise Error, "The base language is aready defined" unless @language.nil?

  @language = language
end

#to_sObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 82

def to_s
  result = ""
  result += "Base language: #{@language}\n" unless @language.nil?
  result += "Languages: #{@languages}\n" unless @languages.empty?
  result += "Strings:\n"
  @strings.each do |key, value|
    result += "#{key}: #{value}\n"
  end
  result
end

#unit(key, language = @language) ⇒ Object



78
79
80
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 78

def unit(key, language = @language)
  @translations.dig(language, key)
end

#update(key, value, comment = nil, stage = nil, language = @language) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 32

def update(key, value, comment = nil, stage = nil, language = @language)
  raise Error, "The base language is not defined" if language.nil?

  stage = "translated" if stage.nil?
  string = make_strings(key, value, comment, stage, language)
  return if string.nil?

  @translations[language] ||= {}
  @translations[language][key] = string
  @strings[key] = string if @language == language
end

#update_variation(key, variant, strings, comment = nil, state = nil, language = @language) ⇒ Object

rubocop:disable Metrics/ParameterLists

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 44

def update_variation(key, variant, strings, comment = nil, state = nil, language = @language) # rubocop:disable Metrics/ParameterLists
  raise Error, "The base language is not defined" if language.nil?

  variations = make_variations(key, variant, strings, comment, state, language)
  return if variations.nil?

  @translations[language] ||= {}
  @translations[language][key] = variations
  @strings[key] = variations if @language == language
end

#value(key) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 61

def value(key)
  raise Error, "The base language is not defined" if @language.nil?

  value_by_language(key, @language)
end

#value_by_language(key, language) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 67

def value_by_language(key, language)
  str = @translations.dig(language, key)
  return nil if str.nil?

  if str.is_a?(LocoString)
    str.value
  elsif str.is_a?(LocoVariantions)
    str.strings.map { |_, v| v.value }
  end
end

#writeObject

Raises:



25
26
27
28
29
30
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 25

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

  json = XCStringsEncoder.new(@strings, @translations, @languages, @language).encode
  File.write(@file_path, json)
end