Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* application_controller.rb: Adjust set_locale logic Adds a :locale parameter (which should come from the URL, e.g.: `refugerestrooms.org/?locale=en` or something like that.) * application_controller.rb: set up default_url_options Allows URLs auto-generated by Rails to automatically be expanded to include "?locale=xyz" or "&locale=xyz" See: https://guides.rubyonrails.org/v5.2.4/i18n.html#setting-the-locale-from-url-params For example, this affects URLs made with ActionView URL helpers ('button_to', 'link_to', etc.) See: https://api.rubyonrails.org/v5.2.4/classes/ActionView/Helpers/UrlHelper.html (Hard-coded URLS will generally not get the parameter added.) * header: Use link_to helper for homepage link Use a link_to helper to dynamically create the homepage link, because URL helpers now automatically include the 'locale=' parameter. * header: Un-hard-code the api docs path Use the 'api_docs_path' helper, rather than hard-coding '/api/docs'. (This is for the navigation header, under the "Resources" drop-down.) * config/routes.rb: Add (optional) locale scope to all pages Adds an optional locale prefix for almost every page in the app. See: https://guides.rubyonrails.org/v5.2.4/i18n.html#setting-the-locale-from-url-params You can now visit e.g. '/es/restrooms/new' and you will see the "Submit a New Restroom" page in Spanish. All links in the app auto-generate this locale prefix, so users can specify a locale via visiting a specific locale prefix, and the links in the app will not misdirect them into another locale. (You can still visit URLs without a locale prefix, like '/restrooms/new', and the app will simply auto-detect the locale based on the preferred languages in your browser settings.) This preserves the existing URLs in working order, and any links out there on the web, or in people's bookmarks, will still work.) The home page does not always get the prefix: - The home page link in nav is just '/?locale=[I18n.locale]' - You can visit '/?locale=es' to see the homepage in Spanish. - The homepage link in the nav are auto-generated as '/?locale=[I18n.locale]'; This is equivalent in functionality to the locale prefixes (e.g. '/es/'). - The homepage links in the footer are auto-generated as '/[I18n.locale]/'. - You can visit '/es/' to see the homepage in Spanish. * _footer.html.haml: Add locale switcher links Adds locale switcher links in the footer, which displays at the bottom of every page. These links dynamically link to the current page the user is viewing, but with the current locale overridden with a specific, new locale. Uses the Rails API's "ActionDispatch::Request" feature to get a string containing the last requested page, (i.e. the page the user is currently viewing). This includes query parameters, such as "?lat=[num]&long=[num]" See: https://api.rubyonrails.org/v5.2.4/classes/ActionDispatch/Request.html#method-i-GET (Parameters can also be derived from the URL, such as the "es/" in "/es/restrooms/new", if routed properly.) See: https://guides.rubyonrails.org/routing.html See these StackOverflow answers/this Wikipedia article for details: - https://stackoverflow.com/questions/3762430/rails-preserving-get-query-string-parameters-in-link-to - https://stackoverflow.com/questions/6885990/rails-params-explained - https://en.wikipedia.org/wiki/Query_string * CSS: Better styling of the locale-switcher links Spaced out the links a bit, and added bullet-point separators. * spec: Fix a path construction, test passes now Passing the whole array of restroom data was causing the whole array to be erroneously interpreted as if it was the :locale prefix in the path. Explicitly pass :id, and only :id, to the `restroom_path` route helper for this test. (We only need the :id from the newly-constructed test restroom in order to visit the correct path. Other restroom data isn't needed here anyhow.)
- Loading branch information