Cockatrice/libcockatrice_card/libcockatrice/card/card_localization.h
BruebachL ef68a7bdcc
[Card] Add a setting for the language used in card search (#7314)
* [Card] Add a setting for the language used in card search

Localized card names and texts can now be searched too, controlled by a
'Language used in card search' toggle (English, selected card language, or
both) on the general settings page. Untranslated cards always keep matching
in English.

Removed the redundant local copy of the URL templates list in the localized
picture loader while here.

* [Card] Bind search language per FilterString instance

The peg parser rules are set up once per process, so the GenericQuery and
OracleQuery rule actions could not capture per-instance state. Instead of
storing the search language in a process-global that FilterString instance
methods mutate, hand it to the rule actions through a thread-local parse
context and copy it into the filter closures they produce. Card evaluation
in FilterString::check no longer reads any process-global state, and each
instance keeps the language it was built with; constructing one instance no
longer changes what unrelated instances (deck filter, drop-to-hand, zone
views) match against.

The card database display model stores the raw query and rebuilds the
FilterString when the search language changes, since the language is now
bound at parse time.

Add tests for the English/Selected/Both search modes, the English fallback
for untranslated cards, and per-instance language independence.

* [Card] Pass the card search language to deck and zone card searches

Wire the two remaining FilterString consumers to the configured card search
language so card-name matches respect it everywhere:

- DeckFilterString now takes the search language and mode, exposes them to its
  [[card name]] rule action via a thread-local parse context (same pattern as
  FilterString), and the engine's card database uses them for content search.
- ZoneViewZone reads the card language from CardsDisplaySettings when applying
  its search filter, and the reveal-zone widget re-applies the active search
  when the language setting changes.
- The deck-storage search re-runs its filter against the current card language
  setting, including live re-application when the setting changes.

Game-action targeting (DlgMoveTopCardsUntil) intentionally keeps evaluating
against English card names.

* [Card] Rename CardSearchLanguage to SearchLanguageMode

* [Card] Restore displaced namespace doc in card_localization.h

* [Filters] Pass CardSearchLanguage as a single struct

* [CardSearchModel] Match English and localized names in Both mode

Card names are stored in both English and localized forms, so search for
matches in both during the 'Both' search mode instead of checking only
the localized name.

* [CreateTokenDialog] Fetch cardsDisplay settings inside the apply lambda

Avoid capturing the raw settings pointer in the lambda: resolve the card
language and card search language from the settings cache at call time so
the values are always current when the search language is re-applied.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-09-21 08:50:48 +02:00

79 lines
No EOL
2.6 KiB
C++

#ifndef CARD_LOCALIZATION_H
#define CARD_LOCALIZATION_H
#include <QString>
#include <QStringList>
/**
* @brief The card languages card search should run against.
*/
enum class SearchLanguageMode
{
English, ///< Only search the English card names and texts.
Selected, ///< Search the selected card language (untranslated cards still match in English).
Both ///< Search both the English and the selected card language names and texts.
};
/**
* @brief The card language and matching mode searches run against.
*
* Bundles the card language code configured in the settings with the
* SearchLanguageMode, so entry points take one value instead of two related
* parameters.
*/
struct CardSearchLanguage
{
QString language; ///< Card language code (e.g. "de"); empty means the English fallback.
SearchLanguageMode mode = SearchLanguageMode::English; ///< How the language participates in the search.
/**
* @brief Whether only the English card data is searched.
*
* @return True when no card language is selected or English itself is selected.
*/
[[nodiscard]] bool isEnglishOnly() const
{
return language.isEmpty() || language == QLatin1String("en");
}
bool operator==(const CardSearchLanguage &) const = default;
bool operator!=(const CardSearchLanguage &) const = default;
};
/**
* @namespace CardLocalization
* @ingroup Cards
*
* @brief Shared language metadata for localized card text and images.
*
* Lists the language codes Cockatrice can display localized card data for and
* provides human-readable names. The list is shared between Oracle (which
* imports the selected language's card data) and the client settings UI (which
* offers the language choice).
*/
namespace CardLocalization
{
/**
* @brief Language codes for which localized card data can be imported/displayed.
*
* Matches the languages Scryfall can serve localized card images for. "en" is
* always available as the default/fallback and is not listed here.
*
* @return The list of supported language codes.
*/
[[nodiscard]] const QStringList &supportedLanguages();
/**
* @brief Human-readable name for a language code.
*
* Follows the same "native name (English name)" format the UI language list
* uses (e.g. "日本語 (Japanese)"), so the English fallback is always visible.
*
* @param lang Language code (e.g. "de", "ja", "zhs").
* @return The language's native name with its English name in parentheses, or
* the code itself if it cannot be resolved.
*/
[[nodiscard]] QString languageDisplayName(const QString &lang);
} // namespace CardLocalization
#endif // CARD_LOCALIZATION_H