From ed060aeb0420a5882c664ece7430207a868f9071 Mon Sep 17 00:00:00 2001 From: twanvl Date: Tue, 1 Jan 2008 16:06:58 +0000 Subject: [PATCH] nicer options for defaults in localization tr() functions git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@808 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/locale.cpp | 75 ++++++++------------- src/data/locale.hpp | 7 +- src/data/symbol_font.cpp | 8 +-- src/gui/control/card_list.cpp | 2 +- src/gui/control/card_list_column_select.cpp | 4 +- src/gui/control/native_look_editor.cpp | 4 +- src/render/value/choice.cpp | 2 +- src/render/value/multiple_choice.cpp | 2 +- src/util/locale.hpp | 22 +++--- 9 files changed, 54 insertions(+), 72 deletions(-) diff --git a/src/data/locale.cpp b/src/data/locale.cpp index 717e2221..7a669146 100644 --- a/src/data/locale.cpp +++ b/src/data/locale.cpp @@ -53,18 +53,23 @@ IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) { // ----------------------------------------------------------------------------- : Translation -String SubLocale::tr(const String& key) { +String warn_and_identity(const String& key) { + handle_warning(_("Missing key in locale: ") + key, false); + return key; +} + +String SubLocale::tr(const String& key, DefaultLocaleFun def) { map::const_iterator it = translations.find(key); if (it == translations.end()) { - return _("missing:") + key; + return def(key); } else { return it->second; } } -String SubLocale::tr(const String& key, const String& def) { - map::const_iterator it = translations.find(key); +String SubLocale::tr(const String& subcat, const String& key, DefaultLocaleFun def) { + map::const_iterator it = translations.find(subcat + _(" ") + key); if (it == translations.end()) { - return def; + return def(key); } else { return it->second; } @@ -72,50 +77,28 @@ String SubLocale::tr(const String& key, const String& def) { // from util/locale.hpp -String tr(LocaleCategory cat, const String& key) { - if (!the_locale) return key; // no locale loaded (yet) - return the_locale->translations[cat].tr(key); +String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def) { + if (!the_locale) return def(key); // no locale loaded (yet) + return the_locale->translations[cat].tr(key,def); } +#define IMPLEMENT_TR_TYPE(Type, type_translations) \ + String tr(const Type& t, const String& key, DefaultLocaleFun def) { \ + if (!the_locale) return def(key); \ + SubLocaleP loc = the_locale->type_translations[t.name()]; \ + if (!loc) return def(key); \ + return loc->tr(key, def); \ + } \ + String tr(const Type& t, const String& subcat, const String& key, DefaultLocaleFun def) { \ + if (!the_locale) return def(key); \ + SubLocaleP loc = the_locale->type_translations[t.name()]; \ + if (!loc) return def(key); \ + return loc->tr(subcat, key, def); \ + } -String tr(const Game& g, const String& key) { - if (!the_locale) return key; // no locale loaded (yet) - SubLocaleP loc = the_locale->game_translations[g.name()]; - if (!loc) return key; // no information on this game - return loc->tr(key); -} -String tr(const StyleSheet& s, const String& key) { - if (!the_locale) return key; // no locale loaded (yet) - SubLocaleP loc = the_locale->stylesheet_translations[s.name()]; - if (!loc) return key; // no information on this stylesheet - return loc->tr(key); -} -String tr(const SymbolFont& f, const String& key) { - if (!the_locale) return key; // no locale loaded (yet) - SubLocaleP loc = the_locale->symbol_font_translations[f.name()]; - if (!loc) return key; // no information on this symbol font - return loc->tr(key); -} - - -String tr(const Game& g, const String& key, const String& def) { - if (!the_locale) return def; // no locale loaded (yet) - SubLocaleP loc = the_locale->game_translations[g.name()]; - if (!loc) return def; // no information on this game - return loc->tr(key, def); -} -String tr(const StyleSheet& s, const String& key, const String& def) { - if (!the_locale) return def; // no locale loaded (yet) - SubLocaleP loc = the_locale->stylesheet_translations[s.name()]; - if (!loc) return def; // no information on this stylesheet - return loc->tr(key, def); -} -String tr(const SymbolFont& f, const String& key, const String& def) { - if (!the_locale) return def; // no locale loaded (yet) - SubLocaleP loc = the_locale->symbol_font_translations[f.name()]; - if (!loc) return def; // no information on this symbol font - return loc->tr(key, def); -} +IMPLEMENT_TR_TYPE(Game, game_translations) +IMPLEMENT_TR_TYPE(StyleSheet, stylesheet_translations) +IMPLEMENT_TR_TYPE(SymbolFont, symbol_font_translations) // ----------------------------------------------------------------------------- : Validation diff --git a/src/data/locale.hpp b/src/data/locale.hpp index 0b4de955..6ffeea61 100644 --- a/src/data/locale.hpp +++ b/src/data/locale.hpp @@ -25,10 +25,9 @@ class SubLocale : public IntrusivePtrBase { public: map translations; - /// Translate a key - String tr(const String& key); - /// Translate a key with a default value - String tr(const String& key, const String& def); + /// Translate a key, if not found, apply the default function to the key + String tr(const String& key, DefaultLocaleFun def); + String tr(const String& subcat, const String& key, DefaultLocaleFun def); /// Is this a valid sublocale? Returns errors String validate(const String& name, const SubLocaleValidatorP&) const; diff --git a/src/data/symbol_font.cpp b/src/data/symbol_font.cpp index 31719a78..ec97e94b 100644 --- a/src/data/symbol_font.cpp +++ b/src/data/symbol_font.cpp @@ -481,8 +481,8 @@ String InsertSymbolMenu::getCode(int id, const SymbolFont& font) const { } else if (id == 0 && type == ITEM_CODE) { return name; } else if (id == 0 && type == ITEM_CUSTOM) { - String caption = tr(font,_("title ") + name, capitalize_sentence(name)); - String message = tr(font,_("message ") + name, capitalize_sentence(name)); + String caption = tr(font,_("title"), name, capitalize_sentence); + String message = tr(font,_("message"), name, capitalize_sentence); return wxGetTextFromUser(message, caption); } return wxEmptyString; @@ -501,7 +501,7 @@ wxMenu* InsertSymbolMenu::makeMenu(int id, SymbolFont& font) const { } wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolFont& font) const { if (type == ITEM_SUBMENU) { - wxMenuItem* item = new wxMenuItem(parent, wxID_ANY, tr(font, _("menu item ") + name, name), + wxMenuItem* item = new wxMenuItem(parent, wxID_ANY, tr(font, _("menu item"), name, capitalize), wxEmptyString, wxITEM_NORMAL, makeMenu(first_id, font)); item->SetBitmap(wxNullBitmap); @@ -510,7 +510,7 @@ wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolF wxMenuItem* item = new wxMenuItem(parent, wxID_SEPARATOR); return item; } else { - wxMenuItem* item = new wxMenuItem(parent, first_id, tr(font, _("menu item ") + name, name)); + wxMenuItem* item = new wxMenuItem(parent, first_id, tr(font, _("menu item"), name, capitalize)); // Generate bitmap for use on this item SymbolInFont* symbol = nullptr; if (type == ITEM_CUSTOM) { diff --git a/src/gui/control/card_list.cpp b/src/gui/control/card_list.cpp index 696c2af9..06ead0ea 100644 --- a/src/gui/control/card_list.cpp +++ b/src/gui/control/card_list.cpp @@ -204,7 +204,7 @@ void CardListBase::rebuild() { else if (f.second->card_list_align & ALIGN_CENTER) align = wxLIST_FORMAT_CENTRE; else align = wxLIST_FORMAT_LEFT; InsertColumn((long)column_fields.size(), - tr(*set->game, f.second->card_list_name, capitalize(f.second->card_list_name)), + tr(*set->game, f.second->card_list_name, capitalize), align, cs.width); column_fields.push_back(f.second); } diff --git a/src/gui/control/card_list_column_select.cpp b/src/gui/control/card_list_column_select.cpp index e2b26d00..e08c322d 100644 --- a/src/gui/control/card_list_column_select.cpp +++ b/src/gui/control/card_list_column_select.cpp @@ -76,7 +76,7 @@ void CardListColumnSelectDialog::initList() { // Init items Color window_color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); FOR_EACH(c, columns) { - list->Append(tr(*game, c.field->card_list_name, capitalize(c.field->card_list_name))); + list->Append(tr(*game, c.field->card_list_name, capitalize)); // check int i = list->GetCount() - 1; list->Check(i, c.settings.visible); @@ -89,7 +89,7 @@ void CardListColumnSelectDialog::initList() { void CardListColumnSelectDialog::refreshItem(int i) { list->Check (i, columns[i].settings.visible); - list->SetString(i, tr(*game, columns[i].field->card_list_name, capitalize(columns[i].field->card_list_name)) ); + list->SetString(i, tr(*game, columns[i].field->card_list_name, capitalize) ); } // ----------------------------------------------------------------------------- : Events diff --git a/src/gui/control/native_look_editor.cpp b/src/gui/control/native_look_editor.cpp index 8191f2ee..ba6903f8 100644 --- a/src/gui/control/native_look_editor.cpp +++ b/src/gui/control/native_look_editor.cpp @@ -44,7 +44,7 @@ void NativeLookEditor::drawViewer(RotatedDC& dc, ValueViewer& v) { // draw label dc.SetFont(*wxNORMAL_FONT); // TODO : tr using stylesheet or using game? - dc.DrawText(tr(*set->game, s.fieldP->name, capitalize_sentence(s.fieldP->name)), + dc.DrawText(tr(*set->game, s.fieldP->name, capitalize_sentence), RealPoint(margin_left - s.left, 1)); // draw 3D border draw_control_border(this, dc.getDC(), dc.trRectStraight(s.getInternalRect().grow(1))); @@ -70,7 +70,7 @@ void NativeLookEditor::resizeViewers() { // width of the label string int w; Style& s = *v->getStyle(); - String text = tr(*set->game, s.fieldP->name, capitalize_sentence(s.fieldP->name)); + String text = tr(*set->game, s.fieldP->name, capitalize_sentence); dc.GetTextExtent(text,&w,nullptr); label_width = max(label_width, w + label_margin); } diff --git a/src/render/value/choice.cpp b/src/render/value/choice.cpp index d404b80d..74ff89e7 100644 --- a/src/render/value/choice.cpp +++ b/src/render/value/choice.cpp @@ -85,7 +85,7 @@ void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, c } } if (style.render_style & RENDER_TEXT) { - String text = tr(*viewer.stylesheet, value, capitalize(value)); + String text = tr(*viewer.stylesheet, value, capitalize_sentence); dc.SetFont(style.font, 1.0); RealPoint pos = align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0); dc.DrawTextWithShadow(text, style.font, pos); diff --git a/src/render/value/multiple_choice.cpp b/src/render/value/multiple_choice.cpp index 500263b5..4855d681 100644 --- a/src/render/value/multiple_choice.cpp +++ b/src/render/value/multiple_choice.cpp @@ -74,7 +74,7 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const } if (style().render_style & RENDER_TEXT) { // draw text - String text = tr(*viewer.stylesheet, choice, capitalize_sentence(choice)); + String text = tr(*viewer.stylesheet, choice, capitalize_sentence); RealSize text_size = dc.GetTextExtent(text); dc.DrawText(text, align_in_rect(ALIGN_MIDDLE_LEFT, text_size, RealRect(pos + RealSize(size.width + 1, 0), RealSize(0,size.height)))); diff --git a/src/util/locale.hpp b/src/util/locale.hpp index 8f8459fd..ee795dd8 100644 --- a/src/util/locale.hpp +++ b/src/util/locale.hpp @@ -39,26 +39,26 @@ enum LocaleCategory , LOCALE_CAT_MAX }; +typedef String (*DefaultLocaleFun)(const String&); +/// Return the input and issue a warning +String warn_and_identity(const String&); + /// Translate 'key' in the category 'cat' using the current locale -String tr(LocaleCategory cat, const String& key); +String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def = warn_and_identity); /// Translate 'key' in the for a Game using the current locale -String tr(const Game&, const String& key); +String tr(const Game&, const String& key, DefaultLocaleFun def); /// Translate 'key' in the for a StyleSheet using the current locale -String tr(const StyleSheet&, const String& key); +String tr(const StyleSheet&, const String& key, DefaultLocaleFun def); /// Translate 'key' in the for a SymbolFont using the current locale -String tr(const SymbolFont&, const String& key); +String tr(const SymbolFont&, const String& key, DefaultLocaleFun def); /// Translate 'key' in the for a Game using the current locale -/** If the key is not found, use the default value */ -String tr(const Game&, const String& key, const String& def); +String tr(const Game&, const String& subcat, const String& key, DefaultLocaleFun def); /// Translate 'key' in the for a StyleSheet using the current locale -/** If the key is not found, use the default value */ -String tr(const StyleSheet&, const String& key, const String& def); +String tr(const StyleSheet&, const String& subcat, const String& key, DefaultLocaleFun def); /// Translate 'key' in the for a SymbolFont using the current locale -/** If the key is not found, use the default value */ -String tr(const SymbolFont&, const String& key, const String& def); - +String tr(const SymbolFont&, const String& subcat, const String& key, DefaultLocaleFun def); /// A localized string for menus #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))