loco_strings 0.1.4.1 → 0.1.6

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: c2d39a13c5afee6bd306c83aee1793452203d521b426645f1fa885b4b3da1e29
4
- data.tar.gz: bd6c23d23e3fb41c39d213f5f33b4f787cf4bea2e323a46b018382a402ca98ec
3
+ metadata.gz: 83291f99249e590483aa4c51d5710eada7bc80a910048bf0ba20b66a25ff187f
4
+ data.tar.gz: 11a3bbb921de0df05fe594518b98a254dc27531a8c312e383827090fb6816391
5
5
  SHA512:
6
- metadata.gz: 90f78c9ba808022d66e4495906c67699023cf224b552ad67b28db15ef040d1da3afabb7b72423630af5d46d17f857fa77cae032881669b222e6d17cb812d8aea
7
- data.tar.gz: 65d4ee5ce0b91ba6e084e0ad84942e88a499476d334d5b8990e097312983b5a0ecd4a153275ff953fe9704f72df69a83320fe02694ac8357e7b3b3130ba39aab
6
+ metadata.gz: 36f248bd8877845fa3983df90cf0903ca7fb26df5fefaeff0a52c98a5b5393e0d1e9a7121bc068db6b0c169b78899796807dabc119c45616d5ce8d77096ba2e5
7
+ data.tar.gz: 6df549b39d2ba998aff3899eb27729a988d428d76ad7c14789f5c0a1e64b920a1eb6f7f6a0051beb24754baf3825413fe2dad08519f6ffb02d7097af4255cb3b
@@ -5,7 +5,7 @@ require "json"
5
5
  module LocoStrings
6
6
  # The XCStringsDecoder class is responsible for decoding the XCStrings file format.
7
7
  class XCStringsDecoder
8
- attr_reader :language, :strings, :translations, :languages
8
+ attr_reader :language, :strings, :translations, :languages, :extraction_states
9
9
 
10
10
  def initialize(file_path)
11
11
  @file_path = file_path
@@ -13,6 +13,7 @@ module LocoStrings
13
13
  @translations = {}
14
14
  @languages = []
15
15
  @language = nil
16
+ @extraction_states = {}
16
17
  end
17
18
 
18
19
  def decode
@@ -43,9 +44,12 @@ module LocoStrings
43
44
  def decode_strings(json)
44
45
  strings = json["strings"]
45
46
  strings.each do |key, value|
47
+ translatable = value.fetch("shouldTranslate", true)
48
+ @extraction_states[key] = value["extractionState"] if value.key?("extractionState")
46
49
  val = decode_string(key, value, @language)
47
50
  @strings[key] = val if val
48
51
  @strings[key] = LocoString.new(key, key, value["comment"], "new") if val.nil?
52
+ @strings[key].translatable = translatable
49
53
 
50
54
  decode_translations(key, value)
51
55
  end
@@ -77,7 +81,7 @@ module LocoStrings
77
81
  plural = decode_plural(variations, comment)
78
82
  return nil if plural.empty?
79
83
 
80
- variation = LocoVariantions.new(key, comment)
84
+ variation = LocoVariantions.new(key, nil, comment)
81
85
  plural.each do |unit|
82
86
  variation.append_string(unit)
83
87
  end
@@ -7,11 +7,12 @@ module LocoStrings
7
7
  class XCStringsEncoder
8
8
  attr_reader :language, :strings, :translations, :languages
9
9
 
10
- def initialize(strings, translations, languages, language)
10
+ def initialize(strings, translations, languages, language, extraction_states = {})
11
11
  @strings = strings
12
12
  @translations = translations
13
13
  @languages = languages
14
14
  @language = language
15
+ @extraction_states = extraction_states || {}
15
16
  end
16
17
 
17
18
  def encode
@@ -40,17 +41,46 @@ module LocoStrings
40
41
 
41
42
  def encode_key(key)
42
43
  row = {}
43
- @translations.each do |language, translation|
44
- next unless translation.key?(key)
44
+ sorted_keys.each do |language|
45
+ process_language(row, language, key)
46
+ end
47
+ state = @extraction_states[key]
48
+ return row if state.nil?
45
49
 
46
- value = translation[key]
47
- next if value.nil?
50
+ # Keep Xcode's alphabetical field order: comment, extractionState, localizations, shouldTranslate.
51
+ ordered = {}
52
+ ordered["comment"] = row.delete("comment") if row.key?("comment")
53
+ ordered["extractionState"] = state
54
+ ordered.merge!(row)
55
+ ordered
56
+ end
48
57
 
49
- row["comment"] = value.comment unless row.key?("comment") || value.comment.nil?
50
- row["localizations"] ||= {}
51
- row["localizations"][language] = encode_value(value)
52
- end
53
- row
58
+ def sorted_keys
59
+ @translations.keys.sort_by(&:to_s)
60
+ end
61
+
62
+ def process_language(row, language, key)
63
+ return unless @translations[language].key?(key)
64
+
65
+ value = @translations[language][key]
66
+ return if value.nil?
67
+
68
+ add_comment(row, value)
69
+ add_localization(row, language, value)
70
+ add_translation_flag(row, value)
71
+ end
72
+
73
+ def add_comment(row, value)
74
+ row["comment"] = value.comment unless row.key?("comment") || value.comment.nil?
75
+ end
76
+
77
+ def add_localization(row, language, value)
78
+ row["localizations"] ||= {}
79
+ row["localizations"][language] = encode_value(value)
80
+ end
81
+
82
+ def add_translation_flag(row, value)
83
+ row["shouldTranslate"] = false if value.translatable == false
54
84
  end
55
85
 
56
86
  def encode_value(value)
@@ -19,13 +19,14 @@ module LocoStrings
19
19
  @strings = decoder.strings
20
20
  @translations = decoder.translations
21
21
  @languages = decoder.languages
22
+ @extraction_states = decoder.extraction_states
22
23
  @strings
23
24
  end
24
25
 
25
26
  def write
26
27
  raise Error, "The base language is not defined" if @language.nil?
27
28
 
28
- json = XCStringsEncoder.new(@strings, @translations, @languages, @language).encode
29
+ json = XCStringsEncoder.new(@strings, @translations, @languages, @language, @extraction_states || {}).encode
29
30
  File.write(@file_path, json)
30
31
  end
31
32
 
@@ -52,6 +53,10 @@ module LocoStrings
52
53
  @strings[key] = variations if @language == language
53
54
  end
54
55
 
56
+ def update_traslatability(key, translatable)
57
+ @strings[key].translatable = translatable
58
+ end
59
+
55
60
  def select_language(language)
56
61
  raise Error, "The base language is aready defined" unless @language.nil?
57
62
 
@@ -95,6 +100,7 @@ module LocoStrings
95
100
  @translations = {}
96
101
  @languages = []
97
102
  @language = nil
103
+ @extraction_states = {}
98
104
  end
99
105
 
100
106
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LocoStrings
4
- VERSION = "0.1.4.1"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/loco_strings.rb CHANGED
@@ -9,42 +9,54 @@ require_relative "loco_strings/parsers/xcstrings_file"
9
9
  module LocoStrings
10
10
  class Error < StandardError; end
11
11
 
12
- LocoString = Struct.new(:key, :value, :comment, :state) do
13
- def initialize(key, value, comment = nil, state = nil)
14
- super
12
+ LocoString = Struct.new(:key, :value, :comment, :state, :translatable) do
13
+ def initialize(key, value, comment = nil, state = nil, translatable = nil)
14
+ translatable = true if translatable.nil?
15
+ super(key, value, comment, state, translatable)
15
16
  end
16
17
 
17
- def update(value, comment = nil, state = nil)
18
+ def update(value, comment = nil, state = nil, translatable = nil)
18
19
  self.value = value
19
20
  self.comment = comment unless comment.nil?
20
21
  self.state = state
22
+ self.translatable = translatable unless translatable.nil?
21
23
  end
22
24
 
23
25
  def to_s
24
- "Key: #{key}, Value: #{value}, Comment: #{comment || "None"}, State: #{state || "None"}"
26
+ if translatable
27
+ "Key: #{key}, Value: #{value}, Comment: #{comment || "None"}, " \
28
+ "State: #{state || "None"}"
29
+ else
30
+ "Key: #{key}, Value: #{value}, Comment: #{comment || "None"}, " \
31
+ "State: #{state || "None"}, Translatable: #{translatable}"
32
+ end
25
33
  end
26
34
  end
27
35
 
28
- LocoVariantions = Struct.new(:key, :strings, :comment) do
29
- def initialize(key, strings = nil, comment = nil)
30
- super
31
- self.strings = strings || {}
36
+ LocoVariantions = Struct.new(:key, :strings, :comment, :translatable) do
37
+ def initialize(key, strings = nil, comment = nil, translatable = nil)
38
+ super(key, strings || {}, comment, translatable || true)
32
39
  end
33
40
 
34
41
  def append_string(string)
35
42
  strings[string.key] = string
36
43
  end
37
44
 
38
- def update_variant(key, value, comment = nil, state = nil)
45
+ def update_variant(key, value, comment = nil, state = nil, translatable = nil)
39
46
  if strings.key? key
40
- strings[key].update(value, comment, state)
47
+ strings[key].update(value, comment, state, translatable)
41
48
  else
42
- strings[key] = LocoString.new(key, value, comment, state)
49
+ strings[key] = LocoString.new(key, value, comment, state, nil, translatable)
43
50
  end
44
51
  end
45
52
 
46
53
  def to_s
47
- "Key: #{key}, Strings: #{strings}, Comment: #{comment || "None"}"
54
+ if translatable
55
+ "Key: #{key}, Strings: #{strings}, Comment: #{comment || "None"}"
56
+ else
57
+ "Key: #{key}, Strings: #{strings}, Comment: #{comment || "None"}, " \
58
+ "Translatable: #{translatable}"
59
+ end
48
60
  end
49
61
  end
50
62
 
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Layout/LineLength
4
+
5
+ require_relative "lib/loco_strings/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "loco_strings"
9
+ spec.version = LocoStrings::VERSION
10
+ spec.authors = ["Aleksei Cherepanov"]
11
+ spec.email = ["ftp27host@gmail.com"]
12
+
13
+ spec.summary = "LocoStrings is a Ruby gem for working with iOS and Android localization strings."
14
+ spec.description = "LocoStrings is a powerful and easy-to-use Ruby gem that simplifies the process of managing localization strings for iOS and Android apps. With LocoStrings, you can easily extract, organize, and update your app's localized strings in one place, making it easy to maintain consistency across all of your app's translations. LocoStrings supports multiple file formats, including XLIFF and CSV, and provides a simple and intuitive API for working with your app's strings in code. Whether you're a solo developer or part of a team, LocoStrings makes managing your app's localization a breeze."
15
+ spec.homepage = "https://github.com/ftp27/loco_strings"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 2.6.0"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_dependency "nokogiri", "~> 1.13", ">= 1.13.8"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ spec.metadata["rubygems_mfa_required"] = "true"
39
+ end
40
+
41
+ # rubocop:enable Layout/LineLength
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loco_strings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Cherepanov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-26 00:00:00.000000000 Z
11
+ date: 2026-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -60,6 +60,7 @@ files:
60
60
  - lib/loco_strings/parsers/loco_file.rb
61
61
  - lib/loco_strings/parsers/xcstrings_file.rb
62
62
  - lib/loco_strings/version.rb
63
+ - loco_strings.gemspec
63
64
  - sig/loco_strings.rbs
64
65
  homepage: https://github.com/ftp27/loco_strings
65
66
  licenses:
@@ -83,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  - !ruby/object:Gem::Version
84
85
  version: '0'
85
86
  requirements: []
86
- rubygems_version: 3.4.19
87
+ rubygems_version: 3.5.22
87
88
  signing_key:
88
89
  specification_version: 4
89
90
  summary: LocoStrings is a Ruby gem for working with iOS and Android localization strings.