Improve I18n support
Among the first steps to produce better i18n support is to test the created translations adhoc without running "make install". To achieve this, fluxbox now honors several environment variables: - NLSPATH: fluxbox won't create the absolute path to the catalog and thus catopen() is free to use NLSPATH as described in the manpage. Example given: "/tmp/%N" will pick "/tmp/fluxbox.cat". %N refers to FLUXBOX_CATFILE. - FLUXBOX_CATFILE: By setting FLUXBOX_CATFILE the users can make fluxbox to use a different name for the catalog file. Default: "fluxbox.cat" - FLUXBOX_CATDIR: Per default fluxbox tries to find FLUXBOX_CATFILE at several places. Setting this environment variable allows to point fluxbox to a different search path for the catalog files. Then, fluxbox tries catopen() first without changing the deduced catalog file name. After that it applies some heuristics to get a good catalog file name.
This commit is contained in:
parent
57f21b64ca
commit
fff0abad76
6 changed files with 140 additions and 74 deletions
177
src/FbTk/I18n.cc
177
src/FbTk/I18n.cc
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <cstdarg>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#ifdef HAVE_LOCALE_H
|
#ifdef HAVE_LOCALE_H
|
||||||
|
@ -65,15 +66,54 @@ using std::string;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const nl_catd INVALID_CATALOG = ((nl_catd)(-1));
|
const char UTF8_SUFFIX[] = "-UTF-8.cat";
|
||||||
nl_catd s_catalog_fd = INVALID_CATALOG;
|
const size_t UTF8_SUFFIX_LEN = sizeof(UTF8_SUFFIX)-1; // without \0
|
||||||
|
const char DEFAULT_CATFILE[] = "fluxbox.cat";
|
||||||
|
const char ENV_CATFILE[] = "FLUXBOX_CATFILE";
|
||||||
|
const char ENV_CATDIR[] = "FLUXBOX_CATDIR";
|
||||||
|
|
||||||
|
const nl_catd INVALID_CATALOG = (nl_catd)(-1);
|
||||||
|
nl_catd s_catalog_fd = INVALID_CATALOG;
|
||||||
|
|
||||||
|
|
||||||
|
const char* getCatalogDir() {
|
||||||
|
const char* cat_dir = getenv(ENV_CATDIR);
|
||||||
|
if (cat_dir) {
|
||||||
|
return cat_dir;
|
||||||
|
}
|
||||||
|
return LOCALEPATH;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string join_str(size_t n, ...) {
|
||||||
|
std::string s;
|
||||||
|
va_list args;
|
||||||
|
va_start(args, n);
|
||||||
|
for (; n > 0; n--) {
|
||||||
|
s.append(va_arg(args, const char*));
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace FbTk {
|
namespace FbTk {
|
||||||
|
|
||||||
|
|
||||||
|
// initialize the i18n-system be opening the catalog-file
|
||||||
|
// named by 'catalog'. per default we expect 'catalog' to
|
||||||
|
// be 0/NULL, the code picks a sane default then:
|
||||||
|
//
|
||||||
|
// - environment variable FLUXBOX_CATFILE is set? use it
|
||||||
|
// - DEFAULT_CATFILE ("fluxbox.cat")
|
||||||
|
// - the utf8 encoded translation for the current locale
|
||||||
|
//
|
||||||
|
// handling things this was allows us to test catalog files
|
||||||
|
// without putting them into the install path
|
||||||
|
// $PREFIX/share/fluxbox/nls/XYZ/
|
||||||
void I18n::init(const char* catalog) {
|
void I18n::init(const char* catalog) {
|
||||||
|
|
||||||
static bool init = false;
|
static bool init = false;
|
||||||
if (init) {
|
if (init) {
|
||||||
return;
|
return;
|
||||||
|
@ -81,50 +121,87 @@ void I18n::init(const char* catalog) {
|
||||||
|
|
||||||
#if defined(NLS) && defined(HAVE_CATOPEN)
|
#if defined(NLS) && defined(HAVE_CATOPEN)
|
||||||
|
|
||||||
FbStringUtil::init();
|
if (!catalog) {
|
||||||
|
const char* c = getenv(ENV_CATFILE);
|
||||||
I18n& i18n = I18n::instance();
|
if (!c) {
|
||||||
|
c = DEFAULT_CATFILE;
|
||||||
string filename = LOCALEPATH;
|
}
|
||||||
filename += '/';
|
catalog = c;
|
||||||
filename += i18n.m_locale;
|
|
||||||
filename += '/';
|
|
||||||
filename += catalog;
|
|
||||||
|
|
||||||
if (!FileUtil::isRegularFile(filename.c_str()) && i18n.m_locale != "C" && FbStringUtil::haveUTF8()) {
|
|
||||||
// try the UTF-8 catalog, this also picks up situations where
|
|
||||||
// the codeset somehow isn't specified
|
|
||||||
|
|
||||||
// remove everything after @
|
|
||||||
string::size_type index = i18n.m_locale.find('.');
|
|
||||||
// erase all characters starting at index
|
|
||||||
if (index != string::npos)
|
|
||||||
i18n.m_locale.erase(index);
|
|
||||||
|
|
||||||
i18n.m_locale.append(".UTF-8");
|
|
||||||
i18n.m_utf8_translate = true;
|
|
||||||
|
|
||||||
filename = LOCALEPATH;
|
|
||||||
filename += '/';
|
|
||||||
filename += i18n.m_locale;
|
|
||||||
filename += '/';
|
|
||||||
filename += catalog;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FbStringUtil::init();
|
||||||
|
|
||||||
|
int flag;
|
||||||
|
|
||||||
|
I18n& i18n = I18n::instance();
|
||||||
|
const string dir = getCatalogDir();
|
||||||
|
const string locale = i18n.m_locale;
|
||||||
|
string clean_locale = locale;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
// clean the locale, we have to append something later on
|
||||||
|
i = clean_locale.find('.');
|
||||||
|
if (i != string::npos)
|
||||||
|
clean_locale.erase(i);
|
||||||
|
|
||||||
#ifdef MCLoadBySet
|
#ifdef MCLoadBySet
|
||||||
s_catalog_fd = catopen(filename.c_str(), MCLoadBySet);
|
flag = MCLoadBySet;
|
||||||
#else // !MCLoadBySet
|
#else
|
||||||
s_catalog_fd = catopen(filename.c_str(), NL_CAT_LOCALE);
|
flag = NL_CAT_LOCALE;
|
||||||
#endif // MCLoadBySet
|
#endif
|
||||||
|
|
||||||
|
struct { std::string catalog; std::string locale; bool utf8; } _catalog[] = {
|
||||||
|
|
||||||
|
// first try pure 'catalog'. catopen() will use NLSPATH if it's
|
||||||
|
// set and replaces '%N' by 'catalog'. eg: with catalog="fluxbox.cat"
|
||||||
|
// "/usr/share/fluxbox/nls/C/%N" becomes "/usr/share/fluxbox/nls/C/fluxbox.cat"
|
||||||
|
{ string(catalog), locale, false },
|
||||||
|
|
||||||
|
// try full-path to 'catalog'
|
||||||
|
{ join_str(5, dir.c_str(), "/", locale.c_str(), "/", catalog), locale, false },
|
||||||
|
|
||||||
|
// try the UTF-8 catalog, this also picks up situations where
|
||||||
|
// the codeset somehow isn't specified
|
||||||
|
{ join_str(5, dir.c_str(), "/", clean_locale.c_str(), ".UTF-8/", catalog),
|
||||||
|
join_str(2, clean_locale.c_str(), ".UTF8"), true},
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(_catalog)/sizeof(_catalog[0]); i++) {
|
||||||
|
|
||||||
|
if (_catalog[i].utf8 && locale == "C") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* fname = _catalog[i].catalog.c_str();
|
||||||
|
|
||||||
|
s_catalog_fd = catopen(fname, flag);
|
||||||
|
if (s_catalog_fd == INVALID_CATALOG) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
i18n.m_locale = _catalog[i].locale;
|
||||||
|
if (FbStringUtil::haveUTF8()) {
|
||||||
|
if (_catalog[i].utf8) {
|
||||||
|
i18n.m_utf8_translate = true;
|
||||||
|
} else {
|
||||||
|
size_t n = _catalog[i].catalog.rfind(UTF8_SUFFIX);
|
||||||
|
if (n != std::string::npos && (n + UTF8_SUFFIX_LEN) == _catalog[i].catalog.size()) {
|
||||||
|
i18n.m_utf8_translate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (s_catalog_fd == INVALID_CATALOG) {
|
if (s_catalog_fd == INVALID_CATALOG) {
|
||||||
cerr<<"Warning: Failed to open file("<<filename<<")"<<endl
|
cerr<<"Warning: Failed to open file("<< catalog <<")"<<endl
|
||||||
<<"for translation, using default messages."<<endl;
|
<<"for translation, using default messages."<<endl;
|
||||||
}
|
}
|
||||||
#endif // HAVE_CATOPEN
|
#endif // HAVE_CATOPEN
|
||||||
}
|
}
|
||||||
|
|
||||||
I18n::I18n():m_multibyte(false), m_utf8_translate(false) {
|
I18n::I18n() : m_multibyte(false), m_utf8_translate(false) {
|
||||||
#if defined(HAVE_SETLOCALE) && defined(NLS)
|
#if defined(HAVE_SETLOCALE) && defined(NLS)
|
||||||
//make sure we don't get 0 to m_locale string
|
//make sure we don't get 0 to m_locale string
|
||||||
char *temp = setlocale(LC_MESSAGES, "");
|
char *temp = setlocale(LC_MESSAGES, "");
|
||||||
|
@ -146,14 +223,13 @@ I18n::I18n():m_multibyte(false), m_utf8_translate(false) {
|
||||||
|
|
||||||
// truncate any encoding off the end of the locale
|
// truncate any encoding off the end of the locale
|
||||||
|
|
||||||
// remove everything after @
|
|
||||||
string::size_type index = m_locale.find('@');
|
string::size_type index = m_locale.find('@');
|
||||||
if (index != string::npos)
|
if (index != string::npos)
|
||||||
m_locale.erase(index); //erase all characters starting at index
|
m_locale.erase(index);
|
||||||
// remove everything before =
|
|
||||||
index = m_locale.find('=');
|
index = m_locale.find('=');
|
||||||
if (index != string::npos)
|
if (index != string::npos)
|
||||||
m_locale.erase(0,index+1); //erase all characters starting up to index
|
m_locale.erase(0, index+1);
|
||||||
}
|
}
|
||||||
#endif // defined(HAVE_SETLOCALE) && defined(NLS)
|
#endif // defined(HAVE_SETLOCALE) && defined(NLS)
|
||||||
}
|
}
|
||||||
|
@ -162,7 +238,7 @@ I18n::I18n():m_multibyte(false), m_utf8_translate(false) {
|
||||||
I18n::~I18n() {
|
I18n::~I18n() {
|
||||||
|
|
||||||
#if defined(NLS) && defined(HAVE_CATCLOSE)
|
#if defined(NLS) && defined(HAVE_CATCLOSE)
|
||||||
if (s_catalog_fd != (nl_catd)-1)
|
if (s_catalog_fd != INVALID_CATALOG)
|
||||||
catclose(s_catalog_fd);
|
catclose(s_catalog_fd);
|
||||||
#endif // HAVE_CATCLOSE
|
#endif // HAVE_CATCLOSE
|
||||||
}
|
}
|
||||||
|
@ -178,27 +254,28 @@ I18n& I18n::instance() {
|
||||||
FbString I18n::getMessage(int set_number, int message_number,
|
FbString I18n::getMessage(int set_number, int message_number,
|
||||||
const char *default_message, bool translate_fb) const {
|
const char *default_message, bool translate_fb) const {
|
||||||
|
|
||||||
|
FbString msg(default_message);
|
||||||
|
|
||||||
#if defined(NLS) && defined(HAVE_CATGETS)
|
#if defined(NLS) && defined(HAVE_CATGETS)
|
||||||
if (s_catalog_fd != INVALID_CATALOG) {
|
if (s_catalog_fd != INVALID_CATALOG) {
|
||||||
const char *ret = catgets(s_catalog_fd, set_number, message_number, default_message);
|
const char *ret = catgets(s_catalog_fd, set_number, message_number, default_message);
|
||||||
// can't translate, leave it in raw ascii (utf-8 compatible)
|
|
||||||
if (ret == default_message || ret == NULL)
|
|
||||||
return default_message;
|
|
||||||
|
|
||||||
if (!m_utf8_translate && translate_fb)
|
if (ret == default_message || ret == NULL) {
|
||||||
|
// can't translate, leave it in raw ascii (utf-8 compatible)
|
||||||
|
} else if (!m_utf8_translate && translate_fb)
|
||||||
// Local input, UTF-8 output
|
// Local input, UTF-8 output
|
||||||
return FbStringUtil::LocaleStrToFb(ret);
|
msg = FbStringUtil::LocaleStrToFb(ret);
|
||||||
else if (m_utf8_translate && !translate_fb)
|
else if (m_utf8_translate && !translate_fb)
|
||||||
// UTF-8 input, local output
|
// UTF-8 input, local output
|
||||||
return FbStringUtil::FbStrToLocale(ret);
|
msg = FbStringUtil::FbStrToLocale(ret);
|
||||||
else
|
else
|
||||||
// UTF-8 input, UTF-8 output OR
|
// UTF-8 input, UTF-8 output OR
|
||||||
// local input, local output
|
// local input, local output
|
||||||
return ret;
|
msg = ret;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
#endif // NLS && HAVE_CATGETS
|
#endif // NLS && HAVE_CATGETS
|
||||||
return default_message;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace FbTk
|
} // end namespace FbTk
|
||||||
|
|
|
@ -72,22 +72,25 @@ void FluxboxCli::showInfo(ostream &ostr) {
|
||||||
<<_FB_CONSOLETEXT(Common, Defaults, "Defaults", "Default values compiled in")
|
<<_FB_CONSOLETEXT(Common, Defaults, "Defaults", "Default values compiled in")
|
||||||
<< ": " << endl;
|
<< ": " << endl;
|
||||||
|
|
||||||
ostr <<_FB_CONSOLETEXT(Common, DefaultMenuFile, " menu", "default menu file (right aligned - make sure same width as other default values)")
|
ostr <<_FB_CONSOLETEXT(Common, DefaultMenuFile, " menu", "default menu file (right aligned - make sure same width as other default values)")
|
||||||
<< ": "
|
<< ": "
|
||||||
<< FbTk::StringUtil::expandFilename(DEFAULTMENU) << endl;
|
<< FbTk::StringUtil::expandFilename(DEFAULTMENU) << endl;
|
||||||
ostr << _FB_CONSOLETEXT(Common, DefaultStyle, " style", "default style (right aligned - make sure same width as other default values)")
|
ostr <<_FB_CONSOLETEXT(Common, DefaultWindowMenuFile, " windowmenu", "default windowmenu file (right aligned - make sure same width as other default values)")
|
||||||
|
<< ": "
|
||||||
|
<< FbTk::StringUtil::expandFilename(DEFAULT_WINDOWMENU) << endl;
|
||||||
|
ostr << _FB_CONSOLETEXT(Common, DefaultStyle, " style", "default style (right aligned - make sure same width as other default values)")
|
||||||
<< ": "
|
<< ": "
|
||||||
<< FbTk::StringUtil::expandFilename(DEFAULTSTYLE) << endl;
|
<< FbTk::StringUtil::expandFilename(DEFAULTSTYLE) << endl;
|
||||||
|
|
||||||
ostr << _FB_CONSOLETEXT(Common, DefaultKeyFile, " keys", "default key file (right aligned - make sure same width as other default values)")
|
ostr << _FB_CONSOLETEXT(Common, DefaultKeyFile, " keys", "default key file (right aligned - make sure same width as other default values)")
|
||||||
<< ": "
|
<< ": "
|
||||||
<< FbTk::StringUtil::expandFilename(DEFAULTKEYSFILE) << endl;
|
<< FbTk::StringUtil::expandFilename(DEFAULTKEYSFILE) << endl;
|
||||||
ostr << _FB_CONSOLETEXT(Common, DefaultInitFile, " init", "default init file (right aligned - make sure same width as other default values)")
|
ostr << _FB_CONSOLETEXT(Common, DefaultInitFile, " init", "default init file (right aligned - make sure same width as other default values)")
|
||||||
<< ": "
|
<< ": "
|
||||||
<< FbTk::StringUtil::expandFilename(DEFAULT_INITFILE) << endl;
|
<< FbTk::StringUtil::expandFilename(DEFAULT_INITFILE) << endl;
|
||||||
|
|
||||||
#ifdef NLS
|
#ifdef NLS
|
||||||
ostr << _FB_CONSOLETEXT(Common, DefaultLocalePath, " nls", "location for localization files (right aligned - make sure same width as other default values)")
|
ostr << _FB_CONSOLETEXT(Common, DefaultLocalePath, " nls", "location for localization files (right aligned - make sure same width as other default values)")
|
||||||
<< ": "
|
<< ": "
|
||||||
<< FbTk::StringUtil::expandFilename(LOCALEPATH) << endl;
|
<< FbTk::StringUtil::expandFilename(LOCALEPATH) << endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -32,22 +32,8 @@
|
||||||
#include "FbTk/Command.hh"
|
#include "FbTk/Command.hh"
|
||||||
#include "FbTk/CommandParser.hh"
|
#include "FbTk/CommandParser.hh"
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#include <cstdlib>
|
||||||
#include "config.h"
|
#include <cstring>
|
||||||
#endif // HAVE_CONFIG_H
|
|
||||||
|
|
||||||
#ifdef HAVE_CSTDLIB
|
|
||||||
#include <cstdlib>
|
|
||||||
#else
|
|
||||||
#include <stdlib.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HAVE_CSTRING
|
|
||||||
#include <cstring>
|
|
||||||
#else
|
|
||||||
#include <string.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
|
|
|
@ -158,7 +158,7 @@ void setupSignalHandling() {
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
FbTk::I18n::init("fluxbox.cat");
|
FbTk::I18n::init(0);
|
||||||
|
|
||||||
FluxboxCli::Options opts;
|
FluxboxCli::Options opts;
|
||||||
int exitcode = opts.parse(argc, argv);
|
int exitcode = opts.parse(argc, argv);
|
||||||
|
|
|
@ -370,7 +370,7 @@ int main(int argc, char **argv) {
|
||||||
char *display_name = (char *) 0;
|
char *display_name = (char *) 0;
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|
||||||
FbTk::I18n::init("fluxbox.cat");
|
FbTk::I18n::init(0);
|
||||||
|
|
||||||
for (; i < argc; i++) {
|
for (; i < argc; i++) {
|
||||||
if (!strcmp(argv[i], "-display") || !strcmp(argv[i], "--display")) {
|
if (!strcmp(argv[i], "-display") || !strcmp(argv[i], "--display")) {
|
||||||
|
|
|
@ -565,7 +565,7 @@ int main(int argc, char **argv) {
|
||||||
bool check = 0;
|
bool check = 0;
|
||||||
pid_t fb_pid = 0;
|
pid_t fb_pid = 0;
|
||||||
|
|
||||||
FbTk::I18n::init("fluxbox.cat");
|
FbTk::I18n::init(0);
|
||||||
_FB_USES_NLS;
|
_FB_USES_NLS;
|
||||||
|
|
||||||
for (; i < argc; i++) {
|
for (; i < argc; i++) {
|
||||||
|
|
Loading…
Reference in a new issue