Skip to content

Commit

Permalink
Ignore Qt positional commands when psuedo translating
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Dec 11, 2024
1 parent 7ae7079 commit a136fbd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/i18n_review.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ namespace i18n_check
LR"((^|\b|[%]{2}|[^%])[%]([%]([[:digit:]]+[$])?p))"
};

// %1, %L1, %n, %Ln
const std::wregex i18n_review::m_positional_command_regex{
LR"([%](n|[L]?[0-9]+|Ln))"
};

// common font faces that we would usually ignore (client can add to this)
std::set<string_util::case_insensitive_wstring> i18n_review::m_font_names = { // NOLINT
L"Arial",
Expand Down Expand Up @@ -2941,6 +2946,32 @@ namespace i18n_check
return results;
}

//--------------------------------------------------
std::vector<std::pair<size_t, size_t>>
i18n_review::load_positional_command_positions(const std::wstring& resource)
{
std::vector<std::pair<size_t, size_t>> results;

std::wstring::const_iterator searchStart{ resource.cbegin() };
std::wsmatch res;
size_t commandPosition{ 0 };
size_t previousLength{ 0 };
while (std::regex_search(searchStart, resource.cend(), res, m_positional_command_regex))
{
searchStart += res.position() + res.length();
commandPosition += res.position() + previousLength;
previousLength = res.length();

results.push_back(std::make_pair(commandPosition, res.length()));
}

// sort by position
std::sort(results.begin(), results.end(),
[](const auto& lhv, const auto& rhv) noexcept { return lhv.first < rhv.first; });

return results;
}

//------------------------------------------------
std::tuple<bool, std::wstring_view, size_t>
i18n_review::read_po_catalog_entry(std::wstring_view& poFileText)
Expand Down
9 changes: 9 additions & 0 deletions src/i18n_review.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,14 @@ namespace i18n_check
static std::vector<std::pair<size_t, size_t>>
load_file_filter_positions(const std::wstring& resource);

/** @brief Loads all positional format commands from a string.
@param resource The string to parse.
@returns A vector of positions and lengths of all
positional format commands from the string.*/
[[nodiscard]]
static std::vector<std::pair<size_t, size_t>>
load_positional_command_positions(const std::wstring& resource);

/** @brief Finds and returns the next translation entry in a gettext po file.
@param poFileText the po file content to parse.
@returns If an entry is found, returns @c true, a view of the block, and its
Expand Down Expand Up @@ -1186,6 +1194,7 @@ namespace i18n_check
static const std::wregex m_printf_cpp_float_regex;
static const std::wregex m_printf_cpp_string_regex;
static const std::wregex m_printf_cpp_pointer_regex;
static const std::wregex m_positional_command_regex;
static const std::wregex m_file_filter_regex;
std::vector<std::wregex> m_untranslatable_regexes;
std::vector<std::wregex> m_translatable_regexes;
Expand Down
14 changes: 12 additions & 2 deletions src/pseudo_translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ namespace i18n_check
}();

const auto printfSpecifiers = i18n_review::load_cpp_printf_command_positions(msg);
const auto positionalSpecifiers = i18n_review::load_positional_command_positions(msg);
const auto fileFilters = i18n_review::load_file_filter_positions(msg);

// Get the position of the first character that is not a space or whitespace control
Expand Down Expand Up @@ -332,6 +333,15 @@ namespace i18n_check
i += foundPos->second;
continue;
}
// step over positional commands
foundPos = std::find_if(positionalSpecifiers.cbegin(), positionalSpecifiers.cend(),
[i](auto val) noexcept { return val.first == i; });
if (foundPos != positionalSpecifiers.cend())
{
newMsg += msg.substr(i, foundPos->second);
i += foundPos->second;
continue;
}

if (std::iswalnum(msg[i]))
{
Expand Down Expand Up @@ -379,14 +389,14 @@ namespace i18n_check
}

if (m_width_change > 0)
{
{
int64_t newCharCountToAdd = static_cast<int64_t>(
std::ceil(msg.length() * (static_cast<double>(m_width_change) / 100)));
if (m_add_surrounding_brackets && newCharCountToAdd >= 2)
{
newCharCountToAdd -= 2;
}
newCharCountToAdd -= trackPrefix.length();
newCharCountToAdd -= trackPrefix.length();
if (newCharCountToAdd > 0)
{
newMsg.insert(0, static_cast<size_t>(newCharCountToAdd) / 2, L'-');
Expand Down

0 comments on commit a136fbd

Please sign in to comment.