Skip to content

Commit

Permalink
Add Qt functions to list of translation marking functions that you ca…
Browse files Browse the repository at this point in the history
…n insert via the editor
  • Loading branch information
Blake-Madden committed Dec 6, 2024
1 parent 7207e6c commit 3de56c6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/manual/gui/editing.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ This warning is emitted when the option **Unencoded extended ASCII characters**

To mark a string as translatable, select the string in the editor and select **Mark for Translation...** from the `r keys("Insert")` button.
From the **Mark for Translation** dialog, options are available for choosing which translation function to use, along with providing a domain or context (if applicable).
The translation marking functions available are compatible with **gettext** and **wxWidgets**.
The translation marking functions available are compatible with *gettext*, *wxWidgets*, and *Qt*.

To mark a string as non-translatable, select the string in the editor and select **Mark as Non-translatable...** from the `r keys("Insert")` button.
From the **Mark as Non-translatable** dialog, options are available for which function to wrap the string in, along with providing an optional context and comment.
These functions will mark the string as non-translatable and **Cuneiform** will ignore them during an analysis.
Additionally, these functions and comments decorating a string will inform developers that the string is not meant for translation and why.
Additionally, these functions and comments decorating a string will inform developers that the string is not meant for translation and why.
53 changes: 45 additions & 8 deletions src/gui/insert_transmacro_dlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ InsertTransMacroDlg::InsertTransMacroDlg(
wxDialog::Create(parent, id, caption, pos, size, style);

m_transMacros.Add(L"_");
// wxWidgets
m_transMacros.Add(L"wxTRANSLATE");
m_transMacros.Add(L"wxTRANSLATE_IN_CONTEXT");
m_transMacros.Add(L"wxGETTEXT_IN_CONTEXT");
m_transMacros.Add(L"wxGetTranslation");
// Qt
m_transMacros.Add(L"tr");
m_transMacros.Add(L"translate");
m_transMacros.Add(L"QT_TR_NOOP");
m_transMacros.Add(L"QT_TR_N_NOOP");
m_transMacros.Add(L"QT_TRANSLATE_NOOP");
m_transMacros.Add(L"QT_TRANSLATE_N_NOOP");
m_transMacros.Add(L"QT_TRANSLATE_NOOP3");
m_transMacros.Add(L"QT_TRANSLATE_N_NOOP3");

m_noTransMacros.Add(L"_DT");
m_noTransMacros.Add(L"DONTTRANSLATE");
Expand Down Expand Up @@ -111,15 +121,18 @@ void InsertTransMacroDlg::CreateControls()
mainDlgSizer->Add(domainSzr, wxSizerFlags{}.Expand().Border());
}

m_commentLabel = new wxStaticText(this, wxID_STATIC,
(m_macroType == TransMacroType::MarkForTranslation) ?
_(L"Explanation for translators:") :
_(L"Explanation for developers:"));
mainDlgSizer->Add(m_commentLabel, wxSizerFlags{}.Border());
m_commentEntry =
new wxTextCtrl(this, wxID_ANY, wxString{}, wxDefaultPosition, FromDIP(wxSize{ 500, 150 }),
wxTE_RICH2 | wxBORDER_THEME | wxTE_BESTWRAP, wxGenericValidator(&m_comment));
mainDlgSizer->Add(m_commentEntry, wxSizerFlags{ 1 }.Expand().Border());

if (m_macroType == TransMacroType::MarkForNoTranslation)
{
m_commentLabel = new wxStaticText(this, wxID_STATIC, _(L"Explanation for developers:"));
mainDlgSizer->Add(m_commentLabel, wxSizerFlags{}.Border());
m_commentEntry = new wxTextCtrl(
this, wxID_ANY, wxString{}, wxDefaultPosition, FromDIP(wxSize{ 500, 150 }),
wxTE_RICH2 | wxBORDER_THEME | wxTE_BESTWRAP, wxGenericValidator(&m_comment));
mainDlgSizer->Add(m_commentEntry, wxSizerFlags{ 1 }.Expand().Border());

mainDlgSizer->Add(
new wxStaticText(
this, wxID_STATIC,
Expand Down Expand Up @@ -185,6 +198,12 @@ void InsertTransMacroDlg::OnOK([[maybe_unused]] wxCommandEvent&)
_(L"Missing Domain"));
return;
}
if (RequiresComment(m_selectedMacro) && m_comment.empty())
{
wxMessageBox(wxString::Format(_(L"%s requires a comment/disambiguation."), m_selectedMacro),
_(L"Missing Comment"));
return;
}

if (IsModal())
{
Expand All @@ -206,6 +225,10 @@ void InsertTransMacroDlg::EnableExtraControls()
m_contextEntry->Enable(RequiresContext(m_selectedMacro) || RequiresDomain(m_selectedMacro));
m_domainLabel->Enable(RequiresDomain(m_selectedMacro));
m_domainEntry->Enable(RequiresDomain(m_selectedMacro));
m_commentLabel->Enable(RequiresComment(m_selectedMacro) ||
CanIncludeComment(m_selectedMacro));
m_commentEntry->Enable(RequiresComment(m_selectedMacro) ||
CanIncludeComment(m_selectedMacro));
}
}

Expand All @@ -223,14 +246,28 @@ wxString InsertTransMacroDlg::GetFormattedOutput()
return m_selectedMacro + L"(" + m_stringToFormat + L", " + quoteStart + m_domain +
L"\", " + quoteStart + m_context + "\")";
}
else if (RequiresComment(m_selectedMacro))
{
return m_selectedMacro + L"(" + quoteStart + m_context + L"\", " + m_stringToFormat +
L", " + quoteStart + m_comment + "\")";
}
else if (RequiresContext(m_selectedMacro))
{
return m_selectedMacro + L"(" + quoteStart + m_context + L"\", " + m_stringToFormat +
")";
}
else
{
return m_selectedMacro + L"(" + m_stringToFormat + ")";
// tr() can take an optional disambiguation
if (m_selectedMacro == L"tr" && !m_comment.empty())
{
return m_selectedMacro + L"(" + m_stringToFormat + L", " + quoteStart + m_comment +
"\")";
}
else
{
return m_selectedMacro + L"(" + m_stringToFormat + ")";
}
}
}
else
Expand Down
17 changes: 16 additions & 1 deletion src/gui/insert_transmacro_dlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class InsertTransMacroDlg final : public wxDialog
bool RequiresContext(const wxString& macro)
{
// wxGetTranslation has a context param, but it is optional
return (macro == L"wxTRANSLATE_IN_CONTEXT" || macro == L"wxGETTEXT_IN_CONTEXT");
return (macro == L"wxTRANSLATE_IN_CONTEXT" || macro == L"wxGETTEXT_IN_CONTEXT" ||
macro == L"QT_TRANSLATE_NOOP" || macro == L"QT_TRANSLATE_N_NOOP");
}

[[nodiscard]]
Expand All @@ -97,6 +98,20 @@ class InsertTransMacroDlg final : public wxDialog
return (macro == L"wxGetTranslation");
}

[[nodiscard]]
bool RequiresComment(const wxString& macro)
{
// QObject::tr has a disambiguation (comment) param, but it is optional
return (macro == L"QT_TRANSLATE_NOOP3" || macro == L"QT_TRANSLATE_N_NOOP3" ||
macro == L"QCoreApplication::translate");
}

[[nodiscard]]
bool CanIncludeComment(const wxString& macro)
{
return (macro == L"tr");
}

void OnOK([[maybe_unused]] wxCommandEvent&);

void OnHelpClicked([[maybe_unused]] wxCommandEvent& event)
Expand Down

0 comments on commit 3de56c6

Please sign in to comment.