Skip to content

Commit

Permalink
Properties: reset plural form expr on language change
Browse files Browse the repository at this point in the history
Previously, changing the language without adjusting plural forms would
result in incorrect plural form expression being used.

Fix this by handling the common case better: if the user changes catalog
language, reset plural forms to use the default expression. This is
almost always what the user wants when manipulating the language.
  • Loading branch information
vslavik committed Sep 16, 2023
1 parent 2830ba5 commit 291c0c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
41 changes: 16 additions & 25 deletions src/propertiesdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,6 @@ PropertiesDialog::PropertiesDialog(wxWindow *parent, CatalogPtr cat, bool fileEx
m_language->Bind(wxEVT_COMBOBOX, &PropertiesDialog::OnLanguageChanged, this);

m_pluralFormsDefault->Bind(wxEVT_RADIOBUTTON, &PropertiesDialog::OnPluralFormsDefault, this);
m_pluralFormsCustom->Bind(wxEVT_RADIOBUTTON, &PropertiesDialog::OnPluralFormsCustom, this);
m_pluralFormsExpr->Bind(
wxEVT_UPDATE_UI,
[=](wxUpdateUIEvent& e){ e.Enable(m_pluralFormsCustom->GetValue()); });
Expand Down Expand Up @@ -875,7 +874,7 @@ void PropertiesDialog::TransferTo(const CatalogPtr& cat)
if (m_hasLang)
{
m_language->SetLang(cat->Header().Lang);
OnLanguageValueChanged(m_language->GetValue());
OnLanguageValueChanged();

PluralFormsExpr pf_def(cat->Header().Lang.DefaultPluralFormsExpr());
PluralFormsExpr pf_cat(cat->Header().GetHeader("Plural-Forms").ToStdString());
Expand Down Expand Up @@ -969,34 +968,34 @@ void PropertiesDialog::DisableSourcesControls()
void PropertiesDialog::OnLanguageChanged(wxCommandEvent& event)
{
m_validatedLang = -1;
OnLanguageValueChanged(event.GetString());
OnLanguageValueChanged();
event.Skip();
}

void PropertiesDialog::OnLanguageValueChanged(const wxString& langstr)
void PropertiesDialog::OnLanguageValueChanged()
{
Language lang = Language::TryParse(langstr.ToStdWstring());
auto pluralForm = lang.DefaultPluralFormsExpr();
if (!pluralForm)
Language lang = m_language->GetLang();
if (lang == m_currentLanguageValue)
return;
m_currentLanguageValue = lang;

auto pluralExpr = lang.DefaultPluralFormsExpr();
auto validPlurals = lang.IsValid() && pluralExpr;
m_pluralFormsDefault->Enable(validPlurals);
if (validPlurals)
{
m_pluralFormsDefault->Disable();
m_pluralFormsCustom->SetValue(true);
m_pluralFormsDefault->SetValue(true);
m_pluralFormsExpr->SetValue(bidi::mark_direction(pluralExpr.str(), TextDirection::LTR));
}
else
{
m_pluralFormsDefault->Enable();
if (m_pluralFormsExpr->GetValue().empty() ||
PluralFormsExpr(m_pluralFormsExpr->GetValue().ToStdString()) == pluralForm)
{
m_pluralFormsDefault->SetValue(true);
}
m_pluralFormsCustom->SetValue(true);
m_pluralFormsExpr->Clear();
}
}

void PropertiesDialog::OnPluralFormsDefault(wxCommandEvent& event)
{
m_rememberedPluralForm = m_pluralFormsExpr->GetValue();

Language lang = m_language->GetLang();
if (lang.IsValid())
{
Expand All @@ -1008,14 +1007,6 @@ void PropertiesDialog::OnPluralFormsDefault(wxCommandEvent& event)
event.Skip();
}

void PropertiesDialog::OnPluralFormsCustom(wxCommandEvent& event)
{
if (!m_rememberedPluralForm.empty())
m_pluralFormsExpr->SetValue(m_rememberedPluralForm);

event.Skip();
}

void PropertiesDialog::OnGettextSettings(wxCommandEvent&)
{
wxWindowPtr<GettextSettingsDialog> dlg(new GettextSettingsDialog(this));
Expand Down
5 changes: 2 additions & 3 deletions src/propertiesdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ class PropertiesDialog : public wxDialog
void DisableSourcesControls();

void OnLanguageChanged(wxCommandEvent& event);
void OnLanguageValueChanged(const wxString& langstr);
void OnLanguageValueChanged();
void OnPluralFormsDefault(wxCommandEvent& event);
void OnPluralFormsCustom(wxCommandEvent& event);
void OnGettextSettings(wxCommandEvent& event);

struct PathsData;
Expand All @@ -83,7 +82,7 @@ class PropertiesDialog : public wxDialog
PathsList *m_paths, *m_excludedPaths;
wxEditableListBox *m_keywords;
wxCheckBox *m_defaultKeywords;
wxString m_rememberedPluralForm;
Language m_currentLanguageValue;
std::shared_ptr<GettextSettings> m_gettextSettings;
bool m_hasLang;
int m_validatedPlural, m_validatedLang;
Expand Down

0 comments on commit 291c0c2

Please sign in to comment.