From 41cb49e6a084607132ed9818e9139925f924a19b Mon Sep 17 00:00:00 2001 From: nertc Date: Thu, 17 Oct 2024 12:21:08 +0400 Subject: [PATCH] Add locale selector --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/language_selector.js | 4 ++++ app/assets/stylesheets/common.scss | 7 +++++++ app/controllers/application_controller.rb | 2 ++ app/controllers/users_controller.rb | 6 +++++- app/views/layouts/_header.html.erb | 9 +++++++++ app/views/shared/_language_selector.html.erb | 16 ++++++++++++++++ test/system/site_test.rb | 19 +++++++++++++++++++ 8 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/language_selector.js create mode 100644 app/views/shared/_language_selector.html.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 05474212645..f9f94cc0b27 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -16,6 +16,7 @@ //= require matomo //= require richtext //= require qs/dist/qs +//= require language_selector /* * Called as the user scrolls/zooms around to manipulate hrefs of the diff --git a/app/assets/javascripts/language_selector.js b/app/assets/javascripts/language_selector.js new file mode 100644 index 00000000000..eac58e40339 --- /dev/null +++ b/app/assets/javascripts/language_selector.js @@ -0,0 +1,4 @@ +$(document).on("change", ".language-change-trigger", function () { + Cookies.set("_osm_locale", this.value, { secure: true, path: "/", samesite: "lax" }); + document.location.reload(); +}); diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index d551462b220..04c755fc7a6 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -52,6 +52,13 @@ time[title] { color: $blue; } +/* Utility for transparent color */ + +.text-transparent{ + color: transparent !important; + user-select: none; +} + /* Bootstrap contextual table classes overrides in dark mode */ @include color-mode(dark) { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4b36607bb00..8ce2b409b78 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -194,6 +194,8 @@ def preferred_languages Locale.list(params[:locale]) elsif current_user current_user.preferred_languages + elsif request.cookies["_osm_locale"] + Locale.list(request.cookies["_osm_locale"]) else Locale.list(http_accept_language.user_preferred_languages) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fc893c9c319..ea35e8f7b82 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -270,7 +270,11 @@ def save_new_user(email_hmac, referer = nil) current_user.description = "" if current_user.description.nil? current_user.creation_ip = request.remote_ip current_user.creation_address = request.remote_ip - current_user.languages = http_accept_language.user_preferred_languages + current_user.languages = if request.cookies["_osm_locale"] + Locale.list(request.cookies["_osm_locale"]) + else + http_accept_language.user_preferred_languages + end current_user.terms_agreed = Time.now.utc current_user.tou_agreed = Time.now.utc current_user.terms_seen = true diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index e96564b82b8..24537f24d98 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -78,6 +78,15 @@
  • <%= link_to t("layouts.about"), about_path, :class => "dropdown-item" %>
  • + <% if current_user && current_user.id %>