Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--platform/get_text_by_id.cpp54
-rw-r--r--platform/get_text_by_id.hpp23
-rw-r--r--platform/platform.pro2
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 \