From ea060b758117627b5629e27c5992693cbe100455 Mon Sep 17 00:00:00 2001 From: auria Date: Thu, 17 Mar 2011 22:34:25 +0000 Subject: [PATCH] Add fallback support in tinygettext, fixing languages like fr_CA, which relies on fr git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@7981 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/tinygettext/dictionary.cpp | 5 ++++- src/tinygettext/dictionary.hpp | 9 +++++++++ src/tinygettext/dictionary_manager.cpp | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/tinygettext/dictionary.cpp b/src/tinygettext/dictionary.cpp index ccd18c1e0..251daff78 100644 --- a/src/tinygettext/dictionary.cpp +++ b/src/tinygettext/dictionary.cpp @@ -27,6 +27,7 @@ Dictionary::Dictionary(const std::string& charset_) : charset(charset_), plural_forms() { + m_has_fallback = false; } Dictionary::~Dictionary() @@ -108,7 +109,9 @@ Dictionary::translate(const Entries& dict, const std::string& msgid) else { //log_info << "Couldn't translate: " << msgid << std::endl; - return msgid; + + if (m_has_fallback) return m_fallback->translate(msgid); + else return msgid; } } diff --git a/src/tinygettext/dictionary.hpp b/src/tinygettext/dictionary.hpp index 743e075a0..668b35e92 100644 --- a/src/tinygettext/dictionary.hpp +++ b/src/tinygettext/dictionary.hpp @@ -43,6 +43,9 @@ private: std::string translate(const Entries& dict, const std::string& msgid); std::string translate_plural(const Entries& dict, const std::string& msgid, const std::string& msgidplural, int num); + bool m_has_fallback; + Dictionary* m_fallback; + public: /** Constructs a dictionary converting to the specified \a charset (default UTF-8) */ Dictionary(const std::string& charset = "UTF-8"); @@ -100,6 +103,12 @@ public: return func; } + void addFallback(Dictionary* fallback) + { + m_has_fallback = true; + m_fallback = fallback; + } + /** Iterate over all messages with a context, Func is of type: void func(const std::string& ctxt, const std::string& msgid, const std::vector& msgstrs) */ template diff --git a/src/tinygettext/dictionary_manager.cpp b/src/tinygettext/dictionary_manager.cpp index b686ebd55..a17802cc0 100644 --- a/src/tinygettext/dictionary_manager.cpp +++ b/src/tinygettext/dictionary_manager.cpp @@ -164,6 +164,11 @@ DictionaryManager::get_dictionary(const Language& language) } } + if (language.get_country().size() > 0) + { + printf("Adding language fallback %s\n", language.get_language().c_str()); + dict->addFallback( &get_dictionary(Language::from_spec(language.get_language())) ); + } return *dict; } }