set_from_accept_language
The 'set_from_accept_language' directive sets a variable based on the client's 'Accept-Language' header, prioritizing the best matching locale from a predefined list.
Description
The 'set_from_accept_language' directive is a feature of the NGINX Accept-Language module that allows you to derive a locale variable from the client's HTTP 'Accept-Language' header. The directive requires you to specify a variable (which should start with a '$' symbol) and one or more supported locales. When a request arrives, NGINX inspects the 'Accept-Language' header, which contains a comma-separated list of languages the client prefers. It matches these languages against the specified locales. If a match is found, the variable is set to that locale; otherwise, it defaults to the first locale in the specified list.
The directive operates in various contexts—http, server, and location—giving you flexibility in where to apply locale detection based on user preferences. The locales specified after the variable must be properly ordered as they represent the priority sequence for selection. The directive also accounts for the case where the 'Accept-Language' header may be sorted based on quality values (denoted by 'q'), although this is not guaranteed and can vary across different browsers. If no suitable match is found, the module seamlessly falls back to the first locale in the list, ensuring that every request receives a defined language output.
Config Example
set_from_accept_language $lang en ja pl;
Ensure the variable name starts with '$'; otherwise, it will trigger a warning.
Be cautious if relying on the order of the 'Accept-Language' header; its consistency can vary across browsers.
Locale name comparison is case-insensitive; ensure your locale identifiers are in the correct format.