loco_strings 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79d663be035e5b1ad273b17e729235204d0f38bbda6dcb69d55e6dd5077145f2
4
- data.tar.gz: 41c2bf22dabec94904f27817d8da2fcf60787a569a7adf3708bb34eadf8a2512
3
+ metadata.gz: 567e3981149f49dd55a99138ca51993030011f886c227f3cd8809c5a0c1e48c5
4
+ data.tar.gz: d9424623eeb635a2798a4f858701be4fa1897714af4181caa5863dbcb8d4bf4d
5
5
  SHA512:
6
- metadata.gz: de7b40862a9998f06ccf6473c5a7c17861d51693e7a6b788e779cfdc81350a313130ef36d4b8ff21f1693b4b76a1953d4f871a981677e28463055b3cc82b9d66
7
- data.tar.gz: 815cc8a29514e75611d74a1b6824252b78171b89ea02eacfbdf6c84c040d27b334416d0d2abbc8251584b1d6e673331947fe2919d457fead8c38179394d1b8b5
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
data/README.md CHANGED
@@ -10,6 +10,27 @@ That's where LocoStrings comes in. With LocoStrings, you can easily parse and ma
10
10
 
11
11
  In this README, we'll show you how to install and use LocoStrings in your Ruby projects. Let's get started!
12
12
 
13
+ ### Supported formats
14
+
15
+ Here is the list of currently supported files, as well as files that are still on the to-do list. I am still working on the library, and I hope to eventually finish all the files on the list. Contributions are welcome.
16
+
17
+ - [x] Android Resources
18
+ - [x] Apple Strings
19
+ - [ ] Gettext
20
+ - [ ] Java Properties
21
+ - [ ] Apple XLIFF
22
+ - [ ] Objective-C/Cocoa Properties
23
+ - [ ] .NET Resources
24
+ - [ ] JavaScript Object
25
+ - [ ] React Native (I18n library)
26
+ - [ ] Symphony PHP XLIFF
27
+ - [ ] Angular i18n XLIFF
28
+ - [ ] Ruby YAML
29
+ - [ ] Salesforce Translation
30
+ - [ ] QT Linguist
31
+ - [ ] Docx
32
+ - [ ] Flutter ARB file
33
+
13
34
  ## Installation
14
35
 
15
36
  Install the gem and add to the application's Gemfile by executing:
@@ -23,6 +23,7 @@ module LocoStrings
23
23
  end
24
24
 
25
25
  def update(key, value, comment = nil)
26
+ comment = @strings[key].comment if comment.nil? && @strings.has_key?(key)
26
27
  @strings[key] = LocoString.new key, value, comment
27
28
  end
28
29
 
@@ -0,0 +1,130 @@
1
+ require_relative "loco_file"
2
+ require "json"
3
+
4
+ module LocoStrings
5
+ class XCStringsFile < LocoFile
6
+ def read
7
+ clean
8
+ return @strings unless File.exist?(@file_path)
9
+
10
+ comment = nil
11
+ file = File.read(@file_path)
12
+ json = JSON.parse(file)
13
+ extract_languages(json)
14
+ @language = json["sourceLanguage"]
15
+ @translations = {}
16
+ strings = json["strings"]
17
+ strings.each do |key, value|
18
+ comment = value["comment"]
19
+ translation = key
20
+ if value.has_key?("localizations") && value["localizations"].has_key?(@language)
21
+ loc = value["localizations"][@language]
22
+ end
23
+ translation = loc["stringUnit"]["value"] unless loc.nil?
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
33
+ end
34
+ @strings
35
+ end
36
+
37
+ def write
38
+ raise Error, "The base language is not defined" if @language.nil?
39
+
40
+ json = {}
41
+ json["sourceLanguage"] = @language
42
+ json["strings"] = {}
43
+ @strings.each do |key, value|
44
+ row = {}
45
+ row["comment"] = value.comment unless value.comment.nil?
46
+ localizations = {}
47
+ if value.value != key && !value.value.nil?
48
+ localizations[@language] = {
49
+ "stringUnit" => {
50
+ "state" => "translated",
51
+ "value" => value.value
52
+ }
53
+ }
54
+ end
55
+ @languages.each do |language|
56
+ next if language == @language
57
+ next unless @translations.has_key?(language) && @translations[language].has_key?(key)
58
+
59
+ localizations[language] = {
60
+ "stringUnit" => {
61
+ "state" => "translated",
62
+ "value" => @translations[language][key]
63
+ }
64
+ }
65
+ end
66
+ row["localizations"] = localizations unless localizations.empty?
67
+ json["strings"][key] = row
68
+ end
69
+ json["version"] = "1.0"
70
+ File.open(@file_path, "w") { |file| file.write(JSON.pretty_generate(json)) }
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
129
+ end
130
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LocoStrings
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/loco_strings.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "loco_strings/version"
4
4
  require_relative "loco_strings/parsers/android_file"
5
5
  require_relative "loco_strings/parsers/ios_file"
6
+ require_relative "loco_strings/parsers/xcstrings_file"
6
7
 
7
8
  # LocoStrings is a Ruby gem for working with iOS and Android localization strings.
8
9
  module LocoStrings
@@ -12,17 +13,23 @@ module LocoStrings
12
13
  def initialize(key, value, comment = nil)
13
14
  super
14
15
  end
16
+
17
+ def to_s
18
+ "Key: #{key}, Value: #{value}, Comment: #{comment || "None"}"
19
+ end
15
20
  end
16
21
 
17
22
  def self.load(file_path)
18
23
  ext = File.extname(file_path)
19
- raise Error, "Unsupported file format: #{ext}" unless [".strings", ".xml"].include? ext
24
+ raise Error, "Unsupported file format: #{ext}" unless [".strings", ".xml", ".xcstrings"].include? ext
20
25
 
21
26
  case ext
22
27
  when ".strings"
23
28
  IosFile.new file_path
24
29
  when ".xml"
25
30
  AndroidFile.new file_path
31
+ when ".xcstrings"
32
+ XCStringsFile.new file_path
26
33
  else
27
34
  raise Error, "Not implemented"
28
35
  end
@@ -0,0 +1,40 @@
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
+ end
39
+
40
+ # 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.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksei Cherepanov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-02 00:00:00.000000000 Z
11
+ date: 2023-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -56,7 +56,9 @@ files:
56
56
  - lib/loco_strings/parsers/android_file.rb
57
57
  - lib/loco_strings/parsers/ios_file.rb
58
58
  - lib/loco_strings/parsers/loco_file.rb
59
+ - lib/loco_strings/parsers/xcstrings_file.rb
59
60
  - lib/loco_strings/version.rb
61
+ - loco_strings.gemspec
60
62
  - sig/loco_strings.rbs
61
63
  homepage: https://github.com/ftp27/loco_strings
62
64
  licenses:
@@ -79,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
81
  - !ruby/object:Gem::Version
80
82
  version: '0'
81
83
  requirements: []
82
- rubygems_version: 3.4.10
84
+ rubygems_version: 3.4.19
83
85
  signing_key:
84
86
  specification_version: 4
85
87
  summary: LocoStrings is a Ruby gem for working with iOS and Android localization strings.