NGINX configure for internationalization

June 5, 2024 · 3 min readLucian Ciobanu
Tags:AngularNGINX

Working on a consumer facing app with hundreds of thousands of users comes with it's special challenges - there is MarTech (marketing technology) with Google Tag Management and it's related scripts which have to be accommodated but also internationalization so the user's experience is custom tailored based and a specific server culture is created.

The challenge I want to take note of is how to configure an NGINX server which is most often used as a proxy for web apps so that it can have access to the user's language preferences which have been predefined in the browser but also, how to overwrite these also from NGINX conf in case they change the language from the app and thus the browser preferences settings secondary.

For this implementation I've used Angular's built in i18n, solely by following it's documentation from angular.

The NGINX config file is default.conf.template:

#The part bellow receives the browser prefernce on http_accept_language
#and maps it to our $accept_language variable
#It is important that what we map it to has the same value to the angular i18n config
map $http_accept_language $accept_language {
  ~*^en en;
  ~*^fr fr;
  ~*^de de;
  ~*^es es;
}
    
#The next part, gets a language property storred in the apps cookies
#if it exists or defaults to $accepted_language
map $http_cookie $preferred_language {
  default $accept_language;
  "~*language=(?<language>(en|fr|de|es))" $language;
}

server {
  #bits of confidential code
  #Fallback to default language if $preferred_language remains undefined
  #after defaultin to $accept_language above
  if ($preferred_language ~ "^$") {
    set $preferred_language "en_US";
  }
  
  #Redirect to Angular app in the preferred language
  rewrite ^/$ /$preferred_language brea;
  location ~ ^/(en|fr|de|es) {
    root /usr/share/nginx.html;
    index index.html index.htm;
    try_files $uri /$1/index.html?$args;
  }
}

For me this seemed the most challenging part as I have not worked prior with NGINX conf files.

From the app itself, to change languages, we need an <a> with href pointing to the new language files. We also need "/ /" around the files to describe the link.

@for (lang of languages; track lang) {
  <a href="/{{ lang.href }}/" (click)="setLanguage(lang.href)">{{lang.name}}</a>
}

The set language call creates the cookie as follows:

languages: {name: string; href: string}[] = [
  { name: 'English', href: 'en'},
  { name: 'French', href: 'fr'},
  { name: 'Deutsch', href: 'de'},
  { name: 'Spanish', href: 'sp'},
];

setLanguage(language: string): void {
  document.cookie = `language=${language}; path=/`;
}

The cookie has a path which makes it relevant to when reloading the app right at the start.

And that's it! This way the server will look for information both from what the user selects within the app, but also to the "quality" array of languages from the browser which is usually sent in the "Accepted-Language" header via each call.

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery