Fall back to the default locale's route when a locale has no localized routes - #22
Merged
Merged
Conversation
A locale without localized routes (supported, but without Gettext translations) raised CaseClauseError in ~q and ArgumentError in the localized helpers. path_for/url_for also accept any locale form that Localize.validate_locale/1 accepts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before
Localize.Plug.PutLocaleaccepts any supported locale, also one without Gettext translations. In the test app,jais supported, and Gettext hasen,frandde:With
supported_locales: [:"de-CH", ...]and Gettextde, the route keys are:"de-CH", sopath_for(:de, "/users/log-in")andpath_for("de-CH", "/users/log-in")raise too.Problem
~qlink.path_for/2andurl_for/2accept only the exact CLDR locale id atom of a route. The moduledoc showspath_for(@locale, "/users")with a locale from the request, which is aLanguageTag, and that raises.After
A locale without routes gets the route of the default locale. If the default locale has no routes either, an
ArgumentErrornames both locales and the locales that have routes.Details
Cause.
Localize.VerifiedRoutes.sigil_q/2,path_for/2andurl_for/2generatecase <locale id> dowith one clause per Gettext locale and no other clause. The public functions inLocalize.Routes.LocalizedHelperspassLocalize.get_locale()tohelper/7, which has clauses only for the same locales.Design. A new
Localize.Routes.route_locale/2returns the locale whose routes to use.~q,path_for,url_forand the localized helpers call it.LanguageTagwhosecldr_locale_idhas routes is returned as is. This is the usual case for~q, which runs once per link, so it costs one list check.Localize.validate_locale/1. This accepts atoms, strings and tags, and maps:deto:"de-CH"whende-CHis the supported locale. An invalid locale raises the validation error, because that is a caller bug.Localize.default_locale(). The path as written in~qis not a safe fallback:localizeregisters only translated paths, so that path may have no route.Helpers for routes localized for only some locales (for example
localize "fr" do ... end) raise as before for the other locales.The three macros now share one private
locale_case/4, which removes the duplicated case generation.Tests.
sigil_q_test.exs,path_for_test.exsandroutes_test.exscover:jain~q,url(~q...),path_for,url_forand a localized helper, the string, regional andLanguageTaginput forms, and an invalid locale.Checks.
mix test273 passed.mix format --check-formatted,mix compile --warnings-as-errorsclean.mix credo --strictreports only the existing module name issue intest/sigil_q_interpolation_test.exs.AI disclosure: AI models helped us find this issue, write the change, and review it.