standard actions; i18n: support merging of translations from several files

This commit is contained in:
Vadim Lopatin 2014-05-22 10:05:10 +04:00
parent add02696aa
commit 6cc3dc342d
5 changed files with 110 additions and 29 deletions

View File

@ -308,6 +308,7 @@
<Folder name="src"> <Folder name="src">
<Folder name="dlangui"> <Folder name="dlangui">
<Folder name="core"> <Folder name="core">
<File path="src\dlangui\core\stdaction.d" />
<File path="src\dlangui\core\collections.d" /> <File path="src\dlangui\core\collections.d" />
<File path="src\dlangui\core\events.d" /> <File path="src\dlangui\core\events.d" />
<File path="src\dlangui\core\i18n.d" /> <File path="src\dlangui\core\i18n.d" />

12
res/i18n/en.ini Normal file
View File

@ -0,0 +1,12 @@
#common actions, used in standard dialogs and controls
#english language resources
ACTION_OK=Ok
ACTION_CANCEL=Cancel
ACTION_YES=Yes
ACTION_NO=No
ACTION_CLOSE=Close
ACTION_ABORT=Abort
ACTION_RETRY=Retry
ACTION_IGNORE=Ignore
ACTION_OPEN=Open
ACTION_SAVE=Save

12
res/i18n/ru.ini Normal file
View File

@ -0,0 +1,12 @@
#common actions, used in standard dialogs and controls
#russian language resources
ACTION_OK=Ок
ACTION_CANCEL=Отмена
ACTION_YES=Да
ACTION_NO=Нет
ACTION_CLOSE=Закрыть
ACTION_ABORT=Прервать
ACTION_RETRY=Повтор
ACTION_IGNORE=Игнорировать
ACTION_OPEN=Открыть
ACTION_SAVE=Сохранить

View File

@ -104,36 +104,41 @@ public __gshared UIStringTranslator i18n = new UIStringTranslator();
class UIStringTranslator { class UIStringTranslator {
private UIStringList _main; private UIStringList _main;
private UIStringList _fallback; private UIStringList _fallback;
private string _resourceDir; private string[] _resourceDirs;
/// get i18n resource directory /// get i18n resource directory
@property string resourceDir() { return _resourceDir; } @property string[] resourceDirs() { return _resourceDirs; }
/// set i18n resource directory /// set i18n resource directory
@property void resourceDir(string dir) { _resourceDir = dir; } @property void resourceDirs(string[] dirs) { _resourceDirs = dirs; }
/// looks for i18n directory inside one of passed dirs, and uses first found as directory to read i18n files from /// looks for i18n directory inside one of passed dirs, and uses first found as directory to read i18n files from
string findTranslationsDir(string[] dirs ...) { string[] findTranslationsDir(string[] dirs ...) {
_resourceDirs.length = 0;
import std.file; import std.file;
foreach(dir; dirs) { foreach(dir; dirs) {
string path = appendPath(dir, "i18n/"); string path = appendPath(dir, "i18n/");
if (exists(path) && isDir(path)) { if (exists(path) && isDir(path)) {
Log.i("Adding i18n dir ", path); Log.i("Adding i18n dir ", path);
_resourceDir = path; _resourceDirs ~= path;
return _resourceDir;
} }
} }
return null; return _resourceDirs;
} }
/// convert resource path - зкуpend resource dir if necessary /// convert resource path - append resource dir if necessary
string convertResourcePath(string filename) { string[] convertResourcePaths(string filename) {
if (filename is null) if (filename is null)
return null; return null;
bool hasPathDelimiters = false; bool hasPathDelimiters = false;
foreach(char ch; filename) foreach(char ch; filename)
if (ch == '/' || ch == '\\') if (ch == '/' || ch == '\\')
hasPathDelimiters = true; hasPathDelimiters = true;
if (!hasPathDelimiters && _resourceDir !is null) string[] res;
return _resourceDir ~ filename; if (!hasPathDelimiters && _resourceDirs.length) {
return filename; foreach (dir; _resourceDirs)
res ~= dir ~ filename;
} else {
res ~= filename;
}
return res;
} }
this() { this() {
@ -144,9 +149,9 @@ class UIStringTranslator {
bool load(string mainFilename, string fallbackFilename = null) { bool load(string mainFilename, string fallbackFilename = null) {
_main.clear(); _main.clear();
_fallback.clear(); _fallback.clear();
bool res = _main.load(convertResourcePath(mainFilename)); bool res = _main.load(convertResourcePaths(mainFilename));
if (fallbackFilename !is null) { if (fallbackFilename !is null) {
res = _fallback.load(convertResourcePath(fallbackFilename)) || res; res = _fallback.load(convertResourcePaths(fallbackFilename)) || res;
} }
return res; return res;
} }
@ -213,21 +218,24 @@ class UIStringList {
} }
/// load strings from file (utf8, id=value lines) /// load strings from file (utf8, id=value lines)
bool load(string filename) { bool load(string[] filenames) {
import std.stream; bool res = false;
import std.file; foreach(filename; filenames) {
try { import std.stream;
Log.d("Loading string resources from file ", filename); import std.file;
if (!exists(filename) || !isFile(filename)) { try {
Log.e("File does not exist: ", filename); Log.d("Loading string resources from file ", filename);
return false; if (!exists(filename) || !isFile(filename)) {
} Log.e("File does not exist: ", filename);
std.stream.File f = new std.stream.File(filename); continue;
scope(exit) { f.close(); } }
return load(f); std.stream.File f = new std.stream.File(filename);
} catch (StreamFileException e) { scope(exit) { f.close(); }
Log.e("Cannot read string resources from file ", filename); res = load(f) || res;
} catch (StreamFileException e) {
Log.e("Cannot read string resources from file ", filename);
}
} }
return false; return res;
} }
} }

View File

@ -0,0 +1,48 @@
// Written in the D programming language.
/**
DLANGUI library.
Definition of standard actions commonly used in dialogs and controls.
Synopsis:
----
import dlangui.core.stdaction;
----
Copyright: Vadim Lopatin, 2014
License: Boost License 1.0
Authors: Vadim Lopatin, coolreader.org@gmail.com
*/
module dlangui.core.stdaction;
public import dlangui.core.events;
/// standard (commonly used) action codes
enum StandardAction : int {
Ok = 1,
Cancel,
Yes,
No,
Close,
Abort,
Retry,
Ignore,
Open,
Save,
}
__gshared const Action ACTION_OK = new Action(StandardAction.Ok, "ACTION_OK"c);
__gshared const Action ACTION_CANCEL = new Action(StandardAction.Cancel, "ACTION_CANCEL"c);
__gshared const Action ACTION_YES = new Action(StandardAction.Yes, "ACTION_YES"c);
__gshared const Action ACTION_NO = new Action(StandardAction.No, "ACTION_NO"c);
__gshared const Action ACTION_CLOSE = new Action(StandardAction.Close, "ACTION_CLOSE"c);
__gshared const Action ACTION_ABORT = new Action(StandardAction.Abort, "ACTION_ABORT"c);
__gshared const Action ACTION_RETRY = new Action(StandardAction.Retry, "ACTION_RETRY"c);
__gshared const Action ACTION_IGNORE = new Action(StandardAction.Ignore, "ACTION_IGNORE"c);
__gshared const Action ACTION_OPEN = new Action(StandardAction.Open, "ACTION_OPEN"c);
__gshared const Action ACTION_SAVE = new Action(StandardAction.Save, "ACTION_SAVE"c);