Skip to content

Commit

Permalink
Add option to encode extended ASCII characters from the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Nov 27, 2024
1 parent 55dcf2f commit 23d460f
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/manual/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ book:
chapters:
- gui/new-project.qmd
- gui/reviewing-output.qmd
- gui/editing.qmd
- gui/options.qmd
- part: "Additional Features"
chapters:
Expand Down
1 change: 1 addition & 0 deletions docs/manual/_quarto.yml.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ book:
chapters:
- gui/new-project.qmd
- gui/reviewing-output.qmd
- gui/editing.qmd
- gui/options.qmd
- part: "Additional Features"
chapters:
Expand Down
41 changes: 41 additions & 0 deletions docs/manual/gui/editing.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
```{r}
#| include: false
#| eval: true
source('../R/appdown.r')
```

# Editing

When a source file in the loaded in the editor and the editor is selected, the **Edit** section of the ribbon will become enabled.
From here, you can edit the source file in the **Editor** and make use of these options from the ribbon.

## Basic Operations

Clipboard operations (e.g., `r keys("Cut")`, `r keys("Copy")`, and `r keys("Paste")`), `r keys("Undo")`, `r keys("Redo")`, and `r keys("Select All")` are all available from this section.

## Insert

From the `r keys("Insert")` button, options are available for inserting and converting content within the editor to fix various warnings.

For example, to fix warnings about strings needing context, place the cursor into the editor in front of a resource loading call (e.g., `_("TABLE")`).
Next, click the `r keys("Insert")` button on the ribbon and select **Translator Comment...**.
Enter a comment that will explain to the translators what "TABLE" means in this context, providing guidance for how they should translate it.
Click `r keys("OK")` and your comment with "TRANSLATORS: " prefixed will be inserted in front of the function call.
("TRANSLATORS: " is the default tag that **gettext** looks for when extracting translator comments.)

::: {.notesection data-latex=""}
This warning is emitted when the option **Check for ambiguous strings that lack a translator comment** (from the **Source Code** settings) is checked.
:::

To fix warnings about extended ASCII characters needing to be encoded, select the text in the editor which contains these characters.
Next, click the `r keys("Insert")` button on the ribbon and select **Encode Extended ASCII Characters...**.
You will then be prompted about how the selection will be re-encoded.
Click `r keys("OK")` and the selection will be replaced with the re-encoded content.

As an example, if you have the string "Błąd" (Polish for "Error") in a source file, some legacy compilers may have difficulty processing it (even if encoded as UTF-8).
By following the aforementioned steps, this will be re-encoded to "B\\U00000142\\U00000105d."

::: {.notesection data-latex=""}
This warning is emitted when the option **Unencoded extended ASCII characters** (from the **Additional Checks** settings) is checked.
:::
12 changes: 1 addition & 11 deletions docs/manual/gui/reviewing-output.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,7 @@ All instances of this warning will be removed from all files. Also, this check w
These options are also available by right clicking any warning and selecting them from the popup menu.
:::

## Editing

When a warning for a source file is selected, the **Edit** section of the ribbon will become enabled.
From here, you can edit the source file in the **Editor** and use these options on the ribbon to edit it.
For example, to fix warnings about strings needing context, place the cursor into the editor in front of a resource loading call (e.g., `_("TABLE")`).
Next, click the `r keys("Insert")` button on the ribbon and select **Translator Comment**.
Enter a comment that will explain to the translators what "TABLE" means in this context, providing guidance for how they should translate it.
Click `r keys("OK")` and your comment with "TRANSLATORS: " prefixed will be inserted in front of the function call.
("TRANSLATORS: " is the default tag that **gettext** looks for when extracting translator comments.)

## Exporting Results

To export all warnings, click the `r keys("Save")` button on the ribbon and select **Export Results**.
From here, you can export the results to either a CSV or tab-delimited text file.
From here, you can export the results to either a CSV or tab-delimited text file.
117 changes: 117 additions & 0 deletions gui/images/unicode-encoded.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "analyze.h"
#include <iostream>
#include <sstream>

namespace i18n_check
{
Expand Down
1 change: 1 addition & 0 deletions src/gui/i18napp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ I18NArtProvider::I18NArtProvider()
{ L"ID_INSERT_TRANSLATOR_COMMENT", L"images/comment.svg" },
{ L"ID_CODE", L"images/code.svg" },
{ L"ID_TRANSLATIONS", L"images/translations.svg" },
{ L"ID_CONVERT_TO_ENCODED_UNICODE", L"images/unicode-encoded.svg" },
{ L"ID_CHECK", L"images/check.svg" },
{ L"ID_FORMAT", L"images/format.svg" },
{ L"ID_DEBUG", L"images/bug.svg" },
Expand Down
60 changes: 57 additions & 3 deletions src/gui/i18nframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ void I18NFrame::InitControls()
Bind(
wxEVT_MENU, [this](wxCommandEvent& event) { OnInsertTranslatorComment(event); },
XRCID("ID_INSERT_TRANSLATOR_COMMENT"));
Bind(
wxEVT_MENU, [this](wxCommandEvent& event) { OnInsertEncodedUnicode(event); },
XRCID("ID_CONVERT_TO_ENCODED_UNICODE"));
Bind(
wxEVT_MENU,
[this]([[maybe_unused]] wxCommandEvent&)
Expand Down Expand Up @@ -765,11 +768,17 @@ void I18NFrame::OnInsert(wxRibbonButtonBarEvent& event)
{
wxMenu menu;
wxMenuItem* menuItem =
new wxMenuItem(&menu, XRCID("ID_INSERT_TRANSLATOR_COMMENT"), _(L"Translator Comment"));
new wxMenuItem(&menu, XRCID("ID_INSERT_TRANSLATOR_COMMENT"), _(L"Translator Comment..."));
menuItem->SetBitmap(wxArtProvider::GetBitmap(L"ID_INSERT_TRANSLATOR_COMMENT", wxART_OTHER,
FromDIP(wxSize{ 16, 16 })));
menu.Append(menuItem);

menuItem = new wxMenuItem(&menu, XRCID("ID_CONVERT_TO_ENCODED_UNICODE"),
_(L"Encode Extended ASCII Characters..."));
menuItem->SetBitmap(wxArtProvider::GetBitmap(L"ID_CONVERT_TO_ENCODED_UNICODE", wxART_OTHER,
FromDIP(wxSize{ 16, 16 })));
menu.Append(menuItem);

event.PopupMenu(&menu);
}

Expand Down Expand Up @@ -1010,7 +1019,7 @@ void I18NFrame::OnInsertTranslatorComment([[maybe_unused]] wxCommandEvent&)
wxTextEntryDialog dialog(
this, _("Enter an explanation for a string that provides context for the translators:"),
_("Translator Comment"), wxString{}, wxTextEntryDialogStyle | wxTE_MULTILINE,
wxDefaultPosition, wxWindow::FromDIP(wxSize{ 150, 250 }));
wxDefaultPosition, wxWindow::FromDIP(wxSize{ 175, 250 }));
if (dialog.ShowModal() != wxID_OK)
{
return;
Expand All @@ -1020,6 +1029,51 @@ void I18NFrame::OnInsertTranslatorComment([[maybe_unused]] wxCommandEvent&)
_DT(L"/* TRANSLATORS: ") + dialog.GetValue() + L" */");
}

//------------------------------------------------------
void I18NFrame::OnInsertEncodedUnicode([[maybe_unused]] wxCommandEvent&)
{
const std::wstring selText{ m_editor->GetSelectedText().wc_string() };
if (selText.empty())
{
wxMessageBox(_(L"No selection found. Please select a string in the editor to encode."),
_(L"No Selection"));
return;
}

std::wstringstream encoded;
bool encodingRequired{ false };
for (const auto& ch : selText)
{
if (ch > 127)
{
encoded << LR"(\U)" << std::setfill(L'0') << std::setw(8)
<< std::uppercase << std::hex << static_cast<int>(ch);
encodingRequired = true;
}
else
{
encoded << ch;
}
}

if (!encodingRequired)
{
wxMessageBox(
_(L"No extened ASCII characters found in the selection that need to be encoded."),
_(L"Encoding Not Required"));
return;
}

if (wxMessageBox(wxString::Format(
_(L"Replace the selected text:\n\n'%s'\n\nwith the following?\n\n'%s'"),
selText, encoded.str()),
_(L"Encode Extended ASCII Characters"),
wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION) == wxYES)
{
m_editor->ReplaceSelection(encoded.str());
}
}

//------------------------------------------------------
void I18NFrame::OnExportResults([[maybe_unused]] wxCommandEvent&)
{
Expand Down Expand Up @@ -1116,7 +1170,7 @@ void I18NFrame::SaveSourceFileIfNeeded()
if (wxFileName{ m_activeSourceFile }.GetExt().CmpNoCase(L"rc") == 0)
{
std::wstring encoding{ L"utf-8" };
const std::wstring& fileText = m_editor->GetText().wc_string();
const std::wstring fileText = m_editor->GetText().wc_string();
const std::wregex codePageRE{ _DT(LR"(#pragma code_page\(([0-9]+)\))") };
std::wsmatch matchResults;
if (std::regex_search(fileText.cbegin(), fileText.cend(), matchResults, codePageRE) &&
Expand Down
1 change: 1 addition & 0 deletions src/gui/i18nframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class I18NFrame : public wxFrame
void OnSave([[maybe_unused]] wxCommandEvent&);
void OnExportResults([[maybe_unused]] wxCommandEvent&);
void OnInsertTranslatorComment([[maybe_unused]] wxCommandEvent&);
void OnInsertEncodedUnicode([[maybe_unused]] wxCommandEvent&);
void OnSaveMenu(wxRibbonButtonBarEvent& event);
void OnRefresh([[maybe_unused]] wxCommandEvent&);
void OnOpenSelectedFile([[maybe_unused]] wxCommandEvent&);
Expand Down

0 comments on commit 23d460f

Please sign in to comment.