From d0515ddc43f141ca8df3b26d935d49b791a612c8 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sat, 13 Jan 2007 09:31:47 +0000 Subject: [PATCH] Messages: - cache_: new cache for gettext translated string. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16662 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/messages.C | 28 +++++++++++++++++++++------- src/messages.h | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/messages.C b/src/messages.C index 90a345470c..13098b8730 100644 --- a/src/messages.C +++ b/src/messages.C @@ -21,7 +21,10 @@ #include #include +#include +using std::endl; +using std::string; namespace lyx { @@ -29,9 +32,6 @@ using support::package; using support::getEnv; using support::setEnv; -using std::string; -using std::endl; - static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$"); @@ -114,10 +114,17 @@ public: ~Pimpl() {} - docstring const get(string const & m) const + docstring const & get(string const & m) const { + static docstring empty_string; if (m.empty()) - return from_ascii(m); + return empty_string; + + // Look for the translated string in the cache. + CacheType::iterator it = cache_.find(m); + if (it != cache_.end()) + return it->second; + // The string was not found, use gettext to generate it: // In this order, see support/filetools.C: string lang = getEnv("LC_ALL"); @@ -206,11 +213,18 @@ public: setlocale(LC_MESSAGES, lang.c_str()); #endif setlocale(LC_CTYPE, oldCTYPE.c_str()); - return translated; + + it = cache_.insert(std::make_pair(m, translated)).first; + return it->second; } private: /// string lang_; + typedef std::map CacheType; + /// Internal cache for gettext translated strings. + /// This is needed for performance reason within \c updateLabels() + /// under Windows. + mutable CacheType cache_; }; #endif @@ -250,7 +264,7 @@ Messages::~Messages() {} -docstring const Messages::get(string const & msg) const +docstring const & Messages::get(string const & msg) const { return pimpl_->get(msg); } diff --git a/src/messages.h b/src/messages.h index 39c7246ecd..8b211eaabe 100644 --- a/src/messages.h +++ b/src/messages.h @@ -29,7 +29,7 @@ public: /// ~Messages(); /// - docstring const get(std::string const & msg) const; + docstring const & get(std::string const & msg) const; private: class Pimpl; boost::scoped_ptr pimpl_; -- 2.39.5