forked from organicmaps/organicmaps
Cuisines parsing and localization wrapper.
This commit is contained in:
parent
2dd4c4c38a
commit
03d6b46db2
3 changed files with 97 additions and 0 deletions
60
indexer/cuisines.cpp
Normal file
60
indexer/cuisines.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include "indexer/cuisines.hpp"
|
||||
|
||||
#include "base/stl_add.hpp"
|
||||
#include "base/string_utils.hpp"
|
||||
|
||||
#include "std/utility.hpp"
|
||||
|
||||
namespace osm
|
||||
{
|
||||
// static
|
||||
Cuisines & Cuisines::Instance()
|
||||
{
|
||||
static Cuisines instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
void InitializeCuisinesForLocale(platform::TGetTextByIdPtr & ptr, string const & lang)
|
||||
{
|
||||
if (!ptr || ptr->GetLocale() != lang)
|
||||
ptr = GetTextByIdFactory(platform::TextSource::Cuisines, lang);
|
||||
CHECK(ptr, ("Error loading cuisines translations for", lang, "language."));
|
||||
}
|
||||
|
||||
string TranslateImpl(platform::TGetTextByIdPtr const & ptr, string const & key)
|
||||
{
|
||||
ASSERT(ptr, ("ptr should be initialized before calling this function."));
|
||||
return ptr->operator()(key);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Cuisines::Parse(string const & osmRawCuisinesTagValue, vector<string> & outCuisines)
|
||||
{
|
||||
strings::Tokenize(osmRawCuisinesTagValue, ";", MakeBackInsertFunctor(outCuisines));
|
||||
}
|
||||
|
||||
void Cuisines::ParseAndLocalize(string const & osmRawCuisinesTagValue, vector<string> & outCuisines,
|
||||
string const & lang)
|
||||
{
|
||||
Parse(osmRawCuisinesTagValue, outCuisines);
|
||||
InitializeCuisinesForLocale(m_translations, lang);
|
||||
for (auto & cuisine : outCuisines)
|
||||
{
|
||||
string tr = TranslateImpl(m_translations, cuisine);
|
||||
if (!tr.empty())
|
||||
cuisine = move(tr);
|
||||
}
|
||||
}
|
||||
|
||||
string Cuisines::Translate(string const & singleOsmCuisine, string const & lang)
|
||||
{
|
||||
ASSERT(singleOsmCuisine.find(';') == string::npos,
|
||||
("Please call Parse method for raw OSM cuisine string."));
|
||||
InitializeCuisinesForLocale(m_translations, lang);
|
||||
return TranslateImpl(m_translations, singleOsmCuisine);
|
||||
}
|
||||
|
||||
TAllCuisines Cuisines::AllSupportedCuisines() { return m_translations->GetAllSortedTranslations(); }
|
||||
} // namespace osm
|
35
indexer/cuisines.hpp
Normal file
35
indexer/cuisines.hpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include "platform/get_text_by_id.hpp"
|
||||
#include "platform/preferred_languages.hpp"
|
||||
|
||||
#include "std/string.hpp"
|
||||
#include "std/vector.hpp"
|
||||
|
||||
namespace osm
|
||||
{
|
||||
using TAllCuisines = vector<pair<string, string>>;
|
||||
|
||||
class Cuisines
|
||||
{
|
||||
Cuisines() = default;
|
||||
|
||||
public:
|
||||
static Cuisines & Instance();
|
||||
/// @param[out] outCuisines contains list of parsed cuisines (not localized).
|
||||
void Parse(string const & osmRawCuisinesTagValue, vector<string> & outCuisines);
|
||||
/// @param[in] lang should be in our twine strings.txt/cuisines.txt format.
|
||||
/// @param[out] outCuisines contains list of parsed cuisines (localized).
|
||||
void ParseAndLocalize(string const & osmRawCuisinesTagValue, vector<string> & outCuisines,
|
||||
string const & lang = languages::GetCurrentTwine());
|
||||
/// @param[in] lang should be in our twine strings.txt/cuisines.txt format.
|
||||
/// @returns translated cuisine.
|
||||
string Translate(string const & singleOsmCuisine,
|
||||
string const & lang = languages::GetCurrentTwine());
|
||||
/// @returns list of osm cuisines in cuisines.txt (not localized).
|
||||
TAllCuisines AllSupportedCuisines();
|
||||
|
||||
private:
|
||||
platform::TGetTextByIdPtr m_translations;
|
||||
};
|
||||
} // namespace osm
|
|
@ -14,6 +14,7 @@ SOURCES += \
|
|||
categories_holder_loader.cpp \
|
||||
classificator.cpp \
|
||||
classificator_loader.cpp \
|
||||
cuisines.cpp \
|
||||
coding_params.cpp \
|
||||
data_factory.cpp \
|
||||
data_header.cpp \
|
||||
|
@ -60,6 +61,7 @@ HEADERS += \
|
|||
classificator.hpp \
|
||||
classificator_loader.hpp \
|
||||
coding_params.hpp \
|
||||
cuisines.hpp \
|
||||
data_factory.hpp \
|
||||
data_header.hpp \
|
||||
drawing_rule_def.hpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue