awesome_translations 0.0.65 → 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: 4a064cb6ccd392d08ccc04527a80a5b5926d7e49597f05560a6a0168d1498d1d
4
- data.tar.gz: 151f20aed7e4142f9446894ce8d5f11ad02e20e3d51df242d7a4b264dc1102cf
3
+ metadata.gz: 4033166fa0b00db136066ec3ec9c798b308fbb1c6e482dc5772d04c00c4288ff
4
+ data.tar.gz: 358aa1cdebc81ae4c702786fbe446e052a0aa74e8d5dcfbe3c286f8cc32db8fe
5
5
  SHA512:
6
- metadata.gz: 16dbaadb429667549c1ab80bd40fe24086ef8bfb47018b4e248dd2e401e4de97a217ebe26a58dfaf4ed29f614b9403516ab79e075140a8b5c8e8b80be2d96db3
7
- data.tar.gz: 7c70d4067de4bad81339467c0e5f270a9146f02fd4b9947f5b07c812f2682b1933f1e768fe8c8707909cd1bcf7fedceb5646e3f072fac43df3ab7e59673f9656
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) : ""
@@ -53,7 +53,7 @@
53
53
  </td>
54
54
  <% I18n.available_locales.each do |locale| %>
55
55
  <td class="translation">
56
- <%= text_field "t", "#{translation.key}[#{locale}]", class: "translation-input", lang: locale, value: translation.translation_value(locale: locale) %>
56
+ <%= text_field "t", "#{translation.key}[#{locale}]", class: "translation-input", lang: locale, name: "t[#{translation.key}][#{locale}]", value: translation.translation_value(locale: locale) %>
57
57
  </td>
58
58
  <% end %>
59
59
  </tr>
@@ -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,
@@ -2,7 +2,7 @@ class AwesomeTranslations::ErbInspector::FileInspector
2
2
  JS_FILE_EXTS = [".cjs", ".coffee", ".coffee.erb", ".es6", ".es6.erb", ".js", ".js.erb", ".jsx", ".mjs"].freeze
3
3
  METHOD_NAMES = %w[t controller_t helper_t].freeze
4
4
  VALID_BEGINNING = '(^|\s+|\(|\{|\[|<%=\s*|I18n\.)'.freeze
5
- VALID_BEGINNING_JS = '(^|\s+|\(|\[|\.)'.freeze
5
+ VALID_BEGINNING_JS = '(^|\s+|\(|\[|\{|\.)'.freeze
6
6
 
7
7
  attr_reader :root_path, :file_path
8
8
 
@@ -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.65".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.65
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-02-25 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