X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Ftinygettext%2Ftinygettext.cpp;h=ffaaf710da3c97c1271039980d99abb2e8ccfb43;hb=5ee161985d711c1cbe18b51b6db0076e34ddf613;hp=d51f627380af9bb78e115412dcdda2c669550df7;hpb=db06d3f57856bc82edbee52016858e3004189584;p=supertux.git diff --git a/src/tinygettext/tinygettext.cpp b/src/tinygettext/tinygettext.cpp index d51f62738..ffaaf710d 100644 --- a/src/tinygettext/tinygettext.cpp +++ b/src/tinygettext/tinygettext.cpp @@ -20,16 +20,17 @@ #include #include -#include #include #include +#include #include #include +#include "SDL.h" + #include "tinygettext.hpp" #include "log.hpp" #include "physfs/physfs_stream.hpp" -#include "log.hpp" #include "findlocale.hpp" //#define TRANSLATION_DEBUG @@ -44,8 +45,21 @@ std::string convert(const std::string& text, if (from_charset == to_charset) return text; - iconv_t cd = iconv_open(to_charset.c_str(), from_charset.c_str()); - + char *in = new char[text.length() + 1]; + strcpy(in, text.c_str()); + char *out = SDL_iconv_string(to_charset.c_str(), from_charset.c_str(), in, text.length() + 1); + delete[] in; + if(out == 0) + { + log_warning << "Error: conversion from " << from_charset << " to " << to_charset << " failed" << std::endl; + return ""; + } + std::string ret(out); + SDL_free(out); + return ret; +#if 0 + iconv_t cd = SDL_iconv_open(to_charset.c_str(), from_charset.c_str()); + size_t in_len = text.length(); size_t out_len = text.length()*3; // FIXME: cross fingers that this is enough @@ -58,7 +72,7 @@ std::string convert(const std::string& text, size_t out_len_temp = out_len; // iconv is counting down the bytes it has // written from this... - size_t retval = iconv(cd, &in, &in_len, &out, &out_len_temp); + size_t retval = SDL_iconv(cd, &in, &in_len, &out, &out_len_temp); out_len -= out_len_temp; // see above if (retval == (size_t) -1) { @@ -66,12 +80,13 @@ std::string convert(const std::string& text, log_warning << "Error: conversion from " << from_charset << " to " << to_charset << " went wrong: " << retval << std::endl; return ""; } - iconv_close(cd); + SDL_iconv_close(cd); std::string ret(out_orig, out_len); delete[] out_orig; delete[] in_orig; return ret; +#endif } bool has_suffix(const std::string& lhs, const std::string rhs) @@ -174,7 +189,7 @@ get_language_def(const std::string& name) else if (name == "sk") return lang_sk; else if (name == "pl") return lang_pl; else if (name == "sl") return lang_sl; - else return lang_en; + else return lang_en; } DictionaryManager::DictionaryManager() @@ -186,12 +201,17 @@ DictionaryManager::DictionaryManager() if( lang ){ set_language( lang ); return; - } + } // use findlocale to setup language FL_Locale *locale; FL_FindLocale( &locale, FL_MESSAGES ); - if( locale->lang) - set_language( locale->lang ); + if(locale->lang) { + if (locale->country) { + set_language( std::string(locale->lang)+"_"+std::string(locale->country) ); + } else { + set_language( std::string(locale->lang) ); + } + } FL_FreeLocale( &locale ); } @@ -200,27 +220,27 @@ DictionaryManager::parseLocaleAliases() { // try to parse language alias list std::ifstream in("/usr/share/locale/locale.alias"); - + char c = ' '; while(in.good() && !in.eof()) { - while(isspace(c) && !in.eof()) + while(isspace(static_cast(c)) && !in.eof()) in.get(c); - + if(c == '#') { // skip comments while(c != '\n' && !in.eof()) in.get(c); continue; } - + std::string alias; - while(!isspace(c) && !in.eof()) { + while(!isspace(static_cast(c)) && !in.eof()) { alias += c; in.get(c); } - while(isspace(c) && !in.eof()) + while(isspace(static_cast(c)) && !in.eof()) in.get(c); std::string language; - while(!isspace(c) && !in.eof()) { + while(!isspace(static_cast(c)) && !in.eof()) { language += c; in.get(c); } @@ -230,11 +250,17 @@ DictionaryManager::parseLocaleAliases() set_language_alias(alias, language); } } - + Dictionary& DictionaryManager::get_dictionary(const std::string& spec) { + + //log_debug << "Dictionary for language \"" << spec << "\" requested" << std::endl; + std::string lang = get_language_from_spec(spec); + + //log_debug << "...normalized as \"" << lang << "\"" << std::endl; + Dictionaries::iterator i = dictionaries.find(get_language_from_spec(lang)); if (i != dictionaries.end()) { @@ -252,7 +278,7 @@ DictionaryManager::get_dictionary(const std::string& spec) for (SearchPath::iterator p = search_path.begin(); p != search_path.end(); ++p) { char** files = PHYSFS_enumerateFiles(p->c_str()); - if(!files) + if(!files) { log_warning << "Error: enumerateFiles() failed on " << *p << std::endl; } @@ -260,7 +286,25 @@ DictionaryManager::get_dictionary(const std::string& spec) { for(const char* const* filename = files; *filename != 0; filename++) { - if(std::string(*filename) == lang + ".po") { + + // check if filename matches requested language + std::string fname = std::string(*filename); + std::string load_from_file = ""; + if(fname == lang + ".po") { + load_from_file = fname; + } else { + std::string::size_type s = lang.find("_"); + if(s != std::string::npos) { + std::string lang_short = std::string(lang, 0, s); + if (fname == lang_short + ".po") { + load_from_file = lang_short; + } + } + } + + // if it matched, load dictionary + if (load_from_file != "") { + //log_debug << "Loading dictionary for language \"" << lang << "\" from \"" << filename << "\"" << std::endl; std::string pofile = *p + "/" + *filename; try { IFileStream in(pofile); @@ -270,6 +314,7 @@ DictionaryManager::get_dictionary(const std::string& spec) log_warning << e.what() << "" << std::endl; } } + } PHYSFS_freeList(files); } @@ -301,14 +346,16 @@ DictionaryManager::get_languages() } PHYSFS_freeList(files); } - } + } return languages; } void DictionaryManager::set_language(const std::string& lang) { + //log_debug << "set_language \"" << lang << "\"" << std::endl; language = get_language_from_spec(lang); + //log_debug << "==> \"" << language << "\"" << std::endl; current_dict = & (get_dictionary(language)); } @@ -341,12 +388,21 @@ DictionaryManager::get_language_from_spec(const std::string& spec) if(i != language_aliases.end()) { lang = i->second; } - - std::string::size_type s = lang.find_first_of("_."); - if(s == std::string::npos) - return lang; - return std::string(lang, 0, s); + std::string::size_type s = lang.find("."); + if(s != std::string::npos) { + lang = std::string(lang, 0, s); + } + + s = lang.find("_"); + if(s == std::string::npos) { + std::string lang_big = lang; + std::transform (lang_big.begin(), lang_big.end(), lang_big.begin(), toupper); + lang += "_" + lang_big; + } + + return lang; + } void @@ -388,7 +444,7 @@ Dictionary::set_language(const LanguageDef& lang) } std::string -Dictionary::translate(const std::string& msgid, const std::string& msgid2, int num) +Dictionary::translate(const std::string& msgid, const std::string& msgid2, int num) { PluralEntries::iterator i = plural_entries.find(msgid); std::map& msgstrs = i->second; @@ -441,7 +497,7 @@ Dictionary::translate(const char* msgid) } std::string -Dictionary::translate(const std::string& msgid) +Dictionary::translate(const std::string& msgid) { Entries::iterator i = entries.find(msgid); if (i != entries.end() && !i->second.empty()) @@ -456,7 +512,7 @@ Dictionary::translate(const std::string& msgid) return msgid; } } - + void Dictionary::add_translation(const std::string& msgid, const std::string& , const std::map& msgstrs) @@ -466,8 +522,8 @@ Dictionary::add_translation(const std::string& msgid, const std::string& , plural_entries[msgid] = msgstrs; } -void -Dictionary::add_translation(const std::string& msgid, const std::string& msgstr) +void +Dictionary::add_translation(const std::string& msgid, const std::string& msgstr) { entries[msgid] = msgstr; } @@ -515,7 +571,7 @@ public: // Seperate the header in lines typedef std::vector Lines; Lines lines; - + std::string::size_type start = 0; for(std::string::size_type i = 0; i < header.length(); ++i) { @@ -549,10 +605,10 @@ public: void add_token(const Token& token) { - switch(state) + switch(state) { case WANT_MSGID: - if (token.keyword == "msgid") + if (token.keyword == "msgid") { current_msgid = token.content; state = WANT_MSGID_PLURAL; @@ -566,13 +622,13 @@ public: log_warning << "tinygettext: expected 'msgid' keyword, got " << token.keyword << " at line " << line_num << std::endl; } break; - + case WANT_MSGID_PLURAL: - if (token.keyword == "msgid_plural") + if (token.keyword == "msgid_plural") { current_msgid_plural = token.content; state = WANT_MSGSTR_PLURAL; - } + } else { state = WANT_MSGSTR; @@ -581,9 +637,9 @@ public: break; case WANT_MSGSTR: - if (token.keyword == "msgstr") + if (token.keyword == "msgstr") { - if (current_msgid == "") + if (current_msgid == "") { // .po Header is hidden in the msgid with the empty string parse_header(token.content); } @@ -592,7 +648,7 @@ public: dict.add_translation(current_msgid, convert(token.content, from_charset, to_charset)); } state = WANT_MSGID; - } + } else { log_warning << "tinygettext: expected 'msgstr' keyword, got " << token.keyword << " at line " << line_num << std::endl; @@ -600,19 +656,19 @@ public: break; case WANT_MSGSTR_PLURAL: - if (has_prefix(token.keyword, "msgstr[")) + if (has_prefix(token.keyword, "msgstr[")) { int num; - if (sscanf(token.keyword.c_str(), "msgstr[%d]", &num) != 1) + if (sscanf(token.keyword.c_str(), "msgstr[%d]", &num) != 1) { log_warning << "Error: Couldn't parse: " << token.keyword << std::endl; - } - else + } + else { msgstr_plural[num] = convert(token.content, from_charset, to_charset); } } - else + else { dict.add_translation(current_msgid, current_msgid_plural, msgstr_plural); @@ -622,18 +678,18 @@ public: break; } } - - inline int getchar(std::istream& in) + + inline int getchar(std::istream& in) { int c = in.get(); if (c == '\n') line_num += 1; return c; } - + void tokenize_po(std::istream& in) { - enum State { READ_KEYWORD, + enum State { READ_KEYWORD, READ_CONTENT, READ_CONTENT_IN_STRING, SKIP_COMMENT }; @@ -652,14 +708,17 @@ public: { state = SKIP_COMMENT; } + else if (c == '\n') + { + } else { // Read a new token token = Token(); - - do { // Read keyword + + do { // Read keyword token.keyword += c; - } while((c = getchar(in)) != EOF && !isspace(c)); + } while((c = getchar(in)) != EOF && !isspace(static_cast(c))); in.unget(); state = READ_CONTENT; @@ -669,16 +728,17 @@ public: case READ_CONTENT: while((c = getchar(in)) != EOF) { - if (c == '"') { + if (c == '"') { // Found start of content state = READ_CONTENT_IN_STRING; break; - } else if (isspace(c)) { + } else if (isspace(static_cast(c))) { // skip } else { // Read something that may be a keyword in.unget(); state = READ_KEYWORD; add_token(token); + token = Token(); break; } } @@ -693,6 +753,7 @@ public: else if (c == 't') token.content += '\t'; else if (c == 'r') token.content += '\r'; else if (c == '"') token.content += '"'; + else if (c == '\\') token.content += '\\'; else { log_warning << "Unhandled escape character: " << char(c) << std::endl; @@ -716,10 +777,11 @@ public: } } add_token(token); + token = Token(); } }; -void read_po_file(Dictionary& dict_, std::istream& in) +void read_po_file(Dictionary& dict_, std::istream& in) { POFileReader reader(in, dict_); }