From 58e78acbbad81e936bbd9ad84cb1f193ae4ff75d Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Wed, 23 Apr 2025 07:45:33 +0200 Subject: [PATCH] Added translations to entire app --- app/controllers/application_controller.rb | 12 ++ .../controllers/main_calendar_controller.js | 28 ++-- app/views/companies/_company.html.erb | 94 ++++++------- app/views/companies/_form.html.erb | 26 ++-- app/views/companies/edit.html.erb | 6 +- app/views/companies/index.html.erb | 28 ++-- app/views/companies/new.html.erb | 4 +- app/views/companies/show.html.erb | 6 +- app/views/layouts/application.html.erb | 4 +- app/views/reservations/_form.html.erb | 28 ++-- app/views/reservations/edit.html.erb | 6 +- app/views/reservations/index.html.erb | 32 ++++- app/views/reservations/new.html.erb | 4 +- app/views/teams/_form.html.erb | 10 +- app/views/teams/_team.html.erb | 12 +- app/views/teams/edit.html.erb | 6 +- app/views/teams/index.html.erb | 28 ++-- app/views/teams/new.html.erb | 4 +- app/views/teams/show.html.erb | 6 +- config/application.rb | 5 + config/locales/bs.yml | 126 ++++++++++++++++++ config/locales/en.yml | 107 +++++++++++++++ 22 files changed, 450 insertions(+), 132 deletions(-) create mode 100644 config/locales/bs.yml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f79f7ea..2199c95 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,18 @@ class ApplicationController < ActionController::Base + before_action :set_locale + private + def set_locale + I18n.locale = params[:locale] || session[:locale] || I18n.default_locale + session[:locale] = I18n.locale + end + + # Optional: Make locale persist across requests via URL helpers + def default_url_options + { locale: I18n.locale } + end + def set_company company_id = session.fetch(:company_id, Company.first&.id) session[:company_id] = company_id diff --git a/app/javascript/controllers/main_calendar_controller.js b/app/javascript/controllers/main_calendar_controller.js index 142cf64..475db88 100644 --- a/app/javascript/controllers/main_calendar_controller.js +++ b/app/javascript/controllers/main_calendar_controller.js @@ -9,6 +9,15 @@ export default class extends Controller { document.getElementById('main-calendar').style.height = '100vh'; document.getElementById('main-calendar').style.width = '100vw'; + // Get current locale from html lang attribute + const currentLocale = document.documentElement.lang || 'bs'; + + // Get translations from data attribute + const translations = JSON.parse(document.getElementById('main-calendar').dataset.translations || '{}'); + + // Translation helper + const t = (key) => translations[key] || key; + const calendar = new tui.Calendar(document.getElementById('main-calendar'), { defaultView: 'week', usageStatistics: false, @@ -27,9 +36,6 @@ export default class extends Controller { displayLabel: 'UTC' // Optional: Label for the timezone } ] - // You might need `primaryTimezone: 'UTC'` here as well, - // depending on the exact library version and desired behavior. - // Let's start with just zones. }, // This is important - set the height to 100% height: '100%', @@ -37,7 +43,7 @@ export default class extends Controller { width: '100%', template: { timegridDisplayPrimaryTime({time}) { - return `${time.getHours()} sati`; + return `${time.getHours()} ${t('hours')}`; }, popupDetailLocation(eventObj) { return ''; // Empty location as requested @@ -50,6 +56,12 @@ export default class extends Controller { }, popupDetailBody(eventObj) { return ''; + }, + popupEdit() { + return t('edit'); + }, + popupDelete() { + return t('delete'); } }, calendars: [ @@ -105,7 +117,7 @@ export default class extends Controller { const csrfToken = this.getCsrfToken(); if (!csrfToken) { console.error("CSRF token not found."); - alert("Error: Could not verify request security token."); + alert(t('delete_error')); return false; } @@ -120,10 +132,10 @@ export default class extends Controller { if (response.ok) { calendar.deleteEvent(reservationId, calendarId); - alert('Reservation deleted.'); + alert(t('delete_success')); } else { console.error(`Failed to delete reservation ${reservationId}. Status: ${response.status}`); - let errorMessage = 'Error deleting reservation.'; + let errorMessage = t('delete_error'); try { const errorData = await response.json(); errorMessage += ` Server says: ${errorData.error || JSON.stringify(errorData)}`; @@ -132,7 +144,7 @@ export default class extends Controller { } } catch (error) { console.error("Network error or exception during delete:", error); - alert("Error deleting reservation due to a network or script issue."); + alert(t('network_error')); } return false; // Prevent TUI default delete handling diff --git a/app/views/companies/_company.html.erb b/app/views/companies/_company.html.erb index c1f327f..fdc81fd 100644 --- a/app/views/companies/_company.html.erb +++ b/app/views/companies/_company.html.erb @@ -1,47 +1,51 @@ -
-

- Name: - <%= company.name %> -

- -

- Id number: - <%= company.id_number %> -

- -

- Vat number: - <%= company.vat_number %> -

- -

- Address line one: - <%= company.address_line_one %> -

- -

- Address line two: - <%= company.address_line_two %> -

- -

- Postal code: - <%= company.postal_code %> -

- -

- City: - <%= company.city %> -

- -

- Entity: - <%= company.entity %> -

- -

- Country: - <%= company.country %> -

+
+
+

<%= company.name %>

+
+ + + +
+
+
+
+

+ <%= t('companies.company.id_number') %> + <%= company.id_number %> +

+

+ <%= t('companies.company.vat_number') %> + <%= company.vat_number %> +

+

+ <%= t('companies.company.entity') %> + <%= company.entity %> +

+

+ <%= t('companies.company.country') %> + <%= company.country %> +

+
+
+

+ <%= t('companies.company.address_line_one') %> + <%= company.address_line_one %> +

+ <% if company.address_line_two.present? %> +

+ <%= t('companies.company.address_line_two') %> + <%= company.address_line_two %> +

+ <% end %> +

+ <%= t('companies.company.postal_code') %> + <%= company.postal_code %> +

+

+ <%= t('companies.company.city') %> + <%= company.city %> +

+
+
diff --git a/app/views/companies/_form.html.erb b/app/views/companies/_form.html.erb index 2f0317d..4a37c07 100644 --- a/app/views/companies/_form.html.erb +++ b/app/views/companies/_form.html.erb @@ -1,7 +1,7 @@ <%= form_with(model: company, class: "contents") do |form| %> <% if company.errors.any? %>
-

<%= pluralize(company.errors.count, "error") %> prohibited this company from being saved:

+

<%= t('companies.form.errors.header', count: company.errors.count) %>

    <% company.errors.each do |error| %> @@ -12,51 +12,55 @@ <% end %>
    - <%= form.label :name %> + <%= form.label :name, t('companies.form.name') %> <%= form.text_field :name, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :id_number %> + <%= form.label :id_number, t('companies.form.id_number') %> <%= form.text_field :id_number, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :vat_number %> + <%= form.label :vat_number, t('companies.form.vat_number') %> <%= form.text_field :vat_number, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :address_line_one %> + <%= form.label :address_line_one, t('companies.form.address_line_one') %> <%= form.text_field :address_line_one, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :address_line_two %> + <%= form.label :address_line_two, t('companies.form.address_line_two') %> <%= form.text_field :address_line_two, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :postal_code %> + <%= form.label :postal_code, t('companies.form.postal_code') %> <%= form.text_field :postal_code, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :city %> + <%= form.label :city, t('companies.form.city') %> <%= form.text_field :city, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :entity %> + <%= form.label :entity, t('companies.form.entity') %> <%= form.text_field :entity, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.label :country %> + <%= form.label :country, t('companies.form.country') %> <%= form.text_field :country, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
    - <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> + <% if company.new_record? %> + <%= form.submit t('companies.form.create'), class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> + <% else %> + <%= form.submit t('companies.form.update'), class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> + <% end %>
    <% end %> diff --git a/app/views/companies/edit.html.erb b/app/views/companies/edit.html.erb index ad4c38d..94f3878 100644 --- a/app/views/companies/edit.html.erb +++ b/app/views/companies/edit.html.erb @@ -1,8 +1,8 @@
    -

    Editing company

    +

    <%= t('.title') %>

    <%= render "form", company: @company %> - <%= link_to "Show this company", @company, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> - <%= link_to "Back to companies", companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to t('.show'), @company, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to t('.back'), companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
    diff --git a/app/views/companies/index.html.erb b/app/views/companies/index.html.erb index 82e16c3..b4746db 100644 --- a/app/views/companies/index.html.erb +++ b/app/views/companies/index.html.erb @@ -1,21 +1,27 @@ -
    +
    <% if notice.present? %> -

    <%= notice %>

    +
    +

    <%= notice %>

    +
    <% end %> - <% content_for :title, "Companies" %> + <% content_for :title, t('.title') %> -
    -

    Companies

    - <%= link_to "New company", new_company_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
    +

    <%= t('.title') %>

    + <%= link_to new_company_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white font-medium hover:bg-blue-700 transition-colors duration-200 flex items-center" do %> + + + + <%= t('.new_company') %> + <% end %>
    -
    +
    <% @companies.each do |company| %> - <%= render company %> -

    - <%= link_to "Show this company", company, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> -

    + <%= link_to company, class: "block transition-all duration-200 hover:shadow-lg hover:-translate-y-1" do %> + <%= render company %> + <% end %> <% end %>
    diff --git a/app/views/companies/new.html.erb b/app/views/companies/new.html.erb index 150e084..6391727 100644 --- a/app/views/companies/new.html.erb +++ b/app/views/companies/new.html.erb @@ -1,7 +1,7 @@
    -

    New company

    +

    <%= t('.title') %>

    <%= render "form", company: @company %> - <%= link_to "Back to companies", companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to t('.back'), companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
    diff --git a/app/views/companies/show.html.erb b/app/views/companies/show.html.erb index 1751f75..0768b90 100644 --- a/app/views/companies/show.html.erb +++ b/app/views/companies/show.html.erb @@ -6,10 +6,10 @@ <%= render @company %> - <%= link_to "Edit this company", edit_company_path(@company), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> - <%= link_to "Back to companies", companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to t('.edit'), edit_company_path(@company), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to t('.back'), companies_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
    - <%= button_to "Destroy this company", @company, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> + <%= button_to t('.destroy'), @company, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
    diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ecbddf7..43feb1f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - + - Terminator + <%= content_for?(:title) ? yield(:title) : "Terminator" %> <%= csrf_meta_tags %> <%= csp_meta_tag %> diff --git a/app/views/reservations/_form.html.erb b/app/views/reservations/_form.html.erb index c4c8f86..7e4e573 100644 --- a/app/views/reservations/_form.html.erb +++ b/app/views/reservations/_form.html.erb @@ -6,7 +6,7 @@ }) do |form| %> <% if reservation.errors.any? %>
    -

    <%= pluralize(reservation.errors.count, "error") %> prohibited this reservation from being saved:

    +

    <%= t('reservations.form.errors.header', count: reservation.errors.count) %>

      <% reservation.errors.each do |error| %> @@ -17,10 +17,10 @@ <% end %>
      - <%= form.label :customer %> + <%= form.label :customer, t('reservations.form.customer') %> <%= form.select :customer_composite_key, [], # Start with empty options - { prompt: "Type to search customers..." }, + { prompt: t('reservations.form.search_prompt') }, { selected: (reservation.persisted? && reservation.customer ? reservation.customer.to_param : nil), class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full", @@ -29,7 +29,7 @@
      - <%= form.label :phone_number %> + <%= form.label :phone_number, t('reservations.form.phone_number') %> <%= form.telephone_field :customer_original_phone, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full", data: { customer_search_target: "phoneField" } %> @@ -37,21 +37,21 @@