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

Use map instead of 'if' where applicable #8

Open
oxpa opened this issue Feb 4, 2019 · 1 comment
Open

Use map instead of 'if' where applicable #8

oxpa opened this issue Feb 4, 2019 · 1 comment

Comments

@oxpa
Copy link

oxpa commented Feb 4, 2019

{% for code, lang in languages %}

this part:

{% for code, lang in languages %}
  if ($http_accept_language ~* ^{{code}}) {
    # Not safe, but it seems to work. May change depending on the outer directives.
    set $lang {{code}};
  }
{% endfor %}

can be replaced with:


map $http_accept_language $lang {
{% for code, lang in languages %}
^{{code}} {{code}};
{% endfor %}
default en;
}

which reduces the size of configuration (and let you avoid IFs).
You can make it even more interesting by composing a more complex RE with capture like:
^(?<language>(en|fr|de)) $language
Which will result in only two lines: one RE for languages and one default.

@vincentbernat
Copy link
Owner

Thanks for the tip! Unfortunately, a map can only be defined at the http level and this file is included at the server level. I have added the "not safe" comment recently to remind me it currently works because of the outer directives. Even fixed, it's still not a correct solution if your preferred language is not one supported but you have another language as a fallback. A solution would be this C module (https://github.com/giom/nginx_accept_language_module/) or an alternative Lua implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants