Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better i18n Accept-Language parsing to properly support examples (+ as standalone function) #1850

Merged
merged 3 commits into from
Jul 18, 2017

Conversation

WebFreak001
Copy link
Contributor

there is now a function to replicate the default behaviour of a translation context (as returning null simply uses the first element in languages) + the cases that were advertised in the examples (like rewriting en_GB to en_US if only that is available) actually work now and are unittested.
Validation for checking if there are ambigious languages in the allowed languages is still not implemented, but documented how it should be done to avoid issues.

I couldn't find the examples (http://vibed.org/docs#web-localization) in the repository so I didn't change the documentation there that now you can also use en-US or simply en (generic international fallback english) as language tags, but I added it to the translationContext documentation.

there is now a function to replicate the default behaviour of a translation context (as returning null simply uses the first element in languages) + the cases that were advertised in the examples (like rewriting en_GB to en_US if only that is available) actually work now and are unittested.
Validation for checking if there are ambigious languages in the allowed languages is still not implemented, but documented how it should be done to avoid issues.
Copy link
Member

@s-ludwig s-ludwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely good to have as a public API. Apart from the behavior change with regards to the fallback behavior this looks good.

/// ditto
public string determineLanguageByHeader(Tuple...)(HTTPServerRequest req, Tuple allowed_languages) @safe pure
{
return determineLanguageByHeader(req.headers.get("Accept-Language", null), [allowed_languages]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also only(...)

if (lcode == alang && !lextra.length)
return lang;
// request en* == serve en_* && be first occurence
if (lcode == alang && lextra.length && !fallback.length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one fallback case missing here: determineLanguageByHeader("en_US,enCA", ["en_GB"]) == "en_GB"). This should ideally still apply, though: determineLanguageByHeader("en_US,enCA", ["en_GB", "en"]) == "en").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those tests already work, assuming the allowed_languages argument is valid (most generic languages should be to the right of the specific ones)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I misread this as !lextra.length and it's actually fine like it is.

/// ditto
public string determineLanguageByHeader(Tuple...)(string accept_language, Tuple allowed_languages) @safe pure
{
return determineLanguageByHeader(accept_language, [allowed_languages]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[allowed_languages] -> std.range.only(allowed_languages) to avoid GC allocations.

@WebFreak001
Copy link
Contributor Author

I only changed the behaviour of the documented unittest (example on the website) to show that you should do this instead of returning null because it gives the user the best user experience if the programmer overrides the provider to support cookies, etc. Before it would default to the first language in the array (en_US) if if wasn't in the session which would mean that potential matches of the Accept-Language header were lost.

@dlang-bot dlang-bot merged commit cecad1e into vibe-d:master Jul 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants