mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
Added validator for locales based on all strings in the source code.
It checks: - whether all keys used by the program are in the locale - whether the right number of %s are used - if there are no extra keys in the locale that shouldn't be there This will become very useful when translations need to be updated for new MSE versions. There is a perl script for generating the 'expected_locale_keys' resource file. This file contains a list of all the locale keys used. This is a resource and not a data file because it is automatically generated from the code, the user has no business modifying it. I also fixed all the locale errors I found in the process. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@613 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+1
-1
@@ -82,7 +82,7 @@ intrusive_ptr<Field> read_new<Field>(Reader& reader) {
|
||||
reader.warning(_ERROR_1_("expected key", _("type")));
|
||||
throw ParseError(_ERROR_("aborting parsing"));
|
||||
} else {
|
||||
reader.warning(_ERROR_1_("Unsupported field type", type));
|
||||
reader.warning(_ERROR_1_("unsupported field type", type));
|
||||
throw ParseError(_ERROR_("aborting parsing"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -697,7 +697,7 @@ void ApprenticeExportWindow::onOk(wxCommandEvent& ev) {
|
||||
}
|
||||
} catch (const AbortException&) {
|
||||
// aborted, cleanup is already handled by dtors
|
||||
wxMessageBox(_LABEL_("apprentice export cancled"), _TITLE_("export canceled"), wxOK | wxICON_INFORMATION);
|
||||
wxMessageBox(_LABEL_("apprentice export cancelled"), _TITLE_("export cancelled"), wxOK | wxICON_INFORMATION);
|
||||
}
|
||||
// Done, close progress window
|
||||
progress_target->Hide();
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
#include <util/io/package_manager.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
|
||||
#include <wx/stdpaths.h>
|
||||
#if defined(__WXMSW__)
|
||||
#include <wx/mstream.h>
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------- : Locale class
|
||||
|
||||
LocaleP the_locale;
|
||||
@@ -109,3 +114,148 @@ String tr(const SymbolFont& f, const String& key, const String& def) {
|
||||
if (!loc) return def; // no information on this symbol font
|
||||
return loc->tr(key, def);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Validation
|
||||
|
||||
DECLARE_POINTER_TYPE(SubLocaleValidator);
|
||||
|
||||
class KeyValidator {
|
||||
public:
|
||||
int args;
|
||||
bool optional;
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
class SubLocaleValidator : public IntrusivePtrBase<SubLocaleValidator> {
|
||||
public:
|
||||
map<String,KeyValidator> keys; ///< Arg count for each key
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
/// Validation information for locales
|
||||
class LocaleValidator {
|
||||
public:
|
||||
map<String, SubLocaleValidatorP> sublocales;
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
template <> void Reader::handle(KeyValidator& k) {
|
||||
String v = getValue();
|
||||
if (starts_with(v, _("optional, "))) {
|
||||
k.optional = true;
|
||||
v = v.substr(10);
|
||||
} else {
|
||||
k.optional = false;
|
||||
}
|
||||
long l = 0;
|
||||
v.ToLong(&l);
|
||||
k.args = l;
|
||||
}
|
||||
template <> void Writer::handle(const KeyValidator& v) {
|
||||
assert(false);
|
||||
}
|
||||
IMPLEMENT_REFLECTION_NO_SCRIPT(SubLocaleValidator) {
|
||||
REFLECT_NAMELESS(keys);
|
||||
}
|
||||
IMPLEMENT_REFLECTION_NO_SCRIPT(LocaleValidator) {
|
||||
REFLECT_NAMELESS(sublocales);
|
||||
}
|
||||
|
||||
/// Count "%s" in str
|
||||
int string_format_args(const String& str) {
|
||||
int count = 0;
|
||||
bool in_percent = false;
|
||||
FOR_EACH_CONST(c, str) {
|
||||
if (in_percent) {
|
||||
if (c == _('s')) {
|
||||
count++;
|
||||
}
|
||||
in_percent = false;
|
||||
} else if (c == _('%')) {
|
||||
in_percent = true;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/// Load a text file from a resource
|
||||
/** TODO: Move me
|
||||
*/
|
||||
InputStreamP load_resource_text(const String& name);
|
||||
InputStreamP load_resource_text(const String& name) {
|
||||
#if defined(__WXMSW__)
|
||||
HRSRC hResource = ::FindResource(wxGetInstance(), name, _("TEXT"));
|
||||
if ( hResource == 0 ) throw InternalError(String::Format(_("Resource not found: %s"), name));
|
||||
HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
|
||||
if ( hData == 0 ) throw InternalError(String::Format(_("Resource not text: %s"), name));
|
||||
char* data = (char *)::LockResource(hData);
|
||||
if ( !data ) throw InternalError(String::Format(_("Resource cannot be locked: %s"), name));
|
||||
int len = ::SizeofResource(wxGetInstance(), hResource);
|
||||
return new_shared2<wxMemoryInputStream>(data, len);
|
||||
#else
|
||||
static String path = wxStandardPaths::Get().GetDataDir() + _("/resource/") + name;
|
||||
return new_shared1<wxFileInputStream>(path);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
DECLARE_TYPEOF(map<String COMMA String>);
|
||||
DECLARE_TYPEOF(map<String COMMA KeyValidator>);
|
||||
|
||||
void Locale::validate(Version ver) {
|
||||
// load locale validator
|
||||
LocaleValidator v;
|
||||
Reader r(load_resource_text(_("expected_locale_keys")), _("expected_locale_keys"));
|
||||
r.handle_greedy(v);
|
||||
// validate
|
||||
String errors;
|
||||
errors += translations[LOCALE_CAT_MENU ].validate(_("menu"), *v.sublocales[_("menu") ]);
|
||||
errors += translations[LOCALE_CAT_HELP ].validate(_("help"), *v.sublocales[_("help") ]);
|
||||
errors += translations[LOCALE_CAT_TOOL ].validate(_("tool"), *v.sublocales[_("tool") ]);
|
||||
errors += translations[LOCALE_CAT_TOOLTIP].validate(_("tooltip"), *v.sublocales[_("tooltip")]);
|
||||
errors += translations[LOCALE_CAT_LABEL ].validate(_("label"), *v.sublocales[_("label") ]);
|
||||
errors += translations[LOCALE_CAT_BUTTON ].validate(_("button"), *v.sublocales[_("button") ]);
|
||||
errors += translations[LOCALE_CAT_TITLE ].validate(_("title"), *v.sublocales[_("title") ]);
|
||||
errors += translations[LOCALE_CAT_ACTION ].validate(_("action"), *v.sublocales[_("action") ]);
|
||||
errors += translations[LOCALE_CAT_ERROR ].validate(_("error"), *v.sublocales[_("error") ]);
|
||||
errors += translations[LOCALE_CAT_TYPE ].validate(_("type"), *v.sublocales[_("type") ]);
|
||||
// errors?
|
||||
if (!errors.empty()) {
|
||||
if (ver != app_version) {
|
||||
errors = _("Errors in locale file ") + short_name + _(":") + errors;
|
||||
} else {
|
||||
errors = _("Errors in locale file ") + short_name +
|
||||
_("\nThis is probably because the locale was made for a different version of MSE.") + errors;
|
||||
}
|
||||
handle_warning(errors);
|
||||
}
|
||||
}
|
||||
|
||||
String SubLocale::validate(const String& name, const SubLocaleValidator& v) const {
|
||||
String errors;
|
||||
// 1. keys in v but not in this, check arg count
|
||||
FOR_EACH_CONST(kc, v.keys) {
|
||||
map<String,String>::const_iterator it = translations.find(kc.first);
|
||||
if (it == translations.end()) {
|
||||
if (!kc.second.optional) {
|
||||
errors += _("\n Missing key:\t\t\t") + name + _(": ") + kc.first;
|
||||
}
|
||||
} else if (string_format_args(it->second) != kc.second.args) {
|
||||
errors += _("\n Incorrect number of arguments for:\t") + name + _(": ") + kc.first
|
||||
+ String::Format(_("\t expected: %d, found %d"), kc.second.args, string_format_args(it->second));
|
||||
}
|
||||
}
|
||||
// 2. keys in this but not in v
|
||||
FOR_EACH_CONST(kv, translations) {
|
||||
map<String,KeyValidator>::const_iterator it = v.keys.find(kv.first);
|
||||
if (it == v.keys.end() && !kv.second.empty()) {
|
||||
// allow extra keys with empty values as a kind of documentation
|
||||
// for example in the help stirngs:
|
||||
// help:
|
||||
// file:
|
||||
// new set: blah blah
|
||||
errors += _("\n Unexpected key:\t\t\t") + name + _(": ") + kv.first;
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
DECLARE_POINTER_TYPE(Locale);
|
||||
DECLARE_POINTER_TYPE(SubLocale);
|
||||
class SubLocaleValidator;
|
||||
|
||||
// ----------------------------------------------------------------------------- : Locale class
|
||||
|
||||
@@ -29,6 +30,9 @@ class SubLocale : public IntrusivePtrBase<SubLocale> {
|
||||
/// Translate a key with a default value
|
||||
String tr(const String& key, const String& def);
|
||||
|
||||
/// Is this a valid sublocale? Returns errors
|
||||
String validate(const String& name, const SubLocaleValidator&) const;
|
||||
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
@@ -47,6 +51,9 @@ class Locale : public Packaged {
|
||||
/// Open a locale with the given name
|
||||
static LocaleP byName(const String& name);
|
||||
|
||||
/// Validate that the locale is valid for this MSE version
|
||||
virtual void validate(Version = app_version);
|
||||
|
||||
protected:
|
||||
String typeName() const;
|
||||
DECLARE_REFLECTION();
|
||||
|
||||
Reference in New Issue
Block a user