awesome_translations 0.0.66 → 0.0.67

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: d460ee15bd42bacf6aa5ec1adb1438e8cffb81e929ee20a7dbcdec73d4dd2c43
4
- data.tar.gz: 3f53538bf471e18e31abbf5475d799b0f786cd4e7aa53322a29d8c69cdf186c6
3
+ metadata.gz: 4033166fa0b00db136066ec3ec9c798b308fbb1c6e482dc5772d04c00c4288ff
4
+ data.tar.gz: 358aa1cdebc81ae4c702786fbe446e052a0aa74e8d5dcfbe3c286f8cc32db8fe
5
5
  SHA512:
6
- metadata.gz: 2da3bfe416adf369ed0b665231a0228cc9005ce8471478e95c1fb1a16cbc7da842b7d62f3c459a4fd349ad6a8dbe6cd1a5c2a19985682f20e98ee38f1b421fa3
7
- data.tar.gz: 0a4cf304c668d621b91b448c10e5624ed5cb02c29209f83df648cd8162dcefc12b8633e6d271ddc444a667a0d916a7d9c86a0ffb57102a0c5480cb4ec3d99b08
6
+ metadata.gz: 18ed1ea722ffced49ac7629f534ca1f05ec4046527424e5e9617c15ce01f371068e5728bf5fb969070960a799f2f97d4b23fb8324bb32b826cfc5415a46214ef
7
+ data.tar.gz: 8624f507e834e4089fcf380102335b0c85a39c13fa93f284fca25300756a3cbdb3613af85a1026702802086ff9a3875b8b7d0bd74064f73647fff88b9789cda8
@@ -34,25 +34,67 @@ class AwesomeTranslations::TranslatedValue
34
34
  match[2].to_i
35
35
  end
36
36
 
37
- def save!
38
- dir = File.dirname(@file)
39
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
40
- File.write(@file, "#{@locale}:\n") unless File.exist?(@file)
37
+ def file_extension
38
+ @file_extension ||= File.extname(@file)
39
+ end
40
+
41
+ def save! # rubocop:disable Metrics/AbcSize
42
+ if file_extension == ".yml"
43
+ translations = YAML.safe_load(File.read(file)) if File.exist?(file)
44
+ elsif file_extension == ".json"
45
+ translations = JSON.parse(File.read(file)) if File.exist?(file)
46
+ else
47
+ raise "Unhandled file extension: #{file_extension}"
48
+ end
41
49
 
42
- translations = YAML.safe_load(File.read(@file))
43
50
  translations ||= {}
44
51
  translations[@locale.to_s] ||= {}
45
52
 
46
53
  insert_translation_into_hash(translations)
54
+ number_of_translactions = count_translations(translations)
47
55
 
48
56
  update_models
49
57
 
50
- I18n.load_path << file unless I18n.load_path.include?(file)
51
- File.write(file, YAML.dump(translations))
58
+ if number_of_translactions.positive?
59
+ dir = File.dirname(file)
60
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
61
+
62
+ if file_extension == ".yml"
63
+ File.write(file, YAML.dump(translations))
64
+ elsif file_extension == ".json"
65
+ File.write(file, JSON.pretty_generate(translations))
66
+ else
67
+ raise "Unhandled file extension: #{file_extension}"
68
+ end
69
+
70
+ I18n.load_path << file unless I18n.load_path.include?(file)
71
+ else
72
+ File.unlink(file) if File.exist?(file)
73
+
74
+ I18n.load_path.reject! do |load_path_value|
75
+ load_path_value == file
76
+ end
77
+ end
52
78
  end
53
79
 
54
80
  private
55
81
 
82
+ def count_translations(translations)
83
+ count = 0
84
+
85
+ translations.each_value do |value|
86
+ if value.is_a?(Hash)
87
+ count += count_translations(value)
88
+ elsif value.is_a?(Array)
89
+ count += value.length
90
+ else
91
+ count += 1
92
+ end
93
+ end
94
+
95
+ count
96
+ end
97
+
56
98
  def insert_translation_into_hash(translations)
57
99
  current = translations[@locale.to_s]
58
100
 
@@ -55,7 +55,7 @@ class AwesomeTranslations::Translation
55
55
  next unless value_for?(locale)
56
56
 
57
57
  result << AwesomeTranslations::TranslatedValue.new(
58
- file: "#{dir}/#{locale}.yml",
58
+ file: "#{dir}/#{locale}.#{AwesomeTranslations.config.format}",
59
59
  key: @key,
60
60
  locale: locale,
61
61
  value: value(locale: locale)
@@ -77,7 +77,7 @@ class AwesomeTranslations::Translation
77
77
 
78
78
  def translated_value_for_locale(locale)
79
79
  AwesomeTranslations::TranslatedValue.new(
80
- file: "#{dir}/#{locale}.yml",
80
+ file: "#{dir}/#{locale}.#{AwesomeTranslations.config.format}",
81
81
  key: @key,
82
82
  locale: locale,
83
83
  value: value_for?(locale) ? value(locale: locale) : ""
@@ -29,7 +29,7 @@
29
29
  <%= path_without_root_or_locales moval.file_path %>
30
30
  </td>
31
31
  <td>
32
- <%= path_without_root_or_locales "#{handler_translation.dir}/#{moval.locale}.yml" %>
32
+ <%= path_without_root_or_locales "#{handler_translation.dir}/#{moval.locale}.#{AwesomeTranslations.config.format}" %>
33
33
  </td>
34
34
  </tr>
35
35
  <% end %>
@@ -204,6 +204,8 @@ private
204
204
 
205
205
  if File.directory?(full_path)
206
206
  cache_translations_in_dir(full_path)
207
+ elsif File.extname(full_path) == ".json"
208
+ cache_translations_in_file_from_json(full_path)
207
209
  elsif File.extname(full_path) == ".yml"
208
210
  cache_translations_in_file(full_path)
209
211
  end
@@ -226,6 +228,21 @@ private
226
228
  debug "Done caching translations in #{file_path}"
227
229
  end
228
230
 
231
+ def cache_translations_in_file_from_json(file_path)
232
+ @translation_keys_found ||= {}
233
+
234
+ debug "Cache translations in #{file_path}"
235
+
236
+ i18n_hash = JSON.parse(File.read(file_path))
237
+ debug "Hash: #{i18n_hash}"
238
+
239
+ i18n_hash.each do |locale, translations|
240
+ cache_translations_in_hash(file_path, locale, translations) if locale && translations
241
+ end
242
+
243
+ debug "Done caching translations in #{file_path}"
244
+ end
245
+
229
246
  def cache_translations_in_hash(file_path, locale, i18n_hash, keys = [])
230
247
  i18n_hash.each do |key, value|
231
248
  current_key = keys.clone
@@ -1,7 +1,8 @@
1
1
  class AwesomeTranslations::Config
2
- attr_accessor :ignored_paths, :paths_to_translate
2
+ attr_accessor :format, :ignored_paths, :paths_to_translate
3
3
 
4
4
  def initialize
5
+ @format = "yml"
5
6
  @ignored_paths = [
6
7
  Rails.root.join(".git").to_s,
7
8
  Rails.root.join("config/locales").to_s,
@@ -12,7 +12,7 @@ class AwesomeTranslations::TranslationMigrator
12
12
  @locale = @translation_value.locale
13
13
 
14
14
  @old_path = @translation_value.file_path
15
- @new_path = "#{@handler_translation.dir}/#{@translation_value.locale}.yml" if @handler_translation
15
+ @new_path = "#{@handler_translation.dir}/#{@translation_value.locale}.#{AwesomeTranslations.config.format}" if @handler_translation
16
16
  end
17
17
 
18
18
  def execute
@@ -1,3 +1,3 @@
1
1
  module AwesomeTranslations
2
- VERSION = "0.0.66".freeze
2
+ VERSION = "0.0.67".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.66
4
+ version: 0.0.67
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Stöckel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-14 00:00:00.000000000 Z
11
+ date: 2025-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active-record-transactioner