Skip to content

Commit

Permalink
Refactor download and process Rails locale files
Browse files Browse the repository at this point in the history
Adds a new command line task to download the latest Rails locale files.
This task also merges and normalizes the files, allowing us to easily
compare them with the existing translations.

To run the task:

thor locales:normalize_from_rails
  • Loading branch information
pama committed Nov 10, 2024
1 parent 955a47c commit 000fde4
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 14 deletions.
39 changes: 39 additions & 0 deletions locales.thor
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ class Locales < Thor
end
end

desc 'normalize_from_rails', 'Download latest Rails locale files, normalize them, and merge into a single file.'
def normalize_from_rails
curr_dir = File.expand_path(File.dirname(__FILE__))
rails_locale_dir = File.expand_path(File.join(curr_dir, 'rails', 'rails'))
normalized_dir = File.join(rails_locale_dir, 'normalized')

puts "Fetching latest Rails locale files to #{rails_locale_dir}... \n\n"
system <<-BASH
curl -Lo '#{rails_locale_dir}/action_view.yml' https://raw.githubusercontent.com/rails/rails/8-0-stable/actionview/lib/action_view/locale/en.yml
curl -Lo '#{rails_locale_dir}/active_model.yml' https://raw.githubusercontent.com/rails/rails/8-0-stable/activemodel/lib/active_model/locale/en.yml
curl -Lo '#{rails_locale_dir}/active_record.yml' https://raw.githubusercontent.com/rails/rails/8-0-stable/activerecord/lib/active_record/locale/en.yml
curl -Lo '#{rails_locale_dir}/active_support.yml' https://raw.githubusercontent.com/rails/rails/8-0-stable/activesupport/lib/active_support/locale/en.yml
BASH
puts "\nDownload complete.\n\n"

merged_content = {}
Dir.glob("#{rails_locale_dir}/*.yml").each do |filename|
puts "Normalizing #{filename}..."

content = YAML.load_file(filename)
normalized_content = Normalize.deep_sort(content)

output_filename = File.join(normalized_dir, File.basename(filename))
File.write(output_filename, normalized_content.to_yaml(line_width: -1))
puts "Normalized file saved to #{output_filename}"

merged_content.deep_merge!(normalized_content)
end

puts "\nAll downloaded files normalized and saved."

puts "\nNormalizing merged content..."
normalized_merged_content = Normalize.deep_sort(merged_content)
merged_filename = File.join(normalized_dir, 'merged.yml')
File.write(merged_filename, normalized_merged_content.to_yaml(line_width: -1))

puts "Merged file saved to #{merged_filename}."
end

desc 'list', 'List locale names.'
def list
puts CheckLocales.list_locales.join(', ')
Expand Down
52 changes: 52 additions & 0 deletions rails/rails/normalized/action_view.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
en:
datetime:
distance_in_words:
about_x_hours:
one: about 1 hour
other: about %{count} hours
about_x_months:
one: about 1 month
other: about %{count} months
about_x_years:
one: about 1 year
other: about %{count} years
almost_x_years:
one: almost 1 year
other: almost %{count} years
half_a_minute: half a minute
less_than_x_seconds:
one: less than 1 second
other: less than %{count} seconds
less_than_x_minutes:
one: less than a minute
other: less than %{count} minutes
over_x_years:
one: over 1 year
other: over %{count} years
x_seconds:
one: 1 second
other: "%{count} seconds"
x_minutes:
one: 1 minute
other: "%{count} minutes"
x_days:
one: 1 day
other: "%{count} days"
x_months:
one: 1 month
other: "%{count} months"
prompts:
second: Seconds
minute: Minute
hour: Hour
day: Day
month: Month
year: Year
helpers:
select:
prompt: Please select
submit:
create: Create %{model}
submit: Save %{model}
update: Update %{model}
35 changes: 35 additions & 0 deletions rails/rails/normalized/active_model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
en:
errors:
format: "%{attribute} %{message}"
messages:
accepted: must be accepted
blank: can't be blank
confirmation: doesn't match %{attribute}
empty: can't be empty
equal_to: must be equal to %{count}
even: must be even
exclusion: is reserved
greater_than: must be greater than %{count}
greater_than_or_equal_to: must be greater than or equal to %{count}
in: must be in %{count}
inclusion: is not included in the list
invalid: is invalid
less_than: must be less than %{count}
less_than_or_equal_to: must be less than or equal to %{count}
model_invalid: 'Validation failed: %{errors}'
not_a_number: is not a number
not_an_integer: must be an integer
odd: must be odd
other_than: must be other than %{count}
password_too_long: is too long
present: must be blank
too_long:
one: is too long (maximum is 1 character)
other: is too long (maximum is %{count} characters)
too_short:
one: is too short (minimum is 1 character)
other: is too short (minimum is %{count} characters)
wrong_length:
one: is the wrong length (should be 1 character)
other: is the wrong length (should be %{count} characters)
13 changes: 13 additions & 0 deletions rails/rails/normalized/active_record.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
en:
activerecord:
errors:
messages:
record_invalid: 'Validation failed: %{errors}'
restrict_dependent_destroy:
has_one: Cannot delete record because a dependent %{record} exists
has_many: Cannot delete record because dependent %{record} exist
errors:
messages:
required: must exist
taken: has already been taken
120 changes: 120 additions & 0 deletions rails/rails/normalized/active_support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
en:
date:
abbr_day_names:
- Sun
- Mon
- Tue
- Wed
- Thu
- Fri
- Sat
abbr_month_names:
-
- Jan
- Feb
- Mar
- Apr
- May
- Jun
- Jul
- Aug
- Sep
- Oct
- Nov
- Dec
day_names:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
month_names:
-
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December
order:
- year
- month
- day
number:
currency:
format:
delimiter: ","
format: "%u%n"
negative_format: "-%u%n"
precision: 2
separator: "."
significant: false
strip_insignificant_zeros: false
unit: "$"
format:
delimiter: ","
precision: 3
round_mode: default
separator: "."
significant: false
strip_insignificant_zeros: false
human:
decimal_units:
format: "%n %u"
units:
billion: Billion
million: Million
quadrillion: Quadrillion
thousand: Thousand
trillion: Trillion
unit: ''
format:
delimiter: ''
precision: 3
significant: true
strip_insignificant_zeros: true
storage_units:
format: "%n %u"
units:
byte:
one: Byte
other: Bytes
eb: EB
gb: GB
kb: KB
mb: MB
pb: PB
tb: TB
zb: ZB
percentage:
format:
delimiter: ''
format: "%n%"
precision:
format:
delimiter: ''
support:
array:
last_word_connector: ", and "
two_words_connector: " and "
words_connector: ", "
time:
am: am
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
long: "%B %d, %Y %H:%M"
short: "%d %b %H:%M"
pm: pm
Loading

0 comments on commit 000fde4

Please sign in to comment.