Setting Locale at Runtime

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Setting Locale at Runtime

Postby chowey » Fri Mar 27, 2015 1:55 pm

Is there anyway to change the locale after CefInitialize?
chowey
Techie
 
Posts: 20
Joined: Fri Oct 25, 2013 3:13 pm

Re: Setting Locale at Runtime

Postby magreenblatt » Mon Mar 30, 2015 4:55 am

Not currently.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Setting Locale at Runtime

Postby kelatty19 » Mon Oct 29, 2018 3:15 am

Will be this feature available in the future? I need it, because i want to change the application language runtime. I don't want to restart the app. The context menu remains english without restart.
kelatty19
Newbie
 
Posts: 2
Joined: Wed Oct 24, 2018 2:40 am

Re: Setting Locale at Runtime

Postby jj707 » Mon Mar 02, 2020 8:58 am

kelatty19 wrote:Will be this feature available in the future? I need it, because i want to change the application language runtime. I don't want to restart the app. The context menu remains english without restart.


Same question
jj707
Newbie
 
Posts: 4
Joined: Wed Feb 12, 2020 1:45 am

Re: Setting Locale at Runtime

Postby Peter » Tue Apr 21, 2020 7:49 am

How can I even set it before context->Initialize() though?

Tried to do CefString(&settings.locale).FromWString(...), and it sort of works - but the context menus are still displayed in the system language.
Peter
Techie
 
Posts: 38
Joined: Thu Mar 05, 2020 9:41 am

Re: Setting Locale at Runtime

Postby ndesktop » Tue Apr 21, 2020 10:03 am

Code: Select all
int Application::Initialize()
{
  ...

  // initialization
  CefSettings settings;
  ...
  std::wstring sLang = L"";
  GetProductLocale(sLang);

  cef_string_copy(sLang.c_str(), static_cast<int>(sLang.length()), &settings->locale);
  ...
}

and implement a function
Code: Select all
void Application::GetProductLocale(std::wstring& sLang)
{
   sLang = L"xx-XX"; // replace with your desired app locale, user locale, system locale etc.
   return;
}


To customize the menus, you need to:
1. derive your client handler implementation from CefContextMenuHandler
2. implement CefClient
Code: Select all
CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() override {
  return this;
}

3. Implement CefContextMenuHandler::OnBeforeContextMenu to set the strings as needed.
In the (Win32) example below, m_pMainWindow is a pointer to a MainWindow class (application main window, obviously), implementing
Code: Select all
bool MainWindow::GetContextMenuLabel(int command_id, std::wstring& str)
{
  str.clear();

  if(command_id == MENU_ID_VIEW_SOURCE) {
    str = L"View le Sources";
    return true;
  }
  // ... more command IDs are in include/internal/cef_types.h, enum cef_menu_id_t

  return !str.empty();
}


Code: Select all
void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params,
    CefRefPtr<CefMenuModel> model)
{
    if(!model)
      return;

    //  set text for all items from our resources, if present
    int nItemCount = model->GetCount();
    for(int c = 0; c < nItemCount; c++) {
        int command_id = model->GetCommandIdAt(c);
        if(command_id != -1) {
            std::wstring strLabel;
            if(m_pMainWindow->GetContextMenuLabel(command_id, strLabel)) {
                _ASSERTE(!strLabel.empty());
                model->SetLabel(command_id, CefString(strLabel));
            }
        }
    }

    //  remove View source in release
    if(model->IsVisible(MENU_ID_VIEW_SOURCE)) {
#ifndef _DEBUG
        model->Remove(MENU_ID_VIEW_SOURCE);
#endif
    }

    return;
}
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: Setting Locale at Runtime

Postby Peter » Tue Apr 21, 2020 11:03 am

str = L"View le Sources"; etc.

You can manually replace these text strings without setting the locale, why bother doing it then?

CEF already has everything translated in /locales/xx_XX.pak
If I change the OS locale, CEF displays context menu in the OS language.
The question is: can I set CEF language without changing the locale in the OS?
Peter
Techie
 
Posts: 38
Joined: Thu Mar 05, 2020 9:41 am

Re: Setting Locale at Runtime

Postby magreenblatt » Tue Apr 21, 2020 11:31 am

Peter wrote:The question is: can I set CEF language without changing the locale in the OS?

What locale are you trying to set? What CEF version are you using? Does the `--lang=xx-XX` command-line flag work for you?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Setting Locale at Runtime

Postby Peter » Wed Apr 22, 2020 4:27 am

CEF version 3112
I have two locales in the project: "en-US" and "ru" (not sure why it is not "ru-RU"), and setting it via `--lang=xx-XX` command-line flag does not work.

Setting `--lang=en-US` while having russian OS locale does not seem to work either.
Peter
Techie
 
Posts: 38
Joined: Thu Mar 05, 2020 9:41 am

Re: Setting Locale at Runtime

Postby magreenblatt » Wed Apr 22, 2020 10:48 am

Please try with a supported CEF version.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 52 guests