i18n: support both resource id and fallback dstring - to allow inline fallback strings

This commit is contained in:
Vadim Lopatin 2015-01-06 23:02:18 +03:00
parent 202ff81633
commit 6cf62f374c
1 changed files with 11 additions and 7 deletions

View File

@ -83,6 +83,11 @@ struct UIString {
this(dstring value) { this(dstring value) {
_value = value; _value = value;
} }
/** create string with resource id and raw value as fallback for missing translations */
this(string id, dstring fallbackValue) {
_id = id;
_value = value;
}
/// Returns string resource id /// Returns string resource id
@ -94,12 +99,9 @@ struct UIString {
} }
/** Get value (either raw or translated by id) */ /** Get value (either raw or translated by id) */
@property dstring value() const { @property dstring value() const {
if (_value !is null) if (_id !is null) // translate ID to dstring
return i18n.get(_id, _value); // get from resource, use _value as fallback
return _value; return _value;
if (_id is null)
return null;
// translate ID to dstring
return i18n.get(_id);
} }
/** Set raw value using property */ /** Set raw value using property */
@property void value(dstring newValue) { @property void value(dstring newValue) {
@ -311,7 +313,7 @@ synchronized class UIStringTranslator {
} }
/** Translate string ID to string (returns "UNTRANSLATED: id" for missing values) */ /** Translate string ID to string (returns "UNTRANSLATED: id" for missing values) */
dstring get(string id) { dstring get(string id, dstring fallbackValue = null) {
if (id is null) if (id is null)
return null; return null;
dstring s = _main.get(id); dstring s = _main.get(id);
@ -320,6 +322,8 @@ synchronized class UIStringTranslator {
s = _fallback.get(id); s = _fallback.get(id);
if (s !is null) if (s !is null)
return s; return s;
if (fallbackValue !is null)
return fallbackValue;
return "UNTRANSLATED: "d ~ toUTF32(id); return "UNTRANSLATED: "d ~ toUTF32(id);
} }
} }