All package specific locales are now under the 'package' key in locale files. Wildcards in package names are supported.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@971 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-06-04 00:57:29 +00:00
parent ae14784fd6
commit 83a0315211
5 changed files with 46 additions and 91 deletions
+35 -19
View File
@@ -14,12 +14,15 @@
#include <util/io/package_manager.hpp>
#include <script/to_value.hpp>
#include <wx/wfstream.h>
#include <wx/regex.h>
#include <wx/stdpaths.h>
#if defined(__WXMSW__)
#include <wx/mstream.h>
#endif
DECLARE_TYPEOF(map<String COMMA SubLocaleP>);
// ----------------------------------------------------------------------------- : Locale class
LocaleP the_locale;
@@ -42,15 +45,29 @@ IMPLEMENT_REFLECTION_NO_SCRIPT(Locale) {
REFLECT_N("action", translations[LOCALE_CAT_ACTION]);
REFLECT_N("error", translations[LOCALE_CAT_ERROR]);
REFLECT_N("type", translations[LOCALE_CAT_TYPE]);
REFLECT_N("game", game_translations);
REFLECT_N("stylesheet", stylesheet_translations);
REFLECT_N("symbol font", symbol_font_translations);
REFLECT_N("package", package_translations);
}
IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) {
REFLECT_NAMELESS(translations);
}
// ----------------------------------------------------------------------------- : Wildcards
bool match_wildcard(const String& wildcard, const String& name) {
return wxRegEx(replace_all(replace_all(wildcard, _("."), _("\\.")), _("*"), _(".*"))).Matches(name);
}
SubLocaleP find_wildcard(map<String,SubLocaleP>& items, const String& name) {
FOR_EACH_CONST(i, items) {
if (i.second && match_wildcard(i.first, name)) return i.second;
}
return new_intrusive<SubLocale>(); // so we don't search again
}
SubLocaleP find_wildcard_and_set(map<String,SubLocaleP>& items, const String& name) {
return items[name] = find_wildcard(items, name);
}
// ----------------------------------------------------------------------------- : Translation
String warn_and_identity(const String& key) {
@@ -82,24 +99,23 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def) {
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 Package& pkg, const String& key, DefaultLocaleFun def) {
if (!the_locale) return def(key);
SubLocaleP loc = the_locale->package_translations[pkg.relativeFilename()];
if (!loc) {
loc = find_wildcard_and_set(the_locale->package_translations, pkg.relativeFilename());
}
return loc->tr(key, def);
}
IMPLEMENT_TR_TYPE(Package, game_translations) //%% TODO!
IMPLEMENT_TR_TYPE(Game, game_translations)
IMPLEMENT_TR_TYPE(StyleSheet, stylesheet_translations)
IMPLEMENT_TR_TYPE(SymbolFont, symbol_font_translations)
String tr(const Package& pkg, const String& subcat, const String& key, DefaultLocaleFun def) {
if (!the_locale) return def(key);
SubLocaleP loc = the_locale->package_translations[pkg.relativeFilename()];
if (!loc) {
loc = find_wildcard_and_set(the_locale->package_translations, pkg.relativeFilename());
}
return loc->tr(subcat, key, def);
}
// ----------------------------------------------------------------------------- : Validation
+2 -6
View File
@@ -40,12 +40,8 @@ class Locale : public Packaged {
public:
/// Translations of UI strings in each category
SubLocale translations[LOCALE_CAT_MAX];
/// Translations of Game specific texts, by game name
map<String,SubLocaleP> game_translations;
/// Translations of StyleSheet specific texts, by stylesheet name
map<String,SubLocaleP> stylesheet_translations;
/// Translations of SymbolFont specific texts, by symbol font name
map<String,SubLocaleP> symbol_font_translations;
/// Translations of Package specific texts, by relativeFilename
map<String,SubLocaleP> package_translations;
/// Open a locale with the given name
static LocaleP byName(const String& name);
+2 -2
View File
@@ -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),
dc.DrawText(tr(getStylePackage(), 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);
String text = tr(getStylePackage(), s.fieldP->name, capitalize_sentence);
dc.GetTextExtent(text,&w,nullptr);
label_width = max(label_width, w + label_margin);
}
+2 -12
View File
@@ -49,19 +49,9 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def = warn_and
/// Translate 'key' in the for a Package using the current locale
String tr(const Package&, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a Game using the current locale
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, DefaultLocaleFun def);
/// Translate 'key' in the for a SymbolFont using the current locale
String tr(const SymbolFont&, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a Game using the current locale
String tr(const Game&, const String& subcat, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a StyleSheet using the current locale
String tr(const StyleSheet&, const String& subcat, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a SymbolFont using the current locale
String tr(const SymbolFont&, const String& subcat, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a Package using the current locale
String tr(const Package&, const String& subcat, const String& key, DefaultLocaleFun def);
/// A localized string for menus
#define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))