0

In the official documentation there is a description how to configure the server for being ready for internationalization https://angular.io/guide/i18n#configuring-servers. Basically the url will be rewritten on the server-side based on the Accept-Language HTTP header.

I'm nevertheless searching for an approach for determine the locale based on a saved user preference in the backend database.

My project configuration: The angular project is included in a Single-Sign-On infrastructure. Which means the angular project itself hasn't any login page. Before coming onto the angular page, I want to go through a kind of proxy page. This page should request the backend server for the saved locale of the current user. After reading the response the user will be redirected to the locale specific url path. For example: Saved user locale on server: en -> url redirect to /app/en

index.html // the proxy page en de fr are the production build directories of the needed locales

My initial approach is the following:

<script>
  const redirect = async () => {
    const response = await fetch(<Webservice>);
    const result = await response.json();
    const locale = result?.user?.locale || 'en';
    window.location.href = window.location.href.replace('/app/', '/app/' + locale + '/'));
  }

  redirect();
</script>

But I'm very unsure if this approach is a good one. Is it safety? Maybe there is already a standard implementation for this use case?

Thank you in advance!

user2622344
  • 956
  • 1
  • 10
  • 26
  • If I understand correctly, you are trying to get the user's language even before requesting the locale JSON. If that is the case - you can rely on the browser language settings. If a user browser was set on a language of Germany. you can use Germany as a user preferred language and use it for loading your locales – sunilbaba Jan 21 '21 at 14:02
  • no, the locale will only be needed/requested in the above index.html redirect() method.The returned locale will be used for redirecting to the specific url path. – user2622344 Jan 21 '21 at 14:07
  • Let your backend handle the redirection – Leonzen Jan 21 '21 at 14:12
  • Is it in the frontend code a bad approach? – user2622344 Jan 21 '21 at 14:34

0 Answers0