salvadordf / CEF4Delphi

CEF4Delphi is an open source project to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows, Linux and MacOS.
https://www.briskbard.com/forum/
Other
1.2k stars 365 forks source link

Spell Checking for multiple languages does not seem to work for some reason the spellcheck.dictionaries is not set #458

Closed spyrman closed 1 year ago

spyrman commented 1 year ago

Here is a sample code that i use to set the Preference but it does not seem to work also if if do a check for GetAllPreferences(True); it always returns nil any have you tested it ? also is it possible to make a sample code that uses multiple dictionaries and how to set them in my example i have en-US and el-GR the spell check works only for one language at a time when i set it on the project source before the start process like GlobalCEFApp.Locale:='el-GR'; or en-US but i cant set it to spell check for both languages any ideas ?

//==========================
//= Sample code GetAllPreferences(True); 
//===========================
  RequestContext := Chromium1.Browser.Host.RequestContext;
  if Assigned(RequestContext) then
  begin
    Preferences := RequestContext.GetAllPreferences(True);
//============================================================
//= Sample code RequestContext.SetPreference('spellcheck.dictionaries', Value1, TempError);
//=============================================================
procedure TForm1.Button4Click(Sender: TObject);
function SetSpellCheckingLanguages(const Languages: array of string): Boolean;
var
  TempError: ustring;
  TempError2: ustring;
  RequestContext: ICefRequestContext;
  Value1, Value2: ICefValue;
  ListValue: ICefListValue;
  i: Integer;
begin
  Result := False;
  if Assigned(Chromium1) and Chromium1.Initialized then
  begin
    RequestContext := Chromium1.Browser.Host.RequestContext;
    //RequestContext := TCefRequestContextRef.Global;
    if Assigned(RequestContext) then
    begin
      // Create a new ICefListValue
      ListValue := TCefListValueRef.New;
      ListValue.SetSize(Length(Languages));
      // Set the language codes in the ICefListValue
      for i := 0 to High(Languages) do
        ListValue.SetString(i, Languages[i]);
      // Create a new ICefValue to hold the ICefListValue
      Value1 := TCefValueRef.New;
      Value1.SetList(ListValue);
      //Set Value2 to True
      Value2 := TCefValueRef.New;
      Value2.SetBool(True);
      // Set the spellcheck.dictionaries preference
                RequestContext.SetPreference('browser.enable_spellchecking',Value2,Temperror2 );
      Result := RequestContext.SetPreference('spellcheck.dictionaries', Value1, TempError);
      Memo1.Lines.Append('browser.enable_spellchecking Error: '+TempError2);
      Memo1.Lines.Append('Set Greek US Error: '+TempError);
    end;
  end;
end;
begin
  // Set spell-checking languages to English (US) and Greek
  if SetSpellCheckingLanguages(['en-US', 'el-GR']) then
    Memo1.Lines.Append('2 Langs OK: ');
    Chromium1.UpdatePreferences;
end;
salvadordf commented 1 year ago

Hi,

GetAllPreferences must be called from the CEF thread called "UI". Use TChromium.SavePreferences instead as you can see in the MiniBrowser demo.

Set the TChromium.SpellCheckerDicts property to a comma-separated list of language codes and set TChromium.SpellChecking to true to enable the multilingual spellchecker.