diff options
author | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2015-07-24 11:57:03 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:58:13 +0300 |
commit | f40a697cb7c62cf4b21cb8a31a3f91b7acad7b22 (patch) | |
tree | 7b730684eec4a1a671116f980a8697a65892ef72 /platform | |
parent | 2669ac941490a177e85e6b3ea2b6348817649ca4 (diff) |
Implementation of GetTextById class.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/get_text_by_id.cpp | 54 | ||||
-rw-r--r-- | platform/get_text_by_id.hpp | 23 | ||||
-rw-r--r-- | platform/platform.pro | 2 |
3 files changed, 79 insertions, 0 deletions
diff --git a/platform/get_text_by_id.cpp b/platform/get_text_by_id.cpp new file mode 100644 index 0000000000..5667ee22a4 --- /dev/null +++ b/platform/get_text_by_id.cpp @@ -0,0 +1,54 @@ +#include "platform/get_text_by_id.hpp" +#include "platform/platform.hpp" + +#include "base/logging.hpp" + +#include "3party/jansson/myjansson.hpp" + + +namespace platform +{ +GetTextById::GetTextById(string const & textsDir, string const & localeName) +{ + string const pathToJson = textsDir + "/" + localeName + ".json/localize.json"; + + string jsonBuffer; + ReaderPtr<Reader>(GetPlatform().GetReader(pathToJson)).ReadAsString(jsonBuffer); + + if (jsonBuffer == "") + { + ASSERT(false, ("No json files found at the path", pathToJson)); + return; + } + + my::Json root(jsonBuffer.c_str()); + if (root.get() == nullptr) + { + ASSERT(false, ("Cannot parse the json file found at the path", pathToJson)); + return; + } + + const char * key = nullptr; + json_t * value = nullptr; + json_object_foreach(root.get(), key, value) + { + ASSERT(key, ()); + ASSERT(value, ()); + const char * valueStr = json_string_value(value); + ASSERT(valueStr, ()); + m_localeTexts[key] = valueStr; + } + ASSERT_EQUAL(m_localeTexts.size(), json_object_size(root.get()), ()); +} + +string GetTextById::operator()(string const & textId) const +{ + if (!IsValid()) + return textId; + + auto const textIt = m_localeTexts.find(textId); + if (textIt == m_localeTexts.end()) + return textId; + return textIt->second; +} +} // namespace platform diff --git a/platform/get_text_by_id.hpp b/platform/get_text_by_id.hpp new file mode 100644 index 0000000000..d5dba5bc53 --- /dev/null +++ b/platform/get_text_by_id.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include <std/string.hpp> +#include <std/unordered_map.hpp> + + +namespace platform +{ + +/// GetTextById represents text messages which are saved in textsDir +/// in a specified locale. +class GetTextById +{ +public: + GetTextById(string const & textsDir, string const & localeName); + + bool IsValid() const { return !m_localeTexts.empty(); } + string operator()(string const & textId) const; + +private: + unordered_map<string, string> m_localeTexts; +}; +} // namespace platform diff --git a/platform/platform.pro b/platform/platform.pro index f465596a42..d5d88f6489 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -67,6 +67,7 @@ HEADERS += \ country_defines.hpp \ country_file.hpp \ file_logging.hpp \ + get_text_by_id.hpp \ http_request.hpp \ http_thread_callback.hpp \ local_country_file.hpp \ @@ -84,6 +85,7 @@ SOURCES += \ country_defines.cpp \ country_file.cpp \ file_logging.cpp \ + get_text_by_id.cpp \ http_request.cpp \ local_country_file.cpp \ local_country_file_utils.cpp \ |