2016-01-15 02:47:19 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2016-01-15 02:47:19 +00:00
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
|
#include <common/Common.h>
|
|
|
|
|
#include <DB/Core/StringRef.h>
|
2016-10-27 17:53:47 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
/** Класс, позволяющий узнавать по id региона его текстовое название на одном из поддерживаемых языков: ru, en, ua, by, kz, tr.
|
2016-02-07 08:42:21 +00:00
|
|
|
|
*
|
2016-01-15 02:47:19 +00:00
|
|
|
|
* Информацию об именах регионов загружает из текстовых файлов с названиями следующего формата:
|
|
|
|
|
* regions_names_xx.txt,
|
|
|
|
|
* где xx - одно из двух буквенных обозначений следующих поддерживаемых языков:
|
|
|
|
|
* ru, en, ua, by, kz, tr.
|
2016-02-07 08:42:21 +00:00
|
|
|
|
*
|
2016-01-15 02:47:19 +00:00
|
|
|
|
* Умеет, по запросу, обновлять данные.
|
|
|
|
|
*/
|
|
|
|
|
class RegionsNames
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum class Language
|
|
|
|
|
{
|
|
|
|
|
RU = 0,
|
|
|
|
|
EN,
|
|
|
|
|
UA,
|
|
|
|
|
BY,
|
|
|
|
|
KZ,
|
|
|
|
|
TR,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static const size_t ROOT_LANGUAGE = 0;
|
|
|
|
|
static const size_t SUPPORTED_LANGUAGES_COUNT = 6;
|
|
|
|
|
static const size_t LANGUAGE_ALIASES_COUNT = 7;
|
|
|
|
|
|
|
|
|
|
static const char ** getSupportedLanguages()
|
|
|
|
|
{
|
|
|
|
|
static const char * res[] { "ru", "en", "ua", "by", "kz", "tr" };
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct language_alias { const char * const name; const Language lang; };
|
|
|
|
|
static const language_alias * getLanguageAliases()
|
|
|
|
|
{
|
|
|
|
|
static constexpr const language_alias language_aliases[] {
|
|
|
|
|
{ "ru", Language::RU },
|
|
|
|
|
{ "en", Language::EN },
|
|
|
|
|
{ "ua", Language::UA },
|
|
|
|
|
{ "uk", Language::UA },
|
|
|
|
|
{ "by", Language::BY },
|
|
|
|
|
{ "kz", Language::KZ },
|
|
|
|
|
{ "tr", Language::TR }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return language_aliases;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 17:53:47 +00:00
|
|
|
|
using RegionID = UInt32;
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-10-27 17:53:47 +00:00
|
|
|
|
using Chars = std::vector<char>;
|
|
|
|
|
using CharsForLanguageID = std::vector<Chars>;
|
|
|
|
|
using ModificationTimes = std::vector<time_t>;
|
|
|
|
|
using StringRefs = std::vector<StringRef>; /// Lookup table RegionID -> StringRef
|
|
|
|
|
using StringRefsForLanguageID = std::vector<StringRefs>;
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
public:
|
2017-01-21 04:24:28 +00:00
|
|
|
|
/** Перезагружает, при необходимости, имена регионов.
|
2016-01-15 02:47:19 +00:00
|
|
|
|
*/
|
2017-01-21 04:24:28 +00:00
|
|
|
|
void reload();
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
/// Has corresponding section in configuration file.
|
|
|
|
|
static bool isConfigured();
|
2016-01-15 02:47:19 +00:00
|
|
|
|
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-10-27 17:53:47 +00:00
|
|
|
|
StringRef getRegionName(RegionID region_id, Language language = Language::RU) const
|
2016-01-15 02:47:19 +00:00
|
|
|
|
{
|
|
|
|
|
size_t language_id = static_cast<size_t>(language);
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-10-27 17:53:47 +00:00
|
|
|
|
if (region_id > names_refs[language_id].size())
|
2016-01-15 02:47:19 +00:00
|
|
|
|
return StringRef("", 0);
|
|
|
|
|
|
|
|
|
|
StringRef ref = names_refs[language_id][region_id];
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
while (ref.size == 0 && language_id != ROOT_LANGUAGE)
|
|
|
|
|
{
|
|
|
|
|
static const size_t FALLBACK[] = { 0, 0, 0, 0, 0, 1 };
|
|
|
|
|
language_id = FALLBACK[language_id];
|
|
|
|
|
ref = names_refs[language_id][region_id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Language getLanguageEnum(const std::string & language)
|
|
|
|
|
{
|
|
|
|
|
if (language.size() == 2)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < LANGUAGE_ALIASES_COUNT; ++i)
|
|
|
|
|
{
|
|
|
|
|
const auto & alias = getLanguageAliases()[i];
|
|
|
|
|
if (language[0] == alias.name[0] && language[1] == alias.name[1])
|
|
|
|
|
return alias.lang;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw Poco::Exception("Unsupported language for region name. Supported languages are: " + dumpSupportedLanguagesNames() + ".");
|
|
|
|
|
}
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
private:
|
2017-01-21 04:24:28 +00:00
|
|
|
|
static std::string dumpSupportedLanguagesNames();
|
|
|
|
|
|
2016-10-27 17:53:47 +00:00
|
|
|
|
ModificationTimes file_modification_times = ModificationTimes(SUPPORTED_LANGUAGES_COUNT);
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
/// Байты имен для каждого языка, уложенные подряд, разделенные нулями
|
2016-10-27 17:53:47 +00:00
|
|
|
|
CharsForLanguageID chars = CharsForLanguageID(SUPPORTED_LANGUAGES_COUNT);
|
2016-02-07 08:42:21 +00:00
|
|
|
|
|
2016-01-15 02:47:19 +00:00
|
|
|
|
/// Отображение для каждого языка из id региона в указатель на диапазон байт имени
|
2016-10-27 17:53:47 +00:00
|
|
|
|
StringRefsForLanguageID names_refs = StringRefsForLanguageID(SUPPORTED_LANGUAGES_COUNT);
|
2016-01-15 02:47:19 +00:00
|
|
|
|
};
|