Commit 392a09a3 authored by twanvl's avatar twanvl

nicer options for defaults in localization tr() functions

parent de2a4414
...@@ -53,18 +53,23 @@ IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) { ...@@ -53,18 +53,23 @@ IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) {
// ----------------------------------------------------------------------------- : Translation // ----------------------------------------------------------------------------- : 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<String,String>::const_iterator it = translations.find(key); map<String,String>::const_iterator it = translations.find(key);
if (it == translations.end()) { if (it == translations.end()) {
return _("missing:") + key; return def(key);
} else { } else {
return it->second; return it->second;
} }
} }
String SubLocale::tr(const String& key, const String& def) { String SubLocale::tr(const String& subcat, const String& key, DefaultLocaleFun def) {
map<String,String>::const_iterator it = translations.find(key); map<String,String>::const_iterator it = translations.find(subcat + _(" ") + key);
if (it == translations.end()) { if (it == translations.end()) {
return def; return def(key);
} else { } else {
return it->second; return it->second;
} }
...@@ -72,50 +77,28 @@ String SubLocale::tr(const String& key, const String& def) { ...@@ -72,50 +77,28 @@ String SubLocale::tr(const String& key, const String& def) {
// from util/locale.hpp // from util/locale.hpp
String tr(LocaleCategory cat, const String& key) { String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def) {
if (!the_locale) return key; // no locale loaded (yet) if (!the_locale) return def(key); // no locale loaded (yet)
return the_locale->translations[cat].tr(key); return the_locale->translations[cat].tr(key,def);
} }
#define IMPLEMENT_TR_TYPE(Type, type_translations) \
String tr(const Game& g, const String& key) { String tr(const Type& t, const String& key, DefaultLocaleFun def) { \
if (!the_locale) return key; // no locale loaded (yet) if (!the_locale) return def(key); \
SubLocaleP loc = the_locale->game_translations[g.name()]; SubLocaleP loc = the_locale->type_translations[t.name()]; \
if (!loc) return key; // no information on this game if (!loc) return def(key); \
return loc->tr(key); return loc->tr(key, def); \
} } \
String tr(const StyleSheet& s, const String& key) { String tr(const Type& t, const String& subcat, const String& key, DefaultLocaleFun def) { \
if (!the_locale) return key; // no locale loaded (yet) if (!the_locale) return def(key); \
SubLocaleP loc = the_locale->stylesheet_translations[s.name()]; SubLocaleP loc = the_locale->type_translations[t.name()]; \
if (!loc) return key; // no information on this stylesheet if (!loc) return def(key); \
return loc->tr(key); return loc->tr(subcat, key, def); \
} }
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) { IMPLEMENT_TR_TYPE(Game, game_translations)
if (!the_locale) return def; // no locale loaded (yet) IMPLEMENT_TR_TYPE(StyleSheet, stylesheet_translations)
SubLocaleP loc = the_locale->game_translations[g.name()]; IMPLEMENT_TR_TYPE(SymbolFont, symbol_font_translations)
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);
}
// ----------------------------------------------------------------------------- : Validation // ----------------------------------------------------------------------------- : Validation
......
...@@ -25,10 +25,9 @@ class SubLocale : public IntrusivePtrBase<SubLocale> { ...@@ -25,10 +25,9 @@ class SubLocale : public IntrusivePtrBase<SubLocale> {
public: public:
map<String,String> translations; map<String,String> translations;
/// Translate a key /// Translate a key, if not found, apply the default function to the key
String tr(const String& key); String tr(const String& key, DefaultLocaleFun def);
/// Translate a key with a default value String tr(const String& subcat, const String& key, DefaultLocaleFun def);
String tr(const String& key, const String& def);
/// Is this a valid sublocale? Returns errors /// Is this a valid sublocale? Returns errors
String validate(const String& name, const SubLocaleValidatorP&) const; String validate(const String& name, const SubLocaleValidatorP&) const;
......
...@@ -481,8 +481,8 @@ String InsertSymbolMenu::getCode(int id, const SymbolFont& font) const { ...@@ -481,8 +481,8 @@ String InsertSymbolMenu::getCode(int id, const SymbolFont& font) const {
} else if (id == 0 && type == ITEM_CODE) { } else if (id == 0 && type == ITEM_CODE) {
return name; return name;
} else if (id == 0 && type == ITEM_CUSTOM) { } else if (id == 0 && type == ITEM_CUSTOM) {
String caption = tr(font,_("title ") + name, capitalize_sentence(name)); String caption = tr(font,_("title"), name, capitalize_sentence);
String message = tr(font,_("message ") + name, capitalize_sentence(name)); String message = tr(font,_("message"), name, capitalize_sentence);
return wxGetTextFromUser(message, caption); return wxGetTextFromUser(message, caption);
} }
return wxEmptyString; return wxEmptyString;
...@@ -501,7 +501,7 @@ wxMenu* InsertSymbolMenu::makeMenu(int id, SymbolFont& font) const { ...@@ -501,7 +501,7 @@ wxMenu* InsertSymbolMenu::makeMenu(int id, SymbolFont& font) const {
} }
wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolFont& font) const { wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolFont& font) const {
if (type == ITEM_SUBMENU) { 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, wxEmptyString, wxITEM_NORMAL,
makeMenu(first_id, font)); makeMenu(first_id, font));
item->SetBitmap(wxNullBitmap); item->SetBitmap(wxNullBitmap);
...@@ -510,7 +510,7 @@ wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolF ...@@ -510,7 +510,7 @@ wxMenuItem* InsertSymbolMenu::makeMenuItem(wxMenu* parent, int first_id, SymbolF
wxMenuItem* item = new wxMenuItem(parent, wxID_SEPARATOR); wxMenuItem* item = new wxMenuItem(parent, wxID_SEPARATOR);
return item; return item;
} else { } 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 // Generate bitmap for use on this item
SymbolInFont* symbol = nullptr; SymbolInFont* symbol = nullptr;
if (type == ITEM_CUSTOM) { if (type == ITEM_CUSTOM) {
......
...@@ -204,7 +204,7 @@ void CardListBase::rebuild() { ...@@ -204,7 +204,7 @@ void CardListBase::rebuild() {
else if (f.second->card_list_align & ALIGN_CENTER) align = wxLIST_FORMAT_CENTRE; else if (f.second->card_list_align & ALIGN_CENTER) align = wxLIST_FORMAT_CENTRE;
else align = wxLIST_FORMAT_LEFT; else align = wxLIST_FORMAT_LEFT;
InsertColumn((long)column_fields.size(), 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); align, cs.width);
column_fields.push_back(f.second); column_fields.push_back(f.second);
} }
......
...@@ -76,7 +76,7 @@ void CardListColumnSelectDialog::initList() { ...@@ -76,7 +76,7 @@ void CardListColumnSelectDialog::initList() {
// Init items // Init items
Color window_color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); Color window_color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
FOR_EACH(c, columns) { 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 // check
int i = list->GetCount() - 1; int i = list->GetCount() - 1;
list->Check(i, c.settings.visible); list->Check(i, c.settings.visible);
...@@ -89,7 +89,7 @@ void CardListColumnSelectDialog::initList() { ...@@ -89,7 +89,7 @@ void CardListColumnSelectDialog::initList() {
void CardListColumnSelectDialog::refreshItem(int i) { void CardListColumnSelectDialog::refreshItem(int i) {
list->Check (i, columns[i].settings.visible); 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 // ----------------------------------------------------------------------------- : Events
......
...@@ -44,7 +44,7 @@ void NativeLookEditor::drawViewer(RotatedDC& dc, ValueViewer& v) { ...@@ -44,7 +44,7 @@ void NativeLookEditor::drawViewer(RotatedDC& dc, ValueViewer& v) {
// draw label // draw label
dc.SetFont(*wxNORMAL_FONT); dc.SetFont(*wxNORMAL_FONT);
// TODO : tr using stylesheet or using game? // 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)); RealPoint(margin_left - s.left, 1));
// draw 3D border // draw 3D border
draw_control_border(this, dc.getDC(), dc.trRectStraight(s.getInternalRect().grow(1))); draw_control_border(this, dc.getDC(), dc.trRectStraight(s.getInternalRect().grow(1)));
...@@ -70,7 +70,7 @@ void NativeLookEditor::resizeViewers() { ...@@ -70,7 +70,7 @@ void NativeLookEditor::resizeViewers() {
// width of the label string // width of the label string
int w; int w;
Style& s = *v->getStyle(); 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); dc.GetTextExtent(text,&w,nullptr);
label_width = max(label_width, w + label_margin); label_width = max(label_width, w + label_margin);
} }
......
...@@ -85,7 +85,7 @@ void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, c ...@@ -85,7 +85,7 @@ void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, c
} }
} }
if (style.render_style & RENDER_TEXT) { 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); dc.SetFont(style.font, 1.0);
RealPoint pos = align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0); RealPoint pos = align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0);
dc.DrawTextWithShadow(text, style.font, pos); dc.DrawTextWithShadow(text, style.font, pos);
......
...@@ -74,7 +74,7 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const ...@@ -74,7 +74,7 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const
} }
if (style().render_style & RENDER_TEXT) { if (style().render_style & RENDER_TEXT) {
// draw 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); RealSize text_size = dc.GetTextExtent(text);
dc.DrawText(text, align_in_rect(ALIGN_MIDDLE_LEFT, text_size, dc.DrawText(text, align_in_rect(ALIGN_MIDDLE_LEFT, text_size,
RealRect(pos + RealSize(size.width + 1, 0), RealSize(0,size.height)))); RealRect(pos + RealSize(size.width + 1, 0), RealSize(0,size.height))));
......
...@@ -39,26 +39,26 @@ enum LocaleCategory ...@@ -39,26 +39,26 @@ enum LocaleCategory
, LOCALE_CAT_MAX , 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 /// 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 /// 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 /// 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 /// 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 /// 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& subcat, const String& key, DefaultLocaleFun def);
String tr(const Game&, const String& key, const String& def);
/// Translate 'key' in the for a StyleSheet using the current locale /// 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& subcat, const String& key, DefaultLocaleFun def);
String tr(const StyleSheet&, const String& key, const String& def);
/// Translate 'key' in the for a SymbolFont using the current locale /// 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& subcat, const String& key, DefaultLocaleFun def);
String tr(const SymbolFont&, const String& key, const String& def);
/// A localized string for menus /// A localized string for menus
#define _MENU_(s) tr(LOCALE_CAT_MENU, _(s)) #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment