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(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 +#include + + +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 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 \