missing_translation 0.2.0 → 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: 4c45b827695eff0dec6447bd5eb42a2e52a7796fd55c1ad70d65825a536a3ba0
4
- data.tar.gz: b18c56a5647d1b68939e5a542f461314aaf35fabe214fba9149e03bf965957ab
3
+ metadata.gz: a13074eb2d7b2c22ace7a15de282c480769412b37eebb599d914ad1f963b4177
4
+ data.tar.gz: 481a55cb94f2f837b9bb5aa32f2b3ea41b91e67bb7d5ba6d7d423cbd566a2a27
5
5
  SHA512:
6
- metadata.gz: bc298f0e829a78ae4f3df5fc38282e07e0a508e96b6f294594bea671e49923efbcaeef3623e022d11ba250304c6a15033e3cbca08741aca8e9fe4440734eb02a
7
- data.tar.gz: 755e0c279bb3b7ab8062c3fd687f7d023e78dd77084ece3285fbed59fe885f7c0857dcaf8b9c4a1f64626fe7f18324f693b0aa5356a8a06d4243f81bee676616
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
@@ -17,8 +18,11 @@ module MissingTranslation
17
18
 
18
19
  def to_json
19
20
  return nil unless @content
20
-
21
- @content.to_hash
21
+
22
+ hash_content = @content.to_hash
23
+ return hash_content[language] if hash_content.has_key?(language)
24
+
25
+ hash_content
22
26
  end
23
27
 
24
28
  def group
@@ -38,24 +42,11 @@ module MissingTranslation
38
42
 
39
43
  def write(directory)
40
44
  return nil unless translations
41
-
42
- result = Psych.parse_stream translations.to_yaml
43
- result.grep(Psych::Nodes::Scalar).each do |node|
44
- node.plain = false
45
- node.quoted = true
46
- node.style = Psych::Nodes::Scalar::DOUBLE_QUOTED
47
- end
48
- result.grep(Psych::Nodes::Mapping).each do |node|
49
- node.children.each_slice(2) do |k, _|
50
- k.plain = true
51
- k.quoted = false
52
- k.style = Psych::Nodes::Scalar::ANY
53
- end
54
- end
55
45
  filename = [group, language, "yml"].compact.reject{|s| s.size <= 0}.join('.')
56
-
57
- p "Writing file #{directory}/#{filename}..."
58
- 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)
59
50
  end
60
51
 
61
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.0"
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.0
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-10-31 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