Class: LocoStrings::XCStringsFile
- Inherits:
-
LocoFile
- Object
- LocoFile
- LocoStrings::XCStringsFile
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
-
#clean ⇒ Object
-
#read ⇒ Object
-
#select_language(language) ⇒ Object
-
#to_s ⇒ Object
-
#unit(key, language = @language) ⇒ Object
-
#update(key, value, comment = nil, stage = nil, language = @language) ⇒ Object
-
#update_traslatability(key, translatable) ⇒ Object
-
#update_variation(key, variant, strings, comment = nil, state = nil, language = @language) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#value(key) ⇒ Object
-
#value_by_language(key, language) ⇒ Object
-
#write ⇒ Object
Methods inherited from LocoFile
#delete, #initialize, #update_file_path
Instance Method Details
#clean ⇒ Object
97
98
99
100
101
102
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 97
def clean
@strings = {}
@translations = {}
@languages = []
@language = nil
end
|
#read ⇒ Object
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
59
60
61
62
63
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 59
def select_language(language)
raise Error, "The base language is aready defined" unless @language.nil?
@language = language
end
|
#to_s ⇒ Object
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 86
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
82
83
84
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 82
def unit(key, language = @language)
@translations.dig(language, key)
end
|
#update(key, value, comment = nil, stage = nil, language = @language) ⇒ Object
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, = 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, , stage, language)
return if string.nil?
@translations[language] ||= {}
@translations[language][key] = string
@strings[key] = string if @language == language
end
|
#update_traslatability(key, translatable) ⇒ Object
55
56
57
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 55
def update_traslatability(key, translatable)
@strings[key].translatable = translatable
end
|
#update_variation(key, variant, strings, comment = nil, state = nil, language = @language) ⇒ Object
rubocop:disable Metrics/ParameterLists
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, = nil, state = nil, language = @language)
raise Error, "The base language is not defined" if language.nil?
variations = make_variations(key, variant, strings, , state, language)
return if variations.nil?
@translations[language] ||= {}
@translations[language][key] = variations
@strings[key] = variations if @language == language
end
|
#value(key) ⇒ Object
65
66
67
68
69
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 65
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
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 71
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
|
#write ⇒ Object
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
|