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="dlangui">
<Folder name="core">
<File path="src\dlangui\core\stdaction.d" />
<File path="src\dlangui\core\collections.d" />
<File path="src\dlangui\core\events.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 {
private UIStringList _main;
private UIStringList _fallback;
private string _resourceDir;
private string[] _resourceDirs;
/// get i18n resource directory
@property string resourceDir() { return _resourceDir; }
@property string[] resourceDirs() { return _resourceDirs; }
/// 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
string findTranslationsDir(string[] dirs ...) {
string[] findTranslationsDir(string[] dirs ...) {
_resourceDirs.length = 0;
import std.file;
foreach(dir; dirs) {
string path = appendPath(dir, "i18n/");
if (exists(path) && isDir(path)) {
Log.i("Adding i18n dir ", path);
_resourceDir = path;
return _resourceDir;
_resourceDirs ~= path;
}
}
return null;
return _resourceDirs;
}
/// convert resource path - зкуpend resource dir if necessary
string convertResourcePath(string filename) {
/// convert resource path - append resource dir if necessary
string[] convertResourcePaths(string filename) {
if (filename is null)
return null;
bool hasPathDelimiters = false;
foreach(char ch; filename)
if (ch == '/' || ch == '\\')
hasPathDelimiters = true;
if (!hasPathDelimiters && _resourceDir !is null)
return _resourceDir ~ filename;
return filename;
string[] res;
if (!hasPathDelimiters && _resourceDirs.length) {
foreach (dir; _resourceDirs)
res ~= dir ~ filename;
} else {
res ~= filename;
}
return res;
}
this() {
@ -144,9 +149,9 @@ class UIStringTranslator {
bool load(string mainFilename, string fallbackFilename = null) {
_main.clear();
_fallback.clear();
bool res = _main.load(convertResourcePath(mainFilename));
bool res = _main.load(convertResourcePaths(mainFilename));
if (fallbackFilename !is null) {
res = _fallback.load(convertResourcePath(fallbackFilename)) || res;
res = _fallback.load(convertResourcePaths(fallbackFilename)) || res;
}
return res;
}
@ -213,21 +218,24 @@ class UIStringList {
}
/// load strings from file (utf8, id=value lines)
bool load(string filename) {
import std.stream;
import std.file;
try {
Log.d("Loading string resources from file ", filename);
if (!exists(filename) || !isFile(filename)) {
Log.e("File does not exist: ", filename);
return false;
}
std.stream.File f = new std.stream.File(filename);
scope(exit) { f.close(); }
return load(f);
} catch (StreamFileException e) {
Log.e("Cannot read string resources from file ", filename);
bool load(string[] filenames) {
bool res = false;
foreach(filename; filenames) {
import std.stream;
import std.file;
try {
Log.d("Loading string resources from file ", filename);
if (!exists(filename) || !isFile(filename)) {
Log.e("File does not exist: ", filename);
continue;
}
std.stream.File f = new std.stream.File(filename);
scope(exit) { f.close(); }
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);