Skip to content

Commit 33f38e7

Browse files
justin808claude
andcommitted
Refine locale generation improvements
Changes: - Fix CHANGELOG PR reference (2091 → 2092) - Add safety guard for empty locale_files array to prevent nil comparison errors - Improve ENV variable parsing to accept force=true|1|yes (more flexible) - Remove unnecessary sleep in test (timestamp difference already guaranteed) - Add RBS type signatures for Locales module and classes All tests pass and RuboCop clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b8e3ba0 commit 33f38e7

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Changes since the last non-beta release.
2525

2626
#### Improved
2727

28-
- **Idempotent Locale Generation**: The `react_on_rails:locale` rake task is now idempotent, automatically skipping generation when locale files are already up-to-date. This makes it safe to call multiple times (e.g., in Shakapacker's `precompile_hook`) without duplicate work. Added `force=true` option to override timestamp checking. [PR 2091](https://github.com/shakacode/react_on_rails/pull/2091) by [justin808](https://github.com/justin808).
28+
- **Idempotent Locale Generation**: The `react_on_rails:locale` rake task is now idempotent, automatically skipping generation when locale files are already up-to-date. This makes it safe to call multiple times (e.g., in Shakapacker's `precompile_hook`) without duplicate work. Added `force=true` option to override timestamp checking. [PR 2092](https://github.com/shakacode/react_on_rails/pull/2092) by [justin808](https://github.com/justin808).
2929

3030
### [v16.2.0.beta.12] - 2025-11-20
3131

lib/react_on_rails/locales/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def exist_files
6464
end
6565

6666
def files_are_outdated
67+
return true if locale_files.empty?
68+
6769
latest_yml = locale_files.map { |file| File.mtime(file) }.max
6870
earliest = exist_files.map { |file| File.mtime(file) }.min
6971
latest_yml > earliest

lib/tasks/locale.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace :react_on_rails do
1515
Example: rake react_on_rails:locale force=true
1616
DESC
1717
task locale: :environment do
18-
force = ENV["force"] == "true"
18+
force = %w[true 1 yes].include?(ENV["force"]&.downcase)
1919
ReactOnRails::Locales.compile(force: force)
2020
end
2121
end

sig/react_on_rails/locales.rbs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module ReactOnRails
2+
module Locales
3+
def self.compile: (?force: bool) -> (ToJs | ToJson)
4+
def self.check_config_directory_exists: (directory: String?, key_name: String, remove_if: String) -> void
5+
6+
class Base
7+
def initialize: (?force: bool) -> void
8+
9+
private
10+
11+
def file_format: () -> String?
12+
def obsolete?: () -> bool
13+
def exist_files: () -> Array[String]
14+
def files_are_outdated: () -> bool
15+
def file_names: () -> Array[String]
16+
def files: () -> Array[String]
17+
def file: (String name) -> String
18+
def locale_files: () -> Array[String]
19+
def i18n_dir: () -> String?
20+
def i18n_yml_dir: () -> String?
21+
def default_locale: () -> String
22+
def convert: () -> void
23+
def generate_file: (String template, String path) -> void
24+
def generate_translations: () -> [String, String]
25+
def format: (untyped input) -> Symbol
26+
def flatten_defaults: (Hash[untyped, untyped] val) -> Hash[Symbol, Hash[Symbol, untyped]]
27+
def flatten: (Hash[untyped, untyped] translations) -> Hash[Symbol, untyped]
28+
def template_translations: () -> String
29+
def template_default: () -> String
30+
end
31+
32+
class ToJs < Base
33+
private
34+
35+
def file_format: () -> String
36+
end
37+
38+
class ToJson < Base
39+
private
40+
41+
def file_format: () -> String
42+
def template_translations: () -> String
43+
def template_default: () -> String
44+
end
45+
end
46+
end

spec/react_on_rails/locales_to_js_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ module ReactOnRails
5656
initial_mtime = File.mtime(translations_path)
5757

5858
# Touch files to make them newer than YAML (up-to-date)
59-
sleep 0.01 # Ensure timestamp difference
6059
future_time = Time.current + 1.minute
6160
FileUtils.touch(translations_path, mtime: future_time)
6261
FileUtils.touch(default_path, mtime: future_time)

0 commit comments

Comments
 (0)