locale_to_js 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f9ba0e471d8f5e710855f56406ffefcbc3770a6a5514875b420307c93bc084c3
4
+ data.tar.gz: 4985af9df6384a823adcccd040f807158c33ab7fc9a739f2c044e1a4f0d181eb
5
+ SHA512:
6
+ metadata.gz: 7047798b50bceecf0434308b1cc048040e17f20e9d8c819ebe377f3682f485c549c74a0f1921bca2e785efd1bf831161da61c9820f8a84a62cd8c91200b31708
7
+ data.tar.gz: faecab527c68d5fa0efa911cfaab4a957b785c30e6df87b4a86d2601dacbe9dbb685492192247b0e78ea7b3bf3fcdaedc530681b1e667c3892d68969feae56c4
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/ClassLength
4
+
5
+ module LocaleToJs
6
+ def self.configure
7
+ yield(configuration)
8
+ configuration.setup_config_values
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new(
13
+ i18n_output_format: nil
14
+ )
15
+ end
16
+
17
+ class Configuration
18
+ attr_accessor :i18n_dir, :i18n_yml_dir, :i18n_output_format, :i18n_yml_safe_load_options
19
+
20
+ # rubocop:disable Metrics/AbcSize
21
+ def initialize(i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil)
22
+ self.i18n_dir = i18n_dir
23
+ self.i18n_yml_dir = i18n_yml_dir
24
+ self.i18n_output_format = i18n_output_format
25
+ self.i18n_yml_safe_load_options = i18n_yml_safe_load_options
26
+ end
27
+
28
+ # on ReactOnRails
29
+ def setup_config_values
30
+ end
31
+ end
32
+ end
33
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LocaleToJs
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+ require 'fileutils'
5
+
6
+ module LocaleToJs
7
+ module Locales
8
+ def self.compile
9
+ config = LocaleToJs.configuration
10
+ if config&.i18n_output_format&.downcase == "json"
11
+ LocaleToJs::Locales::ToJson.new
12
+ else
13
+ LocaleToJs::Locales::ToJs.new
14
+ end
15
+ end
16
+
17
+ def self.check_config_directory_exists(directory:, key_name:, remove_if:)
18
+ return if directory.nil?
19
+ return if Dir.exist?(directory)
20
+
21
+ msg = <<~MSG
22
+ Error configuring /config/initializers/locale_to_js.rb: invalid value for `#{key_name}`.
23
+ Directory does not exist: #{directory}. Set to value to nil or comment it
24
+ out if #{remove_if}.
25
+ MSG
26
+ raise LocaleToJs::Error, msg
27
+ end
28
+
29
+ private_class_method :check_config_directory_exists
30
+
31
+ class Base
32
+ def initialize
33
+ return if i18n_dir.nil?
34
+ return unless obsolete?
35
+
36
+ @translations, @defaults = generate_translations
37
+ convert
38
+ end
39
+
40
+ private
41
+
42
+ def file_format; end
43
+
44
+ def obsolete?
45
+ return true if exist_files.empty?
46
+
47
+ files_are_outdated
48
+ end
49
+
50
+ def exist_files
51
+ @exist_files ||= files.select { |file| File.exist?(file) }
52
+ end
53
+
54
+ def files_are_outdated
55
+ latest_yml = locale_files.map { |file| File.mtime(file) }.max
56
+ earliest = exist_files.map { |file| File.mtime(file) }.min
57
+ latest_yml > earliest
58
+ end
59
+
60
+ def file_names
61
+ %w[translations default]
62
+ end
63
+
64
+ def files
65
+ @files ||= file_names.map { |n| file(n) }
66
+ end
67
+
68
+ def file(name)
69
+ "#{i18n_dir}/#{name}.#{file_format}"
70
+ end
71
+
72
+ def locale_files
73
+ @locale_files ||= if i18n_yml_dir.present?
74
+ Dir["#{i18n_yml_dir}/**/*.yml"]
75
+ else
76
+ LocaleToJs::Utils.truthy_presence(
77
+ Rails.application && Rails.application.config.i18n.load_path
78
+ ).presence
79
+ end
80
+ end
81
+
82
+ def i18n_dir
83
+ return Rails.root.join("app/javascript/lang") unless LocaleToJs.configuration&.i18n_dir&.present?
84
+
85
+ @i18n_dir ||= LocaleToJs.configuration.i18n_dir
86
+ end
87
+
88
+ def i18n_yml_dir
89
+ return Rails.root.join("config/locales") unless LocaleToJs.configuration&.i18n_yml_dir&.present?
90
+
91
+ @i18n_yml_dir ||= LocaleToJs.configuration.i18n_yml_dir
92
+ end
93
+
94
+ def default_locale
95
+ @default_locale ||= I18n.default_locale.to_s || "en"
96
+ end
97
+
98
+ def convert
99
+ file_names.each do |name|
100
+ template = send(:"template_#{name}")
101
+ path = file(name)
102
+ generate_file(template, path)
103
+ end
104
+ end
105
+
106
+ def generate_file(template, path)
107
+ result = ERB.new(template).result
108
+ FileUtils.mkdir_p(File.dirname(path))
109
+ File.write(path, result)
110
+ end
111
+
112
+ def generate_translations
113
+ translations = {}
114
+ defaults = {}
115
+ locale_files.each do |f|
116
+ safe_load_options = LocaleToJs.configuration&.i18n_yml_safe_load_options || {}
117
+ translation = YAML.safe_load(File.open(f), **safe_load_options)
118
+ key = translation.keys[0]
119
+ val = flatten(translation[key])
120
+ translations = translations.deep_merge(key => val)
121
+ defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale
122
+ rescue Psych::Exception => e
123
+ raise LocaleToJs::Error, <<~MSG
124
+ Error parsing #{f}: #{e.message}
125
+ Consider fixing unsafe YAML or permitting with config.i18n_yml_safe_load_options
126
+ MSG
127
+ end
128
+ [translations.to_json, defaults.to_json]
129
+ end
130
+
131
+ def format(input)
132
+ input.to_s.tr(".", "_").camelize(:lower).to_sym
133
+ end
134
+
135
+ def flatten_defaults(val)
136
+ flatten(val).each_with_object({}) do |(k, v), h|
137
+ key = format(k)
138
+ h[key] = { id: k, defaultMessage: v }
139
+ end
140
+ end
141
+
142
+ def flatten(translations)
143
+ translations.each_with_object({}) do |(k, v), h|
144
+ if v.is_a? Hash
145
+ flatten(v).map { |hk, hv| h[:"#{k}.#{hk}"] = hv }
146
+ elsif v.is_a?(String)
147
+ h[k] = v.gsub("%{", "{")
148
+ elsif !v.is_a?(Array)
149
+ h[k] = v
150
+ end
151
+ end
152
+ end
153
+
154
+ def template_translations
155
+ <<-JS.strip_heredoc
156
+ export const translations = #{@translations};
157
+ JS
158
+ end
159
+
160
+ def template_default
161
+ <<-JS.strip_heredoc
162
+ import { defineMessages } from 'react-intl';
163
+
164
+ const defaultLocale = '#{default_locale}';
165
+
166
+ const defaultMessages = defineMessages(#{@defaults});
167
+
168
+ export { defaultMessages, defaultLocale };
169
+ JS
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module LocaleToJs
6
+ module Locales
7
+ class ToJs < Base
8
+ private
9
+
10
+ def file_format
11
+ "js"
12
+ end
13
+
14
+ def template_translations
15
+ <<-JS.strip_heredoc
16
+ export const translations = #{@translations};
17
+ JS
18
+ end
19
+
20
+ def template_default
21
+ <<-JS.strip_heredoc
22
+ import { defineMessages } from 'react-intl';
23
+
24
+ const defaultLocale = '#{default_locale}';
25
+
26
+ const defaultMessages = defineMessages(#{@defaults});
27
+
28
+ export { defaultMessages, defaultLocale };
29
+ JS
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module LocaleToJs
6
+ module Locales
7
+ class ToJson < Base
8
+ private
9
+
10
+ def file_format
11
+ "json"
12
+ end
13
+
14
+ def template_translations
15
+ @translations
16
+ end
17
+
18
+ def template_default
19
+ @defaults
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LocaleToJs
4
+ module Utils
5
+
6
+ # https://forum.shakacode.com/t/yak-of-the-week-ruby-2-4-pathname-empty-changed-to-look-at-file-size/901
7
+ # return object if truthy, else return nil
8
+ def self.truthy_presence(obj)
9
+ if obj.nil? || obj == false
10
+ nil
11
+ else
12
+ obj
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LocaleToJs
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,12 @@
1
+ require "locale_to_js/utils"
2
+ require 'locale_to_js/version'
3
+ require 'locale_to_js/configuration'
4
+ require "locale_to_js/locales/base"
5
+ require "locale_to_js/locales/to_js"
6
+ require "locale_to_js/locales/to_json"
7
+
8
+ # Charger les tâches seulement si Rake est défini
9
+ if defined?(Rake)
10
+ rakefile = File.expand_path('../tasks/locale.rake', __FILE__)
11
+ load rakefile if File.exist?(rakefile)
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "locale_to_js/locales/base"
4
+ require "locale_to_js/locales/to_js"
5
+ require "locale_to_js/locales/to_json"
6
+
7
+ namespace :locale do
8
+ desc "Compile les fichiers de locales en fichiers JS"
9
+ task :js, [] => [] do |_t, args|
10
+ LocaleToJs::Locales.compile
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale_to_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Clercin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-02-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A fork of the react-on-rails rake tasks to transform your Rails locales
14
+ to JS
15
+ email: 'kevin@9troisquarts.com '
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/locale_to_js.rb
21
+ - lib/locale_to_js/configuration.rb
22
+ - lib/locale_to_js/error.rb
23
+ - lib/locale_to_js/locales/base.rb
24
+ - lib/locale_to_js/locales/to_js.rb
25
+ - lib/locale_to_js/locales/to_json.rb
26
+ - lib/locale_to_js/utils.rb
27
+ - lib/locale_to_js/version.rb
28
+ - lib/tasks/locale.rake
29
+ homepage: https://github.com/kclercin/locale-to-js
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.5.22
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Transform your Rails locales to JS
52
+ test_files: []