loco_strings 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ae94b18e40b03e455e87bd4c5a4a4b2139ee822e0f67fb7bddd4f525027a936
4
- data.tar.gz: f4decc1b1751dc3f9cdafdf7db9ac9e950f344e3990c2ed7db9373238e760193
3
+ metadata.gz: 567e3981149f49dd55a99138ca51993030011f886c227f3cd8809c5a0c1e48c5
4
+ data.tar.gz: d9424623eeb635a2798a4f858701be4fa1897714af4181caa5863dbcb8d4bf4d
5
5
  SHA512:
6
- metadata.gz: 7be956a59f8a41eabf7a7196cd03e0979f411b4f511c43c0c4a607af32b680e85b1e7d210445070786f43024d9eabcfe7b6c504283b93c1ddc1ca2206cd7b36e
7
- data.tar.gz: e80e535b831507b88eeb25e9d392ca4f39b91112c73d5f2074f69965cacbb8109d0cf3806694ed9164ce5cee417849e4ed13130885edd5efdab9b9dd6181fd2c
6
+ metadata.gz: 50404d9b25812983c3b6f537d4220572f7df87c837b19411fd22293cc6c1abae4550ec3e630bc359853b8092802cf8324a2b4db8b753ba830d20e0d42bae82c3
7
+ data.tar.gz: f650a177e645204d68e6cd65fa87214976b5c258c266d90bcdf61a1061bd49466a442dd74d3bac22b1fe7a86b13ecacedb90a32bff5fbf0db3e4c70fc4dd63e4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- loco_strings (0.1.1)
4
+ loco_strings (0.1.3)
5
5
  nokogiri (~> 1.13, >= 1.13.8)
6
6
 
7
7
  GEM
@@ -3,12 +3,6 @@ require "json"
3
3
 
4
4
  module LocoStrings
5
5
  class XCStringsFile < LocoFile
6
- def initialize(file_path, language)
7
- @file_path = file_path
8
- @language = language
9
- clean
10
- end
11
-
12
6
  def read
13
7
  clean
14
8
  return @strings unless File.exist?(@file_path)
@@ -16,21 +10,33 @@ module LocoStrings
16
10
  comment = nil
17
11
  file = File.read(@file_path)
18
12
  json = JSON.parse(file)
19
- sourceLanguage = json["sourceLanguage"]
13
+ extract_languages(json)
14
+ @language = json["sourceLanguage"]
15
+ @translations = {}
20
16
  strings = json["strings"]
21
17
  strings.each do |key, value|
22
18
  comment = value["comment"]
23
- translation = key unless @language != sourceLanguage
19
+ translation = key
24
20
  if value.has_key?("localizations") && value["localizations"].has_key?(@language)
25
21
  loc = value["localizations"][@language]
26
22
  end
27
23
  translation = loc["stringUnit"]["value"] unless loc.nil?
28
24
  @strings[key] = LocoString.new key, translation, comment unless translation.nil?
25
+ for language in @languages
26
+ next unless value.has_key?("localizations") && value["localizations"].has_key?(language)
27
+
28
+ loc = value["localizations"][language]
29
+ translation = loc["stringUnit"]["value"] unless loc.nil?
30
+ @translations[language] = {} unless @translations.has_key?(language)
31
+ @translations[language][key] = translation unless translation.nil?
32
+ end
29
33
  end
30
34
  @strings
31
35
  end
32
36
 
33
- def write(files = [])
37
+ def write
38
+ raise Error, "The base language is not defined" if @language.nil?
39
+
34
40
  json = {}
35
41
  json["sourceLanguage"] = @language
36
42
  json["strings"] = {}
@@ -46,16 +52,14 @@ module LocoStrings
46
52
  }
47
53
  }
48
54
  end
49
- files.each do |file|
50
- translation = file.value(key)
51
- next unless translation
52
-
53
- next unless key != translation
55
+ @languages.each do |language|
56
+ next if language == @language
57
+ next unless @translations.has_key?(language) && @translations[language].has_key?(key)
54
58
 
55
- localizations[file.language] = {
59
+ localizations[language] = {
56
60
  "stringUnit" => {
57
61
  "state" => "translated",
58
- "value" => translation
62
+ "value" => @translations[language][key]
59
63
  }
60
64
  }
61
65
  end
@@ -65,5 +69,62 @@ module LocoStrings
65
69
  json["version"] = "1.0"
66
70
  File.open(@file_path, "w") { |file| file.write(JSON.pretty_generate(json)) }
67
71
  end
72
+
73
+ def update(key, value, comment = nil, language = @language)
74
+ raise Error, "The base language is not defined" if @language.nil?
75
+
76
+ if @language != language
77
+ @languages << language unless @languages.include?(language)
78
+ @translations[language] = {} unless @translations.has_key?(language)
79
+ @translations[language][key] = value
80
+ @strings[key] = LocoString.new key, key, comment unless @strings.has_key?(key)
81
+ return
82
+ end
83
+ comment = @strings[key].comment if @strings.has_key?(key) && (comment.nil? && @strings.has_key?(key))
84
+ @strings[key] = LocoString.new key, value, comment
85
+ end
86
+
87
+ def value(key, language = nil)
88
+ return @strings[key] if language.nil? || language == @language
89
+ return @translations[language][key] if @translations.has_key?(language) && @translations[language].has_key?(key)
90
+ end
91
+
92
+ def clean
93
+ @strings = {}
94
+ @translations = {}
95
+ @languages = []
96
+ @language = nil
97
+ end
98
+
99
+ def set_language(language)
100
+ raise Error, "The base language is aready defined" unless @language.nil?
101
+
102
+ @language = language
103
+ end
104
+
105
+ def to_s
106
+ result = ""
107
+ result += "Base language: #{@language}\n" unless @language.nil?
108
+ result += "Languages: #{@languages}\n" unless @languages.empty?
109
+ result += "Strings:\n"
110
+ @strings.each do |key, value|
111
+ result += "#{key}: #{value}\n"
112
+ end
113
+ result
114
+ end
115
+
116
+ private
117
+
118
+ def extract_languages(json)
119
+ @languages = []
120
+ json["strings"].each do |_key, value|
121
+ next unless value.has_key?("localizations")
122
+
123
+ value["localizations"].each do |lang, _loc|
124
+ @languages << lang
125
+ end
126
+ end
127
+ @languages.uniq!
128
+ end
68
129
  end
69
130
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LocoStrings
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/loco_strings.rb CHANGED
@@ -13,9 +13,13 @@ module LocoStrings
13
13
  def initialize(key, value, comment = nil)
14
14
  super
15
15
  end
16
+
17
+ def to_s
18
+ "Key: #{key}, Value: #{value}, Comment: #{comment || "None"}"
19
+ end
16
20
  end
17
21
 
18
- def self.load(file_path, language = nil)
22
+ def self.load(file_path)
19
23
  ext = File.extname(file_path)
20
24
  raise Error, "Unsupported file format: #{ext}" unless [".strings", ".xml", ".xcstrings"].include? ext
21
25
 
@@ -25,9 +29,7 @@ module LocoStrings
25
29
  when ".xml"
26
30
  AndroidFile.new file_path
27
31
  when ".xcstrings"
28
- raise Error, "Language is required for xcstrings files" unless language
29
-
30
- XCStringsFile.new file_path, language
32
+ XCStringsFile.new file_path
31
33
  else
32
34
  raise Error, "Not implemented"
33
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loco_strings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Cherepanov