Search code examples
delphitms-web-core

How to retrieve the Web Browser's language configuration in TMS WEB Core?


I'm working on a TMS WEB Core app where I need to display values to the user in their preferred language. Currently, I'm using the FormatSettings to handle formatting, but I'm struggling with getting the correct language configuration.

I know how to change some values of the current FormatSettings and how it works with respect to the Web Browser. In my app, I use FormatSettings to introduce data and display the user-selected configuration. When communicating with the backend, I force a concrete format momentarily, which works well.

However, sometimes the backend returns values that must be shown to the user in their browser's language.

Question Details:

How can I retrieve the Web Browser's language configuration (e.g., "en-US", "en-UK", "es-US", "es-ES", etc.) and store it in a variable within my TMS WEB Core app?

Are there any specific APIs or methods I should use to achieve this?


Solution

  • You can get it using either one of these two methods as they both return the same values.


    1. GetBrowserLocale function from the WEBLib.Tools unit:
    uses
      WEBLib.WebTools;
    
    ...
    
    procedure TForm1.WebButton1Click(Sender: TObject);
    begin
      ShowMessage(GetBrowserLocale);
    end;
    
    1. window.navigator.language function from the Web unit:
    uses
      Web;
    
    ...
    
    procedure TForm1.WebButton1Click(Sender: TObject);
    begin
      ShowMessage(window.navigator.language);
    end;