missing_translation 0.2.1 → 0.3.0

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: 5935fba9f02fe6b32f835ce25ce5bd32ff8d477ccfa292ffa77ffac437568cb3
4
- data.tar.gz: eedafc5365f8cf312326cf9afaeb05d5d28a1eb10c92fe780472a1b0004859f3
3
+ metadata.gz: a13074eb2d7b2c22ace7a15de282c480769412b37eebb599d914ad1f963b4177
4
+ data.tar.gz: 481a55cb94f2f837b9bb5aa32f2b3ea41b91e67bb7d5ba6d7d423cbd566a2a27
5
5
  SHA512:
6
- metadata.gz: 10a5418ce724f2a5fbf289eee0056a1916fbdf1db0f2a81952525c05484fa6d70dd8a45c5c3cb5f1bd453c2001917341544d589e0f207c061ad16e426afcf2bc
7
- data.tar.gz: 59fb636dfce9eeb21a1b105100b8dac67874119651952aae124eb61afe4b486137f7e450f79b6f3834fa44bd90fc918b515c7a195c9d87967522d3d4bb4a6207
6
+ metadata.gz: d208bce285cfab32f9435c2305fae703ce8afff13d7382166b197c28b299f6d2600ff0d2c504ae5d506aee8278e0f6d41bd5d74094e214baa4e01c01cf398975
7
+ data.tar.gz: 1b441d38d2d51ae804999bf24b19f825bd9fd3b53badd2a11ad601c3dd5591c467f40e967c818919634773929099eefb6cf04db523b14ff7352d64b8684d370a
@@ -1,6 +1,7 @@
1
1
  require 'thor'
2
2
  require 'missing_translation/configuration'
3
3
  require 'missing_translation/translation_file'
4
+ require 'missing_translation/util'
4
5
  require 'missing_translation_api'
5
6
 
6
7
  module MissingTranslation
@@ -35,6 +36,9 @@ module MissingTranslation
35
36
 
36
37
  desc "download", "Download all file from missing translation"
37
38
  def download
39
+ MissingTranslation::Cli.new().sort
40
+ return if MissingTranslation::Util.uncommitted_changes?
41
+
38
42
  directory = "./config/locales"
39
43
  configuration = Configuration.new
40
44
  api = MissingTranslationApi.new(configuration.config[:key])
@@ -48,6 +52,15 @@ module MissingTranslation
48
52
  end
49
53
  end
50
54
 
55
+ desc "sort", "Sort all yaml translations"
56
+ def sort
57
+ return if MissingTranslation::Util.uncommitted_changes?
58
+
59
+ config_folder = "./config/locales/*.yml"
60
+ MissingTranslation::Util.sort(config_folder)
61
+ MissingTranslation::Util.commit_all_changes("locale: Sort yml files")
62
+ end
63
+
51
64
  end
52
65
 
53
66
  end
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'psych'
3
+ require 'missing_translation/translation_file'
3
4
 
4
5
  module MissingTranslation
5
6
  class TranslationFile
@@ -41,24 +42,11 @@ module MissingTranslation
41
42
 
42
43
  def write(directory)
43
44
  return nil unless translations
44
-
45
- result = Psych.parse_stream translations.to_yaml
46
- result.grep(Psych::Nodes::Scalar).each do |node|
47
- node.plain = false
48
- node.quoted = true
49
- node.style = Psych::Nodes::Scalar::DOUBLE_QUOTED
50
- end
51
- result.grep(Psych::Nodes::Mapping).each do |node|
52
- node.children.each_slice(2) do |k, _|
53
- k.plain = true
54
- k.quoted = false
55
- k.style = Psych::Nodes::Scalar::ANY
56
- end
57
- end
58
45
  filename = [group, language, "yml"].compact.reject{|s| s.size <= 0}.join('.')
59
-
60
- p "Writing file #{directory}/#{filename}..."
61
- File.write("#{directory}/#{filename}", result.yaml)
46
+ filepath = "#{directory}/#{filename}"
47
+ p "Writing file #{filepath}..."
48
+ File.write(filepath, translations.to_yaml)
49
+ MissingTranslation::Util.sort_file(filepath)
62
50
  end
63
51
 
64
52
  end
@@ -0,0 +1,42 @@
1
+ require 'yaml'
2
+ require 'missing_translation/yaml_processor'
3
+
4
+ module MissingTranslation
5
+ class Util
6
+
7
+ def self.uncommitted_changes?
8
+ status = `git status --porcelain`
9
+ return false if $CHILD_STATUS.success? && status.empty?
10
+
11
+ error = if $CHILD_STATUS.success?
12
+ "You have uncommitted code. Please commit or stash your changes before continuing"
13
+ else
14
+ "You do not have Git installed. Please install Git, and commit your changes before continuing"
15
+ end
16
+ p error
17
+ true
18
+ end
19
+
20
+ def self.commit_all_changes(message)
21
+ status = `git add .`
22
+ status = system "git commit -m \"#{message}\""
23
+ end
24
+
25
+ def self.sort_file(filepath)
26
+ content = YAML.load_file(filepath)
27
+ hash = MissingTranslation::YamlProcessor.denormalize_translations_hash(content)
28
+ translations = MissingTranslation::YamlProcessor.normalize_translations_hash(hash)
29
+
30
+ File.write(filepath, translations.to_yaml)
31
+ end
32
+
33
+ def self.sort(file_pattern = "./config/locales/#{file_extension}/*.yml")
34
+ file_pathnames = Dir[file_pattern]
35
+ return unless file_pathnames && file_pathnames.size > 0
36
+
37
+ file_pathnames.each do |filepath|
38
+ sort_file(filepath)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MissingTranslation
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -0,0 +1,38 @@
1
+ require 'yaml'
2
+ module MissingTranslation
3
+ class YamlProcessor
4
+ def self.denormalize_translations_hash(translations, parent_key='')
5
+ ans = {}
6
+ translations.each do |key, val|
7
+ if val.is_a? Hash
8
+ aux = denormalize_translations_hash(val, "#{parent_key}#{key}.")
9
+ aux.each do |k, v|
10
+ ans[k] = v
11
+ end
12
+ else
13
+ ans["#{parent_key}#{key}"] = val
14
+ end
15
+ end
16
+ ans
17
+ end
18
+
19
+ def self.dig_set(obj, keys, value)
20
+ key = keys.first
21
+ if keys.length == 1
22
+ obj[key] = value
23
+ else
24
+ obj[key] = {} unless obj[key]
25
+ dig_set(obj[key], keys.slice(1..-1), value)
26
+ end
27
+ end
28
+
29
+ def self.normalize_translations_hash(hash)
30
+ result = {}
31
+ hash.keys.sort.each do |key|
32
+ dig_set(result, key.split("."), hash[key])
33
+ end
34
+ result
35
+ end
36
+ end
37
+ end
38
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: missing_translation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Clercin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-08 00:00:00.000000000 Z
11
+ date: 2023-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -74,7 +74,9 @@ files:
74
74
  - lib/missing_translation/cli.rb
75
75
  - lib/missing_translation/configuration.rb
76
76
  - lib/missing_translation/translation_file.rb
77
+ - lib/missing_translation/util.rb
77
78
  - lib/missing_translation/version.rb
79
+ - lib/missing_translation/yaml_processor.rb
78
80
  - lib/missing_translation_api.rb
79
81
  - sig/missing_translation.rbs
80
82
  homepage: https://github.com/9troisquarts/missing_translation