From 764ef5d8b67bbd7de1c4f63effaaa48f0917d6b8 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 30 May 2024 18:12:26 +0200 Subject: [PATCH] WIP: i18nification --- Gemfile | 4 ++ Gemfile.lock | 24 +++++++ app/controllers/application_controller.rb | 5 ++ app/views/home/index.html.erb | 24 +++---- app/views/hosts/_host.html.erb | 4 +- app/views/hosts/_sort.html.erb | 4 +- app/views/hosts/kind.html.erb | 2 +- app/views/hosts/show.html.erb | 4 +- app/views/layouts/application.html.erb | 4 +- app/views/repositories/_repository.html.erb | 16 ++--- app/views/repositories/show.html.erb | 27 ++++---- config/application.rb | 1 - config/initializers/i18n.rb | 1 + config/initializers/pagy.rb | 1 + config/locales/en.yml | 39 ++--------- config/locales/fr.yml | 71 +++++++++++++++++++++ 16 files changed, 154 insertions(+), 77 deletions(-) create mode 100644 config/initializers/i18n.rb create mode 100644 config/locales/fr.yml diff --git a/Gemfile b/Gemfile index 78cbbea7..44affa71 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,10 @@ gem 'after_commit_action' gem 'postgresql_cursor' gem 'packageurl-ruby', require: 'package_url' gem 'csv' +gem 'http_accept_language' +gem 'i18n' +gem 'i18n-tasks' +gem 'rails-i18n' group :development do gem "web-console" diff --git a/Gemfile.lock b/Gemfile.lock index 2572edff..a6eb19bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM activesupport (>= 3.0.0) appsignal (3.7.5) rack + ast (2.4.2) autoprefixer-rails (10.4.16.0) execjs (~> 2) base64 (0.2.0) @@ -143,12 +144,24 @@ GEM groupdate (6.4.0) activesupport (>= 6.1) hashdiff (1.1.0) + highline (3.0.1) + http_accept_language (2.1.1) httparty (0.22.0) csv mini_mime (>= 1.0.0) multi_xml (>= 0.5.2) i18n (1.14.5) concurrent-ruby (~> 1.0) + i18n-tasks (1.0.14) + activesupport (>= 4.0.2) + ast (>= 2.1.0) + erubi + highline (>= 2.0.0) + i18n + parser (>= 3.2.2.1) + rails-i18n + rainbow (>= 2.2.2, < 4.0) + terminal-table (>= 1.5.1) io-console (0.7.2) irb (1.13.1) rdoc (>= 4.0.0) @@ -209,6 +222,9 @@ GEM bigdecimal (>= 3.0) packageurl-ruby (0.1.0) pagy (8.4.0) + parser (3.3.2.0) + ast (~> 2.4.1) + racc pg (1.5.6) pg_query (5.1.0) google-protobuf (>= 3.22.3) @@ -262,6 +278,9 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) + rails-i18n (7.0.9) + i18n (>= 0.7, < 2) + railties (>= 6.0.0, < 8) railties (7.1.3.3) actionpack (= 7.1.3.3) activesupport (= 7.1.3.3) @@ -270,6 +289,7 @@ GEM rake (>= 12.2) thor (~> 1.0, >= 1.2.2) zeitwerk (~> 2.6) + rainbow (3.1.1) rake (13.2.1) rdoc (6.7.0) psych (>= 4.0.0) @@ -372,6 +392,9 @@ DEPENDENCIES gitlab google-protobuf groupdate + http_accept_language + i18n + i18n-tasks jbuilder jquery-rails mocha @@ -390,6 +413,7 @@ DEPENDENCIES rack-cors rails (~> 7.1.3) rails-controller-testing + rails-i18n redis rswag-api rswag-ui diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6c676a62..38f9a432 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,14 @@ class ApplicationController < ActionController::Base include Pagy::Backend + before_action :set_locale skip_before_action :verify_authenticity_token after_action lambda { request.session_options[:skip] = true } + + def set_locale + I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) + end end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 39b22feb..8dafbebd 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,16 +1,16 @@
- Statistics + <%= t('.statistics') %>
- Hosts: <%= number_with_delimiter @hosts.length %>
- Repositories: <%= number_with_delimiter @hosts.sum(&:repositories_count) %>
- Owners: <%= number_with_delimiter @hosts.sum(&:owners_count) %>
- Tags: <%= number_with_delimiter Tag.fast_total %>
- Manifests: <%= number_with_delimiter Manifest.fast_total %>
- Dependencies: <%= number_with_delimiter Dependency.fast_total %>
+ <%= Host.model_name.human(count: 2) %>: <%= number_with_delimiter @hosts.length %>
+ <%= Repository.model_name.human(count: 2) %>: <%= number_with_delimiter @hosts.sum(&:repositories_count) %>
+ <%= Owner.model_name.human(count: 2) %>: <%= number_with_delimiter @hosts.sum(&:owners_count) %>
+ <%= Tag.model_name.human(count: 2) %>: <%= number_with_delimiter Tag.fast_total %>
+ <%= Manifest.model_name.human(count: 2) %>: <%= number_with_delimiter Manifest.fast_total %>
+ <%= Dependency.model_name.human(count: 2) %>: <%= number_with_delimiter Dependency.fast_total %>
@@ -19,10 +19,10 @@
- <%= link_to kind, kind_hosts_path(kind), class: 'text-decoration-none' %> + <%= link_to t(kind), kind_hosts_path(kind), class: 'text-decoration-none' %> - <%= pluralize number_with_delimiter(hosts.length), 'instance' %> - - <%= pluralize number_with_delimiter(hosts.sum(&:repositories_count)), 'repository' %> + <%= number_with_delimiter(hosts.length) %> <%= t('virtual.models.instance', count: hosts.length) %> - + <%= number_with_delimiter(hosts.sum(&:repositories_count)) %> <%= Repository.model_name.human(count: hosts.sum(&:repositories_count)) %>
    @@ -30,14 +30,14 @@
  • <%= link_to host.name.downcase, host %> - <%= pluralize number_with_delimiter(host.repositories_count), 'repository' %> + <%= number_with_delimiter(host.repositories_count) %> <%= Repository.model_name.human(count: host.repositories_count) %>
  • <% end %>
<% if hosts.length > 20 %> <% end %>
diff --git a/app/views/hosts/_host.html.erb b/app/views/hosts/_host.html.erb index 26eee785..bf012456 100644 --- a/app/views/hosts/_host.html.erb +++ b/app/views/hosts/_host.html.erb @@ -19,8 +19,8 @@ <% if host.repositories_count.zero? %> Coming soon <% else %> - <%= number_with_delimiter host.repositories_count %> repositories
- <%= number_with_delimiter host.owners_count %> owners + <%= "#{number_with_delimiter host.repositories_count} #{Repository.model_name.human(count: host.repositories_count)}" %>
+ <%= "#{number_with_delimiter host.owners_count} #{Owner.model_name.human(count: host.owners_count)}" %> <% end %>

diff --git a/app/views/hosts/_sort.html.erb b/app/views/hosts/_sort.html.erb index 1db84c3a..7efeb005 100644 --- a/app/views/hosts/_sort.html.erb +++ b/app/views/hosts/_sort.html.erb @@ -1,11 +1,11 @@