1

With Delphi 11.3, i use the webview2 via the tedgeBrowser component. The application using the Tedgebrowser is running on servers running an english Windows. By default the tedgebrowser has a navigator.language value to "en-US".

But i want to be able to change that value.

I found information about the property CoreWebView2EnvironmentOptions.Language But i don't see how to give that language property with the tedgebrowser component.

I also see that some of you recommend to use another component : Webview4Delphi. I tried with the Webview4Delphi demo "simplebrowser" to do this :

initialization

GlobalWebView2Loader := TWVLoader.Create(nil);
GlobalWebView2Loader.UserDataFolder := ExtractFileDir(Application.ExeName) +
  '\CustomCache';
GlobalWebView2Loader.Language := 'fr';  // here my test 
GlobalWebView2Loader.StartWebView2;

But it does no effect. When i do a alert(navigator.language) it still give the default language (OS language).

Can anyone help me with this ?


Solution given by Salvador helped me but was working partially in my case. I applied Salvador's solution + the way given by NoriyukiIchijo on that post :https://github.com/MicrosoftEdge/WebView2Feedback/issues/833#issuecomment-890281898

Marianne
  • 13
  • 4

1 Answers1

1

According to the documentation setting CoreWebView2EnvironmentOptions.Language should also change the Accept-language header but there's a low priority bug in WebView2 and this feature is not working at this moment.

There's a workaround if you change the headers manually. You would have to set a filter in the TWVBRowser.OnAfterCreation event like this :

procedure TMiniBrowserFrm.WVBrowser1AfterCreated(Sender: TObject);
begin
  WVWindowParent1.UpdateSize;
  WVBrowser1.AddWebResourceRequestedFilter('*', COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
end;

The filter is needed to use the TWVBrowser.OnWebResourceRequested event to modify the HTTP headers like this :

procedure TMiniBrowserFrm.WVBrowser1WebResourceRequested(Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2WebResourceRequestedEventArgs);
var
  TempArgs : TCoreWebView2WebResourceRequestedEventArgs;
  TempRequestHeaders : ICoreWebView2HttpRequestHeaders;
begin
  TempArgs:= TCoreWebView2WebResourceRequestedEventArgs.Create(aArgs);
  try
    TempArgs.Request.Get_Headers(TempRequestHeaders);
    TempRequestHeaders.SetHeader('Accept-Language','fr-fr');
  finally
    TempArgs.Free;
  end;
end;

Additionally, you would also have to set GlobalWebView2Loader.Language := 'fr-fr'; before the GlobalWebView2Loader.StartWebView2 call to change the user interface language.

  • Thanks Salvador ! This is working. I was doubting because my first test was checking if the value with alert('navigator.language" ) and that value does'nt change. But the text on the webpage is now in the wanted language ! – Marianne Aug 08 '23 at 08:23
  • I was to quickly happy, it resolves the problem for the first page (login page) of the used website but on the following page everything is in again in english. Are the urls responsible ? first url https://login.[website.com] second url https://www.[website.com] – Marianne Aug 08 '23 at 11:06
  • i see in the DevTools->Network that "Accept-Language" is still fr-fr for the second page... I suspect the developer of the site to use 2 ways to dectect the user language... I'm asking to te site editor. – Marianne Aug 08 '23 at 11:49
  • Some web pages use the IP to locate each client and show the right translation. In other cases they let the user select the preferred language. Other WebView2 developers use the same workaround as you can see here https://github.com/MicrosoftEdge/WebView2Feedback/issues/1162 and here https://github.com/MicrosoftEdge/WebView2Feedback/issues/833 – Salvador Díaz Fau Aug 08 '23 at 13:21
  • 1
    Thanks for your help Salvador ! Yesterday, I also found the solution of NoriyukiIchijo on the issue https://github.com/MicrosoftEdge/WebView2Feedback/issues/833 , i applied a change on that "Preferences" file, which give me a full solution in my case. (even if i don't really like it ;-) ) – Marianne Aug 09 '23 at 07:25