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
98
99
100
101
102
103
104
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 98
def clean
@strings = {}
@translations = {}
@languages = []
@language = nil
@extraction_states = {}
end
|
#read ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 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
@extraction_states = decoder.
@strings
end
|
#select_language(language) ⇒ Object
60
61
62
63
64
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 60
def select_language(language)
raise Error, "The base language is aready defined" unless @language.nil?
@language = language
end
|
#to_s ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 87
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
83
84
85
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 83
def unit(key, language = @language)
@translations.dig(language, key)
end
|
#update(key, value, comment = nil, stage = nil, language = @language) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 33
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
56
57
58
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 56
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
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 45
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
66
67
68
69
70
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 66
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
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 72
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
26
27
28
29
30
31
|
# File 'lib/loco_strings/parsers/xcstrings_file.rb', line 26
def write
raise Error, "The base language is not defined" if @language.nil?
json = XCStringsEncoder.new(@strings, @translations, @languages, @language, @extraction_states || {}).encode
File.write(@file_path, json)
end
|