diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index deaea8e2da..a45b3e1869 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -61,7 +61,7 @@ Metrics/BlockNesting: # Offense count: 26 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 299 + Max: 315 # Offense count: 59 # Configuration parameters: AllowedMethods, AllowedPatterns. diff --git a/Vendorfile b/Vendorfile index 4c6a80839d..c98857db4b 100644 --- a/Vendorfile +++ b/Vendorfile @@ -45,4 +45,12 @@ folder 'vendor/assets' do folder 'polyfill' do file 'es6.js', 'https://polyfill.io/v3/polyfill.js?features=es6&flags=gated,always' end + + folder 'jquery.uls' do + from 'https://github.com/wikimedia/jquery.uls.git' do + folder 'css' + folder 'src' + folder 'images' + end + end end diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 0bfff869eb..822dc9836c 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -18,6 +18,7 @@ //= require qs/dist/qs //= require bs-custom-file-input //= require bs-custom-file-input-init +//= require uls /* * Called as the user scrolls/zooms around to manipulate hrefs of the diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index 2e33438dbd..e9d9eb3316 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -21,6 +21,7 @@ OSM = { FOSSGIS_OSRM_URL: <%= Settings.fossgis_osrm_url.to_json %>, FOSSGIS_VALHALLA_URL: <%= Settings.fossgis_valhalla_url.to_json %>, DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>, + AVAILABLE_LOCALES: <%= I18n.available_locales.map(&:to_s).to_json %>, <% if Settings.key?(:thunderforest_key) %> THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>, diff --git a/app/assets/javascripts/uls.js b/app/assets/javascripts/uls.js new file mode 100644 index 0000000000..93cc627177 --- /dev/null +++ b/app/assets/javascripts/uls.js @@ -0,0 +1,30 @@ +//= require src/jquery.uls.data +//= require src/jquery.uls.data.utils +//= require src/jquery.uls.lcd +//= require src/jquery.uls.languagefilter +//= require src/jquery.uls.core + +$(document).ready(function () { + function updateLanguage(language) { + Cookies.set("_osm_locale", language, { secure: true, path: "/", samesite: "lax" }); + + document.location.reload(); + } + + var languages = $.uls.data.getAutonyms(); + + for (var code in languages) { + if (!OSM.AVAILABLE_LOCALES.includes(code)) { + delete languages[code]; + } + } + + $(".uls-trigger").uls({ + onSelect: updateLanguage, + languages: languages + }); + + var application_data = $("head").data(); + + $(".uls-trigger").text(Cookies.get("_osm_locale") || application_data.locale); +}); diff --git a/app/assets/stylesheets/screen-ltr.scss b/app/assets/stylesheets/screen-ltr.scss index c525060af5..9bb84bad01 100644 --- a/app/assets/stylesheets/screen-ltr.scss +++ b/app/assets/stylesheets/screen-ltr.scss @@ -1 +1,2 @@ @import "common"; +@import "uls"; diff --git a/app/assets/stylesheets/screen-rtl.rtlcss.scss b/app/assets/stylesheets/screen-rtl.rtlcss.scss index c525060af5..9bb84bad01 100644 --- a/app/assets/stylesheets/screen-rtl.rtlcss.scss +++ b/app/assets/stylesheets/screen-rtl.rtlcss.scss @@ -1 +1,2 @@ @import "common"; +@import "uls"; diff --git a/app/assets/stylesheets/uls.scss b/app/assets/stylesheets/uls.scss new file mode 100644 index 0000000000..54e49036e9 --- /dev/null +++ b/app/assets/stylesheets/uls.scss @@ -0,0 +1,42 @@ +/* + *= require css/jquery.uls + *= require css/jquery.uls.grid + *= require css/jquery.uls.lcd + */ + +.uls-menu { + z-index: 100000; + + h1 { + margin: 16px 0px; + } + + .row { + display: block; + + * { + width: auto; + max-width: none; + } + + li { + width: max-content; + } + } +} + +.uls-trigger { + cursor: pointer; + background-image: image-url('images/language.png'); + background-image: linear-gradient(transparent, transparent), image-url('images/language.svg'); +} + +.uls-icon-close { + background-image: image-url('images/close.png'); + background-image: linear-gradient(transparent, transparent), image-url('images/close.svg'); +} + +.uls-menu .uls-search-label { + background-image: image-rl('images/search.png'); + background-image: linear-gradient(transparent, transparent), image-url('images/search.svg'); +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c830d4bcd3..a25b0a3c91 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -175,6 +175,8 @@ def report_error(message, status = :bad_request) def preferred_languages @preferred_languages ||= if params[:locale] Locale.list(params[:locale]) + elsif request.cookies["_osm_locale"] + Locale.list(request.cookies["_osm_locale"]) elsif current_user current_user.preferred_languages else diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 961be40246..b6d601c573 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -179,7 +179,11 @@ def save current_user.data_public = true current_user.description = "" if current_user.description.nil? current_user.creation_ip = 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 e9ded0f75c..773c0c9a6f 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -41,6 +41,7 @@ <% end -%> <% end %> +