Skip to content

Commit

Permalink
Gui: fixes #6663: [Bug] Decimal separator not chosen by locale system…
Browse files Browse the repository at this point in the history
… settings anymore
  • Loading branch information
wwmayer committed Mar 28, 2022
1 parent d60dd08 commit bba7719
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 30 deletions.
8 changes: 5 additions & 3 deletions src/Gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,10 +2072,12 @@ void Application::runApplication(void)
mainApp.installEventFilter(filter);
}

if (hGrp->GetBool("UseLocaleFormatting", false)) {
Translator::instance()->setLocale(hGrp->GetASCII(("Language"), Translator::instance()->activeLanguage().c_str()));
// For values different to 1 and 2 use the OS locale settings
auto localeFormat = hGrp->GetInt("UseLocaleFormatting", 0);
if (localeFormat == 1) {
Translator::instance()->setLocale(hGrp->GetASCII("Language", Translator::instance()->activeLanguage().c_str()));
}
else {
else if (localeFormat == 2) {
Translator::instance()->setLocale("C");
}

Expand Down
41 changes: 27 additions & 14 deletions src/Gui/DlgGeneral.ui
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,28 @@
</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>
<widget class="Gui::PrefComboBox" name="UseLocaleFormatting">
<property name="prefEntry" stdset="0">
<cstring>UseLocaleFormatting</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
<item>
<property name="text">
<string>Number format of operating system</string>
</property>
</item>
<item>
<property name="text">
<string>Use selected language number format</string>
</property>
</item>
<item>
<property name="text">
<string>C/POSIX number format</string>
</property>
</item>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -174,24 +182,24 @@ If not, C/POSIX default formatting will be used (English-like)</string>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>30</number>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>100</number>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>30</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>16</number>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>24</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>16</number>
</attribute>
<column>
<property name="text">
<string>Name</string>
Expand Down Expand Up @@ -601,6 +609,11 @@ after FreeCAD launches</string>
<extends>QCheckBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefComboBox</class>
<extends>QComboBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>Languages</tabstop>
Expand Down
50 changes: 40 additions & 10 deletions src/Gui/DlgGeneralImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ using namespace Gui::Dialog;
*/
DlgGeneralImp::DlgGeneralImp( QWidget* parent )
: PreferencePage(parent)
, localeIndex(0)
, ui(new Ui_DlgGeneral)
{
ui->setupUi(this);
Expand Down Expand Up @@ -119,6 +120,38 @@ void DlgGeneralImp::setRecentFileSize()
}
}

void DlgGeneralImp::setLanguage()
{
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString lang = QLocale::languageToString(QLocale().language());
QByteArray language = hGrp->GetASCII("Language", (const char*)lang.toLatin1()).c_str();
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
if (current != language) {
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
}

void DlgGeneralImp::setNumberLocale()
{
int localeFormat = ui->UseLocaleFormatting->currentIndex();

// Only make the change if locale setting has changed
if (localeIndex == localeFormat)
return;

if (localeFormat == 0) {
Translator::instance()->setSystemLocale();
}
else if (localeFormat == 1) {
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
Translator::instance()->setLocale(current.constData());
}
else if (localeFormat == 2) {
Translator::instance()->setLocale("C");
}
}

void DlgGeneralImp::saveSettings()
{
int index = ui->AutoloadModuleCombo->currentIndex();
Expand All @@ -134,17 +167,10 @@ void DlgGeneralImp::saveSettings()
ui->SplashScreen->onSave();

setRecentFileSize();
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString lang = QLocale::languageToString(QLocale().language());
QByteArray language = hGrp->GetASCII("Language", (const char*)lang.toLatin1()).c_str();
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
if (current != language) {
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
if (ui->UseLocaleFormatting->isChecked())
Translator::instance()->setLocale(current.constData());
setLanguage();
setNumberLocale();

ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QVariant size = ui->toolbarIconSize->itemData(ui->toolbarIconSize->currentIndex());
int pixel = size.toInt();
hGrp->SetInt("ToolbarIconSize", pixel);
Expand Down Expand Up @@ -196,6 +222,8 @@ void DlgGeneralImp::loadSettings()
auto langToStr = Translator::instance()->activeLanguage();
QByteArray language = hGrp->GetASCII("Language", langToStr.c_str()).c_str();

localeIndex = ui->UseLocaleFormatting->currentIndex();

int index = 1;
TStringMap list = Translator::instance()->supportedLocales();
ui->Languages->clear();
Expand Down Expand Up @@ -303,7 +331,9 @@ void DlgGeneralImp::loadSettings()
void DlgGeneralImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
int index = ui->UseLocaleFormatting->currentIndex();
ui->retranslateUi(this);
ui->UseLocaleFormatting->setCurrentIndex(index);
}
else {
QWidget::changeEvent(e);
Expand Down
3 changes: 3 additions & 0 deletions src/Gui/DlgGeneralImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ protected Q_SLOTS:
void setRecentFileSize();
void saveAsNewPreferencePack();
void revertToSavedConfig();
void setLanguage();
void setNumberLocale();

private:
int localeIndex;
std::unique_ptr<Ui_DlgGeneral> ui;
std::unique_ptr<DlgCreateNewPreferencePackImp> newPreferencePackDialog;
std::unique_ptr<DlgPreferencePackManagementImp> preferencePackManagementDialog;
Expand Down
18 changes: 15 additions & 3 deletions src/Gui/Language/Translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,28 @@ bool Translator::setLocale(const std::string& language) const
if (!bcp47.empty())
loc = QLocale(QString::fromStdString(bcp47));
QLocale::setDefault(loc);
// Need to manually send the event so locale change is fully took into account on widgets
auto ev = QEvent(QEvent::LocaleChange);
qApp->sendEvent(qApp, &ev);
updateLocaleChange();

#ifdef FC_DEBUG
Base::Console().Log("Locale changed to %s => %s\n", qPrintable(loc.bcp47Name()), qPrintable(loc.name()));
#endif

return (loc.language() != loc.C);
}

void Translator::setSystemLocale() const
{
QLocale::setDefault(QLocale::system());
updateLocaleChange();
}

void Translator::updateLocaleChange() const
{
// Need to manually send the event so locale change is fully took into account on widgets
auto ev = QEvent(QEvent::LocaleChange);
qApp->sendEvent(qApp, &ev);
}

QStringList Translator::directories() const
{
QStringList list;
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/Language/Translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class GuiExport Translator : public QObject
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;
void setSystemLocale() const;
/** Returns a list of supported languages. */
TStringList supportedLanguages() const;
/** Returns a map of supported languages/locales. */
Expand All @@ -81,6 +82,7 @@ class GuiExport Translator : public QObject
void removeTranslators();
QStringList directories() const;
void installQMFiles(const QDir& dir, const char* locale);
void updateLocaleChange() const;

private:
static Translator* _pcSingleton;
Expand Down

0 comments on commit bba7719

Please sign in to comment.