diff options
| author | Adam <Adam@anope.org> | 2011-06-17 19:57:43 -0400 |
|---|---|---|
| committer | Adam <Adam@anope.org> | 2011-06-17 19:57:43 -0400 |
| commit | a1b36ec0a527e6d7a9d224b56bf9571619d5f92e (patch) | |
| tree | 5e74290f46205ef1ad86700e1f04ec016b1f66fd /src | |
| parent | 48e995ddf2cd30a05222a51e665c02b1e7ae567a (diff) | |
Search all domains for language strings, fixes the mess that we used to use to translate strings in 3rd party modules
Diffstat (limited to 'src')
| -rw-r--r-- | src/botserv.cpp | 6 | ||||
| -rw-r--r-- | src/command.cpp | 7 | ||||
| -rw-r--r-- | src/commands.cpp | 13 | ||||
| -rw-r--r-- | src/language.cpp | 58 | ||||
| -rw-r--r-- | src/misc.cpp | 25 | ||||
| -rw-r--r-- | src/module.cpp | 17 | ||||
| -rw-r--r-- | src/modules.cpp | 22 | ||||
| -rw-r--r-- | src/regchannel.cpp | 4 | ||||
| -rw-r--r-- | src/users.cpp | 5 |
9 files changed, 61 insertions, 96 deletions
diff --git a/src/botserv.cpp b/src/botserv.cpp index 48b355f24..9d7c38817 100644 --- a/src/botserv.cpp +++ b/src/botserv.cpp @@ -51,7 +51,7 @@ void bot_raw_ban(User *requester, ChannelInfo *ci, User *u, const Anope::string if (ModeManager::FindUserModeByName(UMODE_PROTECTED) && u->IsProtected() && requester != u) { - ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", GetString(requester->Account(), ACCESS_DENIED).c_str()); + ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", translate(requester, ACCESS_DENIED)); return; } @@ -62,7 +62,7 @@ void bot_raw_ban(User *requester, ChannelInfo *ci, User *u, const Anope::string if (matches_list(ci->c, u, CMODE_EXCEPT)) { - ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", GetString(requester->Account(), gtl("User matches channel except.")).c_str()); + ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", translate(requester, _("User matches channel except."))); return; } @@ -93,7 +93,7 @@ void bot_raw_kick(User *requester, ChannelInfo *ci, User *u, const Anope::string if (ModeManager::FindUserModeByName(UMODE_PROTECTED) && u->IsProtected() && requester != u) { - ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", GetString(requester->Account(), ACCESS_DENIED).c_str()); + ircdproto->SendPrivmsg(ci->bi, ci->name, "%s", translate(requester, ACCESS_DENIED)); return; } diff --git a/src/command.cpp b/src/command.cpp index 04295bbe6..cb39ba926 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -29,7 +29,10 @@ void CommandSource::Reply(const Anope::string &message) sepstream sep(message, '\n'); Anope::string tok; while (sep.GetToken(tok)) - this->reply.push_back(tok); + { + const char *translated_message = translate(this->u, tok.c_str()); + this->reply.push_back(translated_message); + } } void CommandSource::DoReply() @@ -76,7 +79,7 @@ const Anope::string &Command::GetDesc() const void Command::OnServHelp(CommandSource &source) { - source.Reply(" %-14s %s", this->name.c_str(), _(this->GetDesc().c_str())); + source.Reply(" %-14s %s", this->name.c_str(), translate(source.u, (this->GetDesc().c_str()))); } bool Command::OnHelp(CommandSource &source, const Anope::string &subcommand) { return false; } diff --git a/src/commands.cpp b/src/commands.cpp index 194547a58..fea6e4376 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -53,21 +53,17 @@ void mod_run_cmd(BotInfo *bi, User *u, ChannelInfo *ci, Command *c, const Anope: if (!bi || !u) return; - PushLanguage("anope", u->Account() ? u->Account()->language : ""); - if (!c) { u->SendMessage(bi, _("Unknown command \002%s\002. \"%s%s HELP\" for help."), command.c_str(), Config->UseStrictPrivMsgString.c_str(), bi->nick.c_str()); - PopLanguage(); return; } // Command requires registered users only if (!c->HasFlag(CFLAG_ALLOW_UNREGISTERED) && !u->IsIdentified()) { - u->SendMessage(bi, _(NICK_IDENTIFY_REQUIRED), Config->UseStrictPrivMsgString.c_str(), Config->s_NickServ.c_str()); + u->SendMessage(bi, NICK_IDENTIFY_REQUIRED, Config->UseStrictPrivMsgString.c_str(), Config->s_NickServ.c_str()); Log(LOG_COMMAND, "denied", bi) << "Access denied for unregistered user " << u->GetMask() << " with command " << command; - PopLanguage(); return; } @@ -107,7 +103,6 @@ void mod_run_cmd(BotInfo *bi, User *u, ChannelInfo *ci, Command *c, const Anope: { c->OnSyntaxError(source, !params.empty() ? params[params.size() - 1] : ""); source.DoReply(); - PopLanguage(); return; } @@ -116,17 +111,15 @@ void mod_run_cmd(BotInfo *bi, User *u, ChannelInfo *ci, Command *c, const Anope: if (MOD_RESULT == EVENT_STOP) { source.DoReply(); - PopLanguage(); return; } // If the command requires a permission, and they aren't registered or don't have the required perm, DENIED if (!c->permission.empty() && !u->HasCommand(c->permission)) { - u->SendMessage(bi, _(ACCESS_DENIED)); + u->SendMessage(bi, ACCESS_DENIED); Log(LOG_COMMAND, "denied", bi) << "Access denied for user " << u->GetMask() << " with command " << command; source.DoReply(); - PopLanguage(); return; } @@ -137,8 +130,6 @@ void mod_run_cmd(BotInfo *bi, User *u, ChannelInfo *ci, Command *c, const Anope: FOREACH_MOD(I_OnPostCommand, OnPostCommand(source, c, params)); source.DoReply(); } - - PopLanguage(); } /** diff --git a/src/language.cpp b/src/language.cpp index 59df4c7dc..4e1b94e02 100644 --- a/src/language.cpp +++ b/src/language.cpp @@ -2,6 +2,7 @@ #include <stack> std::vector<Anope::string> languages; +std::vector<Anope::string> domains; void InitLanguages() { @@ -34,49 +35,34 @@ void InitLanguages() #endif } -const Anope::string GetString(NickCore *nc, const char *string) +const char *translate(const char *string) { - return GetString("anope", nc ? nc->language : Config->NSDefLanguage, string); + return anope_gettext(Config->NSDefLanguage.c_str(), string); } -const Anope::string GetString(const Anope::string &domain, const Anope::string &lang, const char *string) +const char *translate(User *u, const char *string) { - PushLanguage(domain, lang); - const char *t_string = anope_gettext(string); - PopLanguage(); - return t_string; + return translate(u ? u->Account() : NULL, string); } -#if GETTEXT_FOUND -static std::stack<std::pair<Anope::string, Anope::string > > language_stack; - -void PushLanguage(const Anope::string &domain, Anope::string language) +const char *translate(NickCore *nc, const char *string) { - if (language.empty() && !Config->NSDefLanguage.empty()) - language = Config->NSDefLanguage; - - language_stack.push(std::make_pair(domain, language)); + return anope_gettext(nc ? nc->language.c_str() : Config->NSDefLanguage.c_str(), string); } -void PopLanguage() -{ - language_stack.pop(); -} +#if GETTEXT_FOUND /* Used by gettext to make it always dynamically load language strings (so we can drop them in while Anope is running) */ extern "C" int _nl_msg_cat_cntr; -const char *anope_gettext(const char *string) +const char *anope_gettext(const char *lang, const char *string) { - std::pair<Anope::string, Anope::string> lang_info; - if (!language_stack.empty()) - lang_info = language_stack.top(); - if (*string == 0 || lang_info.first.empty() || lang_info.second.empty()) - return string; + if (!string || !*string || !lang || !*lang) + return string ? string : ""; ++_nl_msg_cat_cntr; #ifdef _WIN32 - SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang_info.second.c_str()), SUBLANG_DEFAULT), SORT_DEFAULT)); + SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT)); #else /* First, set LANGUAGE env variable. * Some systems (Debian) don't care about this, so we must setlocale LC_ALL aswell. @@ -85,11 +71,13 @@ const char *anope_gettext(const char *string) * use the LANGUAGE env variable. We must reset the locale to en_US (or, anything not * C or POSIX) then. */ - setenv("LANGUAGE", lang_info.second.c_str(), 1); - if (setlocale(LC_ALL, lang_info.second.c_str()) == NULL) + setenv("LANGUAGE", lang, 1); + if (setlocale(LC_ALL, lang) == NULL) setlocale(LC_ALL, "en_US"); #endif - const char *translated_string = dgettext(lang_info.first.c_str(), string); + const char *translated_string = dgettext("anope", string); + for (unsigned i = 0; translated_string == string && i < domains.size(); ++i) + translated_string = dgettext(domains[i].c_str(), string); #ifdef _WIN32 SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT)); #else @@ -97,18 +85,10 @@ const char *anope_gettext(const char *string) setlocale(LC_ALL, ""); #endif - return translated_string != NULL ? translated_string : ""; + return translated_string; } #else -void PushLanguage(const Anope::string &, Anope::string) -{ -} - -void PopLanguage() -{ -} - -const char *anope_gettext(const char *string) +const char *anope_gettext(const char *lang, const char *string) { return string != NULL ? string : ""; } diff --git a/src/misc.cpp b/src/misc.cpp index 6b88a5846..73791a0cc 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -212,26 +212,26 @@ Anope::string duration(const time_t &t, NickCore *nc) time_t seconds = (t) % 60; if (!days && !hours && !minutes) - return stringify(seconds) + " " + (seconds != 1 ? GetString(nc, gtl("seconds")) : GetString(nc, gtl("second"))); + return stringify(seconds) + " " + (seconds != 1 ? translate(nc, _("seconds")) : translate(nc, _("second"))); else { bool need_comma = false; Anope::string buffer; if (days) { - buffer = stringify(days) + " " + (days != 1 ? GetString(nc, gtl("days")) : GetString(nc, gtl("day"))); + buffer = stringify(days) + " " + (days != 1 ? translate(nc, _("days")) : translate(nc, _("day"))); need_comma = true; } if (hours) { buffer += need_comma ? ", " : ""; - buffer += stringify(hours) + " " + (hours != 1 ? GetString(nc, gtl("hours")) : GetString(nc, gtl("hour"))); + buffer += stringify(hours) + " " + (hours != 1 ? translate(nc, _("hounslaters")) : translate(nc, _("hour"))); need_comma = true; } if (minutes) { buffer += need_comma ? ", " : ""; - buffer += stringify(minutes) + " " + (minutes != 1 ? GetString(nc, gtl("minutes")) : GetString(nc, gtl("minute"))); + buffer += stringify(minutes) + " " + (minutes != 1 ? translate(nc, _("minutes")) : translate(nc, _("minute"))); } return buffer; } @@ -240,14 +240,13 @@ Anope::string duration(const time_t &t, NickCore *nc) Anope::string do_strftime(const time_t &t, NickCore *nc, bool short_output) { tm tm = *localtime(&t); - char buf[BUFSIZE]; - strftime(buf, sizeof(buf), GetString(nc, gtl("%b %d %H:%M:%S %Y %Z")).c_str(), &tm); + const char *buf = translate(nc, _("%b %d %H:%M:%S %Y %Z")); if (short_output) return buf; if (t < Anope::CurTime) - return Anope::string(buf) + " " + Anope::printf(GetString(nc, gtl("(%s ago)")).c_str(), duration(Anope::CurTime - t).c_str(), nc); + return Anope::string(buf) + " " + Anope::printf(translate(nc, _("(%s ago)")), duration(Anope::CurTime - t).c_str(), nc); else - return Anope::string(buf) + " " + Anope::printf(GetString(nc, gtl("(%s from now)")).c_str(), duration(t - Anope::CurTime).c_str(), nc); + return Anope::string(buf) + " " + Anope::printf(translate(nc, _("(%s from now)")), duration(t - Anope::CurTime).c_str(), nc); } /*************************************************************************/ @@ -261,9 +260,9 @@ Anope::string do_strftime(const time_t &t, NickCore *nc, bool short_output) Anope::string expire_left(NickCore *nc, time_t expires) { if (!expires) - return GetString(nc, NO_EXPIRE); + return translate(nc, NO_EXPIRE); else if (expires <= Anope::CurTime) - return GetString(nc, gtl("expires at next database update")); + return translate(nc, _("expires at next database update")); else { char buf[256]; @@ -272,21 +271,21 @@ Anope::string expire_left(NickCore *nc, time_t expires) if (diff >= 86400) { int days = diff / 86400; - snprintf(buf, sizeof(buf), GetString(nc, days == 1 ? gtl("expires in %d day") : gtl("expires in %d days")).c_str(), days); + snprintf(buf, sizeof(buf), translate(nc, days == 1 ? _("expires in %d day") : _("expires in %d days")), days); } else { if (diff <= 3600) { int minutes = diff / 60; - snprintf(buf, sizeof(buf), GetString(nc, minutes == 1 ? gtl("expires in %d minute") : gtl("expires in %d minutes")).c_str(), minutes); + snprintf(buf, sizeof(buf), translate(nc, minutes == 1 ? _("expires in %d minute") : _("expires in %d minutes")), minutes); } else { int hours = diff / 3600, minutes; diff -= hours * 3600; minutes = diff / 60; - snprintf(buf, sizeof(buf), GetString(nc, hours == 1 && minutes == 1 ? gtl("expires in %d hour, %d minute") : (hours == 1 && minutes != 1 ? gtl("expires in %d hour, %d minutes") : (hours != 1 && minutes == 1 ? gtl("expires in %d hours, %d minute") : gtl("expires in %d hours, %d minutes")))).c_str(), hours, minutes); + snprintf(buf, sizeof(buf), translate(nc, hours == 1 && minutes == 1 ? _("expires in %d hour, %d minute") : (hours == 1 && minutes != 1 ? _("expires in %d hour, %d minutes") : (hours != 1 && minutes == 1 ? _("expires in %d hours, %d minute") : _("expires in %d hours, %d minutes")))), hours, minutes); } } diff --git a/src/module.cpp b/src/module.cpp index 929984e2e..7ec6892ac 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -28,8 +28,15 @@ Module::Module(const Anope::string &modname, const Anope::string &, ModType modt Modules.push_back(this); #if GETTEXT_FOUND - if (!bindtextdomain(this->name.c_str(), (services_dir + "/languages/").c_str())) - Log() << "Error calling bindtextdomain, " << Anope::LastError(); + for (unsigned i = 0; i < languages.size(); ++i) + if (IsFile("languages/" + languages[i] + "/LC_MESSAGES/" + modname + ".mo")) + { + if (!bindtextdomain(this->name.c_str(), (services_dir + "/languages/").c_str())) + Log() << "Error calling bindtextdomain, " << Anope::LastError(); + else + domains.push_back(modname); + break; + } #endif } @@ -45,6 +52,12 @@ Module::~Module() std::list<Module *>::iterator it = std::find(Modules.begin(), Modules.end(), this); if (it != Modules.end()) Modules.erase(it); + +#if GETTEXT_FOUND + std::vector<Anope::string>::iterator dit = std::find(domains.begin(), domains.end(), this->name); + if (dit != domains.end()) + domains.erase(dit); +#endif } void Module::SetPermanent(bool state) diff --git a/src/modules.cpp b/src/modules.cpp index f77b28676..c379a66dd 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -108,28 +108,6 @@ int Module::DelCommand(BotInfo *bi, Command *c) return MOD_ERR_OK; } -void Module::SendMessage(CommandSource &source, const char *fmt, ...) -{ - Anope::string language = (source.u && source.u->Account() ? source.u->Account()->language : ""); - - PushLanguage(this->name, language); - fmt = anope_gettext(fmt); - - va_list args; - char buf[4096]; - va_start(args, fmt); - vsnprintf(buf, sizeof(buf) - 1, fmt, args); - va_end(args); - - sepstream sep(buf, '\n'); - Anope::string token; - - while (sep.GetToken(token)) - source.Reply(token); - - PopLanguage(); -} - Service::Service(Module *o, const Anope::string &n) : owner(o), name(n) { } diff --git a/src/regchannel.cpp b/src/regchannel.cpp index b76c85b64..cf2cb42df 100644 --- a/src/regchannel.cpp +++ b/src/regchannel.cpp @@ -793,7 +793,7 @@ bool ChannelInfo::CheckKick(User *user) if (!user->HasMode(UMODE_OPER) && this->HasFlag(CI_SUSPENDED)) { get_idealban(this, user, mask); - reason = GetString(user->Account(), gtl("This channel may not be used.")); + reason = translate(user, _("This channel may not be used.")); set_modes = true; do_kick = true; } @@ -837,7 +837,7 @@ bool ChannelInfo::CheckKick(User *user) if (!do_kick && check_access(user, this, CA_NOJOIN)) { get_idealban(this, user, mask); - reason = GetString(user->Account(), CHAN_NOT_ALLOWED_TO_JOIN); + reason = translate(user, CHAN_NOT_ALLOWED_TO_JOIN); do_kick = true; } diff --git a/src/users.cpp b/src/users.cpp index fe947d89a..6f676e79b 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -243,10 +243,11 @@ void User::SendMessage(BotInfo *source, Anope::string msg) Anope::string tok; while (sep.GetToken(tok)) { + const char *translated_message = translate(this, tok.c_str()); if (Config->UsePrivmsg && ((!this->nc && Config->NSDefFlags.HasFlag(NI_MSG)) || (this->nc && this->nc->HasFlag(NI_MSG)))) - ircdproto->SendPrivmsg(source, this->nick, "%s", tok.c_str()); + ircdproto->SendPrivmsg(source, this->nick, "%s", translated_message); else - ircdproto->SendNotice(source, this->nick, "%s", tok.c_str()); + ircdproto->SendNotice(source, this->nick, "%s", translated_message); } } |
