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:
Mathias Gumz 2015-01-31 21:37:44 +01:00
parent 57f21b64ca
commit fff0abad76
6 changed files with 140 additions and 74 deletions

View file

@ -37,6 +37,7 @@
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <iostream>
#ifdef HAVE_LOCALE_H
@ -65,15 +66,54 @@ using std::string;
namespace {
const nl_catd INVALID_CATALOG = ((nl_catd)(-1));
nl_catd s_catalog_fd = INVALID_CATALOG;
const char UTF8_SUFFIX[] = "-UTF-8.cat";
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 {
// 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) {
static bool init = false;
if (init) {
return;
@ -81,50 +121,87 @@ void I18n::init(const char* catalog) {
#if defined(NLS) && defined(HAVE_CATOPEN)
FbStringUtil::init();
I18n& i18n = I18n::instance();
string filename = LOCALEPATH;
filename += '/';
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;
if (!catalog) {
const char* c = getenv(ENV_CATFILE);
if (!c) {
c = DEFAULT_CATFILE;
}
catalog = c;
}
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
s_catalog_fd = catopen(filename.c_str(), MCLoadBySet);
#else // !MCLoadBySet
s_catalog_fd = catopen(filename.c_str(), NL_CAT_LOCALE);
#endif // MCLoadBySet
flag = MCLoadBySet;
#else
flag = NL_CAT_LOCALE;
#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) {
cerr<<"Warning: Failed to open file("<<filename<<")"<<endl
cerr<<"Warning: Failed to open file("<< catalog <<")"<<endl
<<"for translation, using default messages."<<endl;
}
#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)
//make sure we don't get 0 to m_locale string
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
// remove everything after @
string::size_type index = m_locale.find('@');
if (index != string::npos)
m_locale.erase(index); //erase all characters starting at index
// remove everything before =
m_locale.erase(index);
index = m_locale.find('=');
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)
}
@ -162,7 +238,7 @@ I18n::I18n():m_multibyte(false), m_utf8_translate(false) {
I18n::~I18n() {
#if defined(NLS) && defined(HAVE_CATCLOSE)
if (s_catalog_fd != (nl_catd)-1)
if (s_catalog_fd != INVALID_CATALOG)
catclose(s_catalog_fd);
#endif // HAVE_CATCLOSE
}
@ -178,27 +254,28 @@ I18n& I18n::instance() {
FbString I18n::getMessage(int set_number, int message_number,
const char *default_message, bool translate_fb) const {
FbString msg(default_message);
#if defined(NLS) && defined(HAVE_CATGETS)
if (s_catalog_fd != INVALID_CATALOG) {
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
return FbStringUtil::LocaleStrToFb(ret);
msg = FbStringUtil::LocaleStrToFb(ret);
else if (m_utf8_translate && !translate_fb)
// UTF-8 input, local output
return FbStringUtil::FbStrToLocale(ret);
msg = FbStringUtil::FbStrToLocale(ret);
else
// UTF-8 input, UTF-8 output OR
// local input, local output
return ret;
msg = ret;
}
else
#endif // NLS && HAVE_CATGETS
return default_message;
return msg;
}
} // end namespace FbTk

View file

@ -72,22 +72,25 @@ void FluxboxCli::showInfo(ostream &ostr) {
<<_FB_CONSOLETEXT(Common, Defaults, "Defaults", "Default values compiled in")
<< ": " << 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;
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;
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;
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;
#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;
#endif

View file

@ -32,22 +32,8 @@
#include "FbTk/Command.hh"
#include "FbTk/CommandParser.hh"
#ifdef HAVE_CONFIG_H
#include "config.h"
#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 <cstdlib>
#include <cstring>
#include <iostream>
using std::cerr;

View file

@ -158,7 +158,7 @@ void setupSignalHandling() {
int main(int argc, char **argv) {
FbTk::I18n::init("fluxbox.cat");
FbTk::I18n::init(0);
FluxboxCli::Options opts;
int exitcode = opts.parse(argc, argv);

View file

@ -370,7 +370,7 @@ int main(int argc, char **argv) {
char *display_name = (char *) 0;
int i = 1;
FbTk::I18n::init("fluxbox.cat");
FbTk::I18n::init(0);
for (; i < argc; i++) {
if (!strcmp(argv[i], "-display") || !strcmp(argv[i], "--display")) {

View file

@ -565,7 +565,7 @@ int main(int argc, char **argv) {
bool check = 0;
pid_t fb_pid = 0;
FbTk::I18n::init("fluxbox.cat");
FbTk::I18n::init(0);
_FB_USES_NLS;
for (; i < argc; i++) {