Skip to content

Commit

Permalink
Gui: add preference to use selected language number formatting, fixes #…
Browse files Browse the repository at this point in the history
…6330

 If not enabled (default), will defaults to C/POSIX formatting
  • Loading branch information
0penBrain authored and donovaly committed Mar 17, 2022
1 parent 4aec686 commit e3bc06a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Gui/Application.cpp
Expand Up @@ -1920,13 +1920,11 @@ void Application::runApplication(void)
// http://forum.freecadweb.org/viewtopic.php?f=3&t=15540
mainApp.setAttribute(Qt::AA_DontShowIconsInMenus, false);

#ifdef Q_OS_UNIX
// Make sure that we use '.' as decimal point. See also
// http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=559846
// and issue #0002891
// http://doc.qt.io/qt-5/qcoreapplication.html#locale-settings
setlocale(LC_NUMERIC, "C");
#endif

// check if a single or multiple instances can run
it = cfg.find("SingleInstance");
Expand Down Expand Up @@ -2072,6 +2070,13 @@ void Application::runApplication(void)
mainApp.installEventFilter(filter);
}

if (hGrp->GetBool("UseLocaleFormatting", false)) {
Translator::instance()->setLocale(hGrp->GetASCII(("Language"), Translator::instance()->activeLanguage().c_str()));
}
else {
Translator::instance()->setLocale("C");
}

// set text cursor blinking state
int blinkTime = hGrp->GetBool("EnableCursorBlinking", true) ? -1 : 0;
qApp->setCursorFlashTime(blinkTime);
Expand Down
19 changes: 18 additions & 1 deletion src/Gui/DlgGeneral.ui
Expand Up @@ -89,7 +89,7 @@
</item>
</layout>
</item>
<item row="1" column="0">
<item row="1" column="1">
<widget class="Gui::PrefCheckBox" name="SubstituteDecimal">
<property name="toolTip">
<string>If enabled, numerical keypad decimal separator will be substituted with locale separator</string>
Expand All @@ -105,6 +105,23 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="Gui::PrefCheckBox" name="UseLocaleFormatting">
<property name="toolTip">
<string>If enabled, number formatting will be set according to selected language
If not, C/POSIX default formatting will be used (English-like)</string>
</property>
<property name="text">
<string>Use selected language number format</string>
</property>
<property name="prefEntry" stdset="0">
<cstring>UseLocaleFormatting</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
8 changes: 6 additions & 2 deletions src/Gui/DlgGeneralImp.cpp
Expand Up @@ -128,6 +128,7 @@ void DlgGeneralImp::saveSettings()
SetASCII("AutoloadModule", startWbName.toLatin1());

ui->SubstituteDecimal->onSave();
ui->UseLocaleFormatting->onSave();
ui->RecentFiles->onSave();
ui->EnableCursorBlinking->onSave();
ui->SplashScreen->onSave();
Expand All @@ -141,6 +142,8 @@ void DlgGeneralImp::saveSettings()
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
if (ui->UseLocaleFormatting->isChecked())
Translator::instance()->setLocale(current.constData());

QVariant size = ui->toolbarIconSize->itemData(ui->toolbarIconSize->currentIndex());
int pixel = size.toInt();
Expand Down Expand Up @@ -183,14 +186,15 @@ void DlgGeneralImp::loadSettings()
ui->AutoloadModuleCombo->setCurrentIndex(ui->AutoloadModuleCombo->findData(startWbName));

ui->SubstituteDecimal->onRestore();
ui->UseLocaleFormatting->onRestore();
ui->RecentFiles->onRestore();
ui->EnableCursorBlinking->onRestore();
ui->SplashScreen->onRestore();

// search for the language files
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString langToStr = QLocale::languageToString(QLocale().language());
QByteArray language = hGrp->GetASCII("Language", langToStr.toLatin1()).c_str();
auto langToStr = Translator::instance()->activeLanguage();
QByteArray language = hGrp->GetASCII("Language", langToStr.c_str()).c_str();

int index = 1;
TStringMap list = Translator::instance()->supportedLocales();
Expand Down
13 changes: 13 additions & 0 deletions src/Gui/Language/Translator.cpp
Expand Up @@ -251,6 +251,19 @@ std::string Translator::locale(const std::string& lang) const
return loc;
}

bool Translator::setLocale(const std::string& language) const
{
auto loc = QLocale::c(); //Defaulting to POSIX locale
auto bcp47 = locale(language);
if (!bcp47.empty())
loc = QLocale(QString::fromStdString(bcp47));
QLocale::setDefault(loc);
#ifdef FC_DEBUG
Base::Console().Log("Locale changed to %s => %s\n", qPrintable(loc.bcp47Name()), qPrintable(loc.name()));
#endif
return (loc.language() != loc.C);
}

QStringList Translator::directories() const
{
QStringList list;
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/Language/Translator.h
Expand Up @@ -66,6 +66,8 @@ class GuiExport Translator : public QObject
std::string activeLanguage() const;
/** Returns the locale (e.g. "de") to the given language name. */
std::string locale(const std::string&) const;
/** Sets default Qt locale based on given language name. Returns true if matching QLocale found**/
bool setLocale(const std::string&) const;
/** Returns a list of supported languages. */
TStringList supportedLanguages() const;
/** Returns a map of supported languages/locales. */
Expand Down

0 comments on commit e3bc06a

Please sign in to comment.