2018-07-03 19:28:00 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-07-03 19:28:00 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/LanguagePackManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2018-08-06 16:22:22 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2018-07-03 19:28:00 +02:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-07-17 04:58:34 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2023-07-01 13:53:04 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2019-10-22 19:01:15 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2018-07-22 00:59:13 +02:00
|
|
|
#include "td/db/SqliteDb.h"
|
2018-07-20 19:50:38 +02:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-11-23 00:11:05 +01:00
|
|
|
#include "td/utils/ExitGuard.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2018-07-20 19:50:38 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-07-22 02:29:48 +02:00
|
|
|
#include "td/utils/misc.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-07-20 19:50:38 +02:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2018-07-22 00:59:13 +02:00
|
|
|
#include <atomic>
|
2018-08-26 10:08:45 +02:00
|
|
|
#include <limits>
|
2018-09-04 14:01:08 +02:00
|
|
|
#include <map>
|
2018-07-22 19:22:19 +02:00
|
|
|
#include <utility>
|
2018-07-22 00:59:13 +02:00
|
|
|
|
2018-07-03 19:28:00 +02:00
|
|
|
namespace td {
|
|
|
|
|
2018-07-22 00:59:13 +02:00
|
|
|
struct LanguagePackManager::PluralizedString {
|
|
|
|
string zero_value_;
|
|
|
|
string one_value_;
|
|
|
|
string two_value_;
|
|
|
|
string few_value_;
|
|
|
|
string many_value_;
|
|
|
|
string other_value_;
|
2022-02-11 17:27:32 +01:00
|
|
|
|
|
|
|
PluralizedString(string &&zero_value, string &&one_value, string &&two_value, string &&few_value, string &&many_value,
|
|
|
|
string &&other_value)
|
|
|
|
: zero_value_(std::move(zero_value))
|
|
|
|
, one_value_(std::move(one_value))
|
|
|
|
, two_value_(std::move(two_value))
|
|
|
|
, few_value_(std::move(few_value))
|
|
|
|
, many_value_(std::move(many_value))
|
|
|
|
, other_value_(std::move(other_value)) {
|
|
|
|
}
|
2018-07-22 00:59:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LanguagePackManager::Language {
|
|
|
|
std::mutex mutex_;
|
|
|
|
std::atomic<int32> version_{-1};
|
2018-08-23 02:45:04 +02:00
|
|
|
std::atomic<int32> key_count_{0};
|
2019-02-11 01:45:09 +01:00
|
|
|
std::string base_language_code_;
|
2018-07-23 13:45:31 +02:00
|
|
|
bool is_full_ = false;
|
2018-08-23 02:45:04 +02:00
|
|
|
bool was_loaded_full_ = false;
|
2018-07-22 00:59:13 +02:00
|
|
|
bool has_get_difference_query_ = false;
|
2019-02-23 18:27:14 +01:00
|
|
|
vector<Promise<Unit>> get_difference_queries_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, string> ordinary_strings_;
|
2022-02-11 17:27:32 +01:00
|
|
|
FlatHashMap<string, unique_ptr<PluralizedString>> pluralized_strings_;
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<string> deleted_strings_;
|
2022-08-17 21:49:05 +02:00
|
|
|
SqliteKeyValue kv_; // usages must be guarded by database_->mutex_
|
2018-07-22 00:59:13 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 19:18:28 +02:00
|
|
|
struct LanguagePackManager::LanguageInfo {
|
2019-02-11 18:57:35 +01:00
|
|
|
string name_;
|
|
|
|
string native_name_;
|
|
|
|
string base_language_code_;
|
|
|
|
string plural_code_;
|
|
|
|
bool is_official_ = false;
|
|
|
|
bool is_rtl_ = false;
|
|
|
|
bool is_beta_ = false;
|
2019-02-12 02:15:45 +01:00
|
|
|
bool is_from_database_ = false;
|
2019-02-11 18:57:35 +01:00
|
|
|
int32 total_string_count_ = 0; // TODO move to LanguagePack and calculate as max(total_string_count)
|
|
|
|
int32 translated_string_count_ = 0;
|
|
|
|
string translation_url_;
|
2018-09-04 14:01:08 +02:00
|
|
|
|
|
|
|
friend bool operator==(const LanguageInfo &lhs, const LanguageInfo &rhs) {
|
2019-02-11 18:57:35 +01:00
|
|
|
return lhs.name_ == rhs.name_ && lhs.native_name_ == rhs.native_name_ &&
|
|
|
|
lhs.base_language_code_ == rhs.base_language_code_ && lhs.plural_code_ == rhs.plural_code_ &&
|
|
|
|
lhs.is_official_ == rhs.is_official_ && lhs.is_rtl_ == rhs.is_rtl_ && lhs.is_beta_ == rhs.is_beta_ &&
|
|
|
|
lhs.total_string_count_ == rhs.total_string_count_ &&
|
|
|
|
lhs.translated_string_count_ == rhs.translated_string_count_ && lhs.translation_url_ == rhs.translation_url_;
|
2018-09-04 14:01:08 +02:00
|
|
|
}
|
2018-08-06 19:18:28 +02:00
|
|
|
};
|
|
|
|
|
2018-07-22 00:59:13 +02:00
|
|
|
struct LanguagePackManager::LanguagePack {
|
|
|
|
std::mutex mutex_;
|
2022-08-17 21:49:05 +02:00
|
|
|
SqliteKeyValue pack_kv_; // usages must be guarded by database_->mutex_
|
2018-09-04 14:01:08 +02:00
|
|
|
std::map<string, LanguageInfo> custom_language_pack_infos_; // sorted by language_code
|
|
|
|
vector<std::pair<string, LanguageInfo>> server_language_pack_infos_; // sorted by server
|
2022-02-11 17:27:32 +01:00
|
|
|
FlatHashMap<string, unique_ptr<LanguageInfo>> all_server_language_pack_infos_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, unique_ptr<Language>> languages_;
|
2018-07-22 00:59:13 +02:00
|
|
|
};
|
|
|
|
|
2018-07-20 19:50:38 +02:00
|
|
|
struct LanguagePackManager::LanguageDatabase {
|
|
|
|
std::mutex mutex_;
|
|
|
|
string path_;
|
2018-07-22 00:59:13 +02:00
|
|
|
SqliteDb database_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, unique_ptr<LanguagePack>> language_packs_;
|
2018-07-20 19:50:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
LanguagePackManager::~LanguagePackManager() = default;
|
|
|
|
|
2018-07-22 02:29:48 +02:00
|
|
|
bool LanguagePackManager::check_language_pack_name(Slice name) {
|
|
|
|
for (auto c : name) {
|
|
|
|
if (c != '_' && !is_alpha(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
return name.size() <= 64;
|
2018-07-22 02:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LanguagePackManager::check_language_code_name(Slice name) {
|
|
|
|
for (auto c : name) {
|
2018-08-23 02:45:04 +02:00
|
|
|
if (c != '-' && !is_alpha(c) && !is_digit(c)) {
|
2018-07-22 02:29:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
return name.size() <= 64 && (is_custom_language_code(name) || name.empty() || name.size() >= 2);
|
2018-08-06 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LanguagePackManager::is_custom_language_code(Slice language_code) {
|
|
|
|
return !language_code.empty() && language_code[0] == 'X';
|
2018-07-22 02:29:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 00:59:13 +02:00
|
|
|
static Result<SqliteDb> open_database(const string &path) {
|
2021-09-22 18:04:56 +02:00
|
|
|
TRY_RESULT(database, SqliteDb::open_with_key(path, true, DbKey::empty()));
|
2018-07-22 00:59:13 +02:00
|
|
|
TRY_STATUS(database.exec("PRAGMA journal_mode=WAL"));
|
|
|
|
return std::move(database);
|
2018-07-20 19:50:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 19:22:19 +02:00
|
|
|
static int32 load_database_language_version(SqliteKeyValue *kv) {
|
2018-07-23 13:45:31 +02:00
|
|
|
CHECK(kv != nullptr);
|
|
|
|
if (kv->empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-22 19:22:19 +02:00
|
|
|
string str_version = kv->get("!version");
|
|
|
|
if (str_version.empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return to_integer<int32>(str_version);
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
static int32 load_database_language_key_count(SqliteKeyValue *kv) {
|
|
|
|
CHECK(kv != nullptr);
|
|
|
|
if (kv->empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
string str_key_count = kv->get("!key_count");
|
|
|
|
if (str_key_count.empty()) {
|
|
|
|
// calculate key count once for the database and cache it
|
|
|
|
int key_count = 0;
|
|
|
|
for (auto &str : kv->get_all()) {
|
|
|
|
key_count += str.first[0] != '!' && (str.second[0] == '1' || str.second[0] == '2');
|
|
|
|
}
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Set language pack key count in database to " << key_count;
|
2018-08-23 02:45:04 +02:00
|
|
|
kv->set("!key_count", to_string(key_count));
|
|
|
|
return key_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return to_integer<int32>(str_key_count);
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:45:09 +01:00
|
|
|
static string load_database_language_base_language_code(SqliteKeyValue *kv) {
|
|
|
|
CHECK(kv != nullptr);
|
|
|
|
if (kv->empty()) {
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
return kv->get("!base_language_code");
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
LanguagePackManager::LanguageDatabase *LanguagePackManager::add_language_database(string path) {
|
2018-07-23 19:29:14 +02:00
|
|
|
auto it = language_databases_.find(path);
|
|
|
|
if (it != language_databases_.end()) {
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
SqliteDb database;
|
|
|
|
if (!path.empty()) {
|
|
|
|
auto r_database = open_database(path);
|
|
|
|
if (r_database.is_error()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(ERROR) << "Can't open language pack database " << path << ": " << r_database.error();
|
2018-07-23 19:29:14 +02:00
|
|
|
return add_language_database(string());
|
|
|
|
}
|
|
|
|
|
|
|
|
database = r_database.move_as_ok();
|
|
|
|
}
|
|
|
|
|
2018-12-26 19:54:35 +01:00
|
|
|
it = language_databases_.emplace(path, make_unique<LanguageDatabase>()).first;
|
2018-07-23 19:29:14 +02:00
|
|
|
it->second->path_ = std::move(path);
|
|
|
|
it->second->database_ = std::move(database);
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:49:05 +02:00
|
|
|
LanguagePackManager::LanguagePackManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
2019-02-11 01:45:09 +01:00
|
|
|
std::lock_guard<std::mutex> database_lock(language_database_mutex_);
|
2018-07-19 00:14:20 +02:00
|
|
|
manager_count_++;
|
2022-08-17 15:11:13 +02:00
|
|
|
language_pack_ = G()->get_option_string("localization_target");
|
|
|
|
language_code_ = G()->get_option_string("language_pack_id");
|
2018-07-22 02:29:48 +02:00
|
|
|
CHECK(check_language_pack_name(language_pack_));
|
|
|
|
CHECK(check_language_code_name(language_code_));
|
2018-07-20 15:15:19 +02:00
|
|
|
|
2022-08-17 15:11:13 +02:00
|
|
|
database_ = add_language_database(G()->get_option_string("language_pack_database_path"));
|
2018-08-24 04:26:19 +02:00
|
|
|
if (!language_pack_.empty() && !language_code_.empty()) {
|
|
|
|
auto language = add_language(database_, language_pack_, language_code_);
|
2019-02-11 01:45:09 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> language_lock(language->mutex_);
|
|
|
|
base_language_code_ = language->base_language_code_;
|
|
|
|
if (!check_language_code_name(base_language_code_)) {
|
2019-02-11 18:57:35 +01:00
|
|
|
LOG(ERROR) << "Have invalid base language pack ID \"" << base_language_code_ << '"';
|
2019-02-11 01:45:09 +01:00
|
|
|
base_language_code_.clear();
|
|
|
|
}
|
|
|
|
if (!base_language_code_.empty()) {
|
2022-08-17 21:49:05 +02:00
|
|
|
add_language(database_, language_pack_, base_language_code_);
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2018-07-23 13:45:31 +02:00
|
|
|
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Use localization target \"" << language_pack_ << "\" with language pack \"" << language_code_
|
2019-02-11 01:45:09 +01:00
|
|
|
<< "\" based on \"" << base_language_code_ << "\" of version " << language->version_.load()
|
|
|
|
<< " with database \"" << database_->path_ << '"';
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2018-07-05 02:52:58 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 21:49:05 +02:00
|
|
|
void LanguagePackManager::start_up() {
|
|
|
|
if (language_pack_.empty() || language_code_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto language = get_language(database_, language_pack_, language_code_);
|
|
|
|
CHECK(language != nullptr);
|
|
|
|
if (language->version_ == -1) {
|
|
|
|
load_empty_language_pack(language_code_);
|
|
|
|
}
|
|
|
|
repair_chosen_language_info();
|
|
|
|
|
|
|
|
if (!base_language_code_.empty()) {
|
|
|
|
auto base_language = get_language(database_, language_pack_, base_language_code_);
|
|
|
|
CHECK(base_language != nullptr);
|
|
|
|
if (base_language->version_ == -1) {
|
|
|
|
load_empty_language_pack(base_language_code_);
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 23:37:19 +02:00
|
|
|
|
|
|
|
on_language_pack_version_changed(false, -1);
|
|
|
|
on_language_pack_version_changed(true, -1);
|
2022-08-17 21:49:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 00:14:20 +02:00
|
|
|
void LanguagePackManager::tear_down() {
|
2020-11-23 00:11:05 +01:00
|
|
|
if (ExitGuard::is_exited()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-20 15:15:19 +02:00
|
|
|
std::lock_guard<std::mutex> lock(language_database_mutex_);
|
2018-07-19 00:14:20 +02:00
|
|
|
manager_count_--;
|
|
|
|
if (manager_count_ == 0) {
|
2022-08-17 21:49:05 +02:00
|
|
|
// can't clear language packs, because they can be accessed later using synchronous requests
|
2018-07-20 15:15:19 +02:00
|
|
|
// LOG(INFO) << "Clear language packs";
|
|
|
|
// language_databases_.clear();
|
2018-07-19 00:14:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:38:45 +02:00
|
|
|
string LanguagePackManager::get_main_language_code() {
|
|
|
|
if (language_pack_.empty() || language_code_.empty()) {
|
|
|
|
return "en";
|
|
|
|
}
|
2020-10-01 16:21:26 +02:00
|
|
|
if (language_code_.size() == 2) {
|
2020-08-24 22:38:45 +02:00
|
|
|
return language_code_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
|
|
|
|
LanguageInfo *info = nullptr;
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
std::lock_guard<std::mutex> languages_lock(pack->mutex_);
|
|
|
|
if (is_custom_language_code(language_code_)) {
|
|
|
|
auto custom_it = pack->custom_language_pack_infos_.find(language_code_);
|
|
|
|
if (custom_it != pack->custom_language_pack_infos_.end()) {
|
|
|
|
info = &custom_it->second;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto &server_info : pack->server_language_pack_infos_) {
|
|
|
|
if (server_info.first == language_code_) {
|
|
|
|
info = &server_info.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info == nullptr) {
|
2023-01-24 16:34:49 +01:00
|
|
|
LOG(INFO) << "Failed to find information about chosen language " << language_code_
|
|
|
|
<< ", ensure that valid language pack ID is used";
|
2020-08-24 22:38:45 +02:00
|
|
|
if (!is_custom_language_code(language_code_)) {
|
2023-01-24 16:34:49 +01:00
|
|
|
get_languages(false, Auto());
|
2020-08-24 22:38:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!info->base_language_code_.empty()) {
|
|
|
|
return info->base_language_code_;
|
|
|
|
}
|
|
|
|
if (!info->plural_code_.empty()) {
|
|
|
|
return info->plural_code_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "en";
|
|
|
|
}
|
|
|
|
|
2019-07-11 03:22:17 +02:00
|
|
|
vector<string> LanguagePackManager::get_used_language_codes() {
|
2019-05-21 17:48:35 +02:00
|
|
|
if (language_pack_.empty() || language_code_.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
|
|
|
|
LanguageInfo *info = nullptr;
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
std::lock_guard<std::mutex> languages_lock(pack->mutex_);
|
|
|
|
if (is_custom_language_code(language_code_)) {
|
|
|
|
auto custom_it = pack->custom_language_pack_infos_.find(language_code_);
|
|
|
|
if (custom_it != pack->custom_language_pack_infos_.end()) {
|
|
|
|
info = &custom_it->second;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto &server_info : pack->server_language_pack_infos_) {
|
|
|
|
if (server_info.first == language_code_) {
|
|
|
|
info = &server_info.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> result;
|
2020-10-01 16:21:26 +02:00
|
|
|
if (language_code_.size() == 2) {
|
2019-05-21 17:48:35 +02:00
|
|
|
result.push_back(language_code_);
|
|
|
|
}
|
2019-07-11 03:22:17 +02:00
|
|
|
if (info == nullptr) {
|
2021-12-12 21:50:05 +01:00
|
|
|
LOG(INFO) << "Failed to find information about chosen language " << language_code_
|
|
|
|
<< ", ensure that valid language pack ID is used";
|
2019-07-11 03:22:17 +02:00
|
|
|
if (!is_custom_language_code(language_code_)) {
|
2023-01-24 16:34:49 +01:00
|
|
|
get_languages(false, Auto());
|
2019-07-11 03:22:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!info->base_language_code_.empty()) {
|
|
|
|
result.push_back(info->base_language_code_);
|
|
|
|
}
|
|
|
|
if (!info->plural_code_.empty()) {
|
|
|
|
result.push_back(info->plural_code_);
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-05 02:52:58 +02:00
|
|
|
void LanguagePackManager::on_language_pack_changed() {
|
2022-08-17 15:11:13 +02:00
|
|
|
auto new_language_pack = G()->get_option_string("localization_target");
|
2018-07-05 02:52:58 +02:00
|
|
|
if (new_language_pack == language_pack_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
language_pack_ = std::move(new_language_pack);
|
2018-07-22 02:29:48 +02:00
|
|
|
CHECK(check_language_pack_name(language_pack_));
|
2018-07-05 02:52:58 +02:00
|
|
|
inc_generation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::on_language_code_changed() {
|
2022-08-17 15:11:13 +02:00
|
|
|
auto new_language_code = G()->get_option_string("language_pack_id");
|
2018-07-05 02:52:58 +02:00
|
|
|
if (new_language_code == language_code_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
language_code_ = std::move(new_language_code);
|
2018-07-22 02:29:48 +02:00
|
|
|
CHECK(check_language_code_name(language_code_));
|
2018-07-05 02:52:58 +02:00
|
|
|
inc_generation();
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:45:09 +01:00
|
|
|
void LanguagePackManager::on_language_pack_version_changed(bool is_base, int32 new_version) {
|
|
|
|
if (language_pack_.empty() || language_code_.empty()) {
|
2018-08-06 16:22:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-20 15:15:19 +02:00
|
|
|
Language *language = get_language(database_, language_pack_, language_code_);
|
2018-07-17 15:49:57 +02:00
|
|
|
int32 version = language == nullptr ? static_cast<int32>(-1) : language->version_.load();
|
2023-03-22 19:50:07 +01:00
|
|
|
LOG(INFO) << (is_base ? "Base" : "Main") << " language pack version has changed from main " << version << " to "
|
2022-08-17 21:49:05 +02:00
|
|
|
<< new_version;
|
2018-07-17 04:58:34 +02:00
|
|
|
if (version == -1) {
|
2019-02-11 01:45:09 +01:00
|
|
|
return load_empty_language_pack(language_code_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_version < 0) {
|
|
|
|
Slice version_key = is_base ? Slice("base_language_pack_version") : Slice("language_pack_version");
|
2022-08-17 15:11:13 +02:00
|
|
|
new_version = narrow_cast<int32>(G()->get_option_integer(version_key, -1));
|
2019-02-11 01:45:09 +01:00
|
|
|
}
|
|
|
|
if (new_version <= 0) {
|
2018-07-17 04:58:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:45:09 +01:00
|
|
|
string language_code;
|
|
|
|
if (is_base) {
|
|
|
|
language_code = base_language_code_;
|
|
|
|
if (language_code.empty()) {
|
|
|
|
LOG(ERROR) << "Have no base language, but received new version " << new_version;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
language = get_language(database_, language_pack_, language_code);
|
|
|
|
version = language == nullptr ? static_cast<int32>(-1) : language->version_.load();
|
|
|
|
if (version == -1) {
|
|
|
|
return load_empty_language_pack(language_code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
language_code = language_code_;
|
|
|
|
}
|
|
|
|
if (is_custom_language_code(language_code) || new_version <= version) {
|
2018-07-17 04:58:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-22 19:50:07 +01:00
|
|
|
LOG(INFO) << (is_base ? "Base" : "Main") << " language pack " << language_code << " version has changed to "
|
2019-02-12 04:28:14 +01:00
|
|
|
<< new_version;
|
2021-10-19 17:11:16 +02:00
|
|
|
send_language_get_difference_query(language, std::move(language_code), version, Auto());
|
2019-02-23 18:27:14 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void LanguagePackManager::send_language_get_difference_query(Language *language, string language_code, int32 version,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-07-17 04:58:34 +02:00
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2019-02-23 18:27:14 +01:00
|
|
|
language->get_difference_queries_.push_back(std::move(promise));
|
2018-07-17 04:58:34 +02:00
|
|
|
if (language->has_get_difference_query_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 18:27:14 +01:00
|
|
|
CHECK(language->get_difference_queries_.size() == 1);
|
2018-07-17 04:58:34 +02:00
|
|
|
language->has_get_difference_query_ = true;
|
|
|
|
auto request_promise =
|
2019-02-11 01:45:09 +01:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code,
|
2018-07-17 04:58:34 +02:00
|
|
|
from_version = version](Result<NetQueryPtr> r_query) mutable {
|
|
|
|
auto r_result = fetch_result<telegram_api::langpack_getDifference>(std::move(r_query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
send_closure(actor_id, &LanguagePackManager::on_failed_get_difference, std::move(language_pack),
|
2019-02-23 18:27:14 +01:00
|
|
|
std::move(language_code), r_result.move_as_error());
|
2018-07-17 04:58:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = r_result.move_as_ok();
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Receive language pack difference for language pack " << result->lang_code_ << " from version "
|
2018-07-17 04:58:34 +02:00
|
|
|
<< result->from_version_ << " with version " << result->version_ << " of size "
|
|
|
|
<< result->strings_.size();
|
2019-03-09 17:45:45 +01:00
|
|
|
to_lower_inplace(result->lang_code_);
|
2018-07-17 04:58:34 +02:00
|
|
|
LOG_IF(ERROR, result->lang_code_ != language_code)
|
|
|
|
<< "Receive strings for " << result->lang_code_ << " instead of " << language_code;
|
|
|
|
LOG_IF(ERROR, result->from_version_ != from_version)
|
|
|
|
<< "Receive strings from " << result->from_version_ << " instead of " << from_version;
|
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_language_pack_strings, std::move(language_pack),
|
|
|
|
std::move(language_code), result->version_, true, vector<string>(), std::move(result->strings_),
|
|
|
|
Promise<td_api::object_ptr<td_api::languagePackStrings>>());
|
|
|
|
});
|
2020-03-15 02:51:14 +01:00
|
|
|
send_with_promise(G()->net_query_creator().create_unauth(
|
2020-03-15 23:01:14 +01:00
|
|
|
telegram_api::langpack_getDifference(language_pack_, language_code, version)),
|
2019-03-10 19:30:15 +01:00
|
|
|
std::move(request_promise));
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 23:12:42 +01:00
|
|
|
void LanguagePackManager::on_language_pack_too_long(string language_code) {
|
|
|
|
if (language_code == language_code_) {
|
2019-02-11 01:45:09 +01:00
|
|
|
return on_language_pack_version_changed(false, std::numeric_limits<int32>::max());
|
|
|
|
}
|
|
|
|
if (language_code == base_language_code_) {
|
|
|
|
return on_language_pack_version_changed(true, std::numeric_limits<int32>::max());
|
2019-02-10 23:12:42 +01:00
|
|
|
}
|
|
|
|
LOG(WARNING) << "Receive languagePackTooLong for language " << language_code << ", but use language "
|
2019-02-11 01:45:09 +01:00
|
|
|
<< language_code_ << " with base language " << base_language_code_;
|
2019-02-10 23:12:42 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 04:58:34 +02:00
|
|
|
void LanguagePackManager::on_update_language_pack(tl_object_ptr<telegram_api::langPackDifference> difference) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Receive update language pack difference for language pack " << difference->lang_code_
|
|
|
|
<< " from version " << difference->from_version_ << " with version " << difference->version_ << " of size "
|
2018-07-17 04:58:34 +02:00
|
|
|
<< difference->strings_.size();
|
2019-03-09 17:45:45 +01:00
|
|
|
to_lower_inplace(difference->lang_code_);
|
2020-06-23 00:02:46 +02:00
|
|
|
if (language_code_.empty()) {
|
|
|
|
LOG(INFO) << "Ignore difference for language pack " << difference->lang_code_
|
|
|
|
<< ", because have no used language pack";
|
|
|
|
return;
|
|
|
|
}
|
2018-08-24 04:26:19 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(WARNING) << "Ignore difference for language pack " << difference->lang_code_
|
2020-06-23 00:02:46 +02:00
|
|
|
<< ", because localization target is not set";
|
2018-08-24 04:26:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-02-11 01:45:09 +01:00
|
|
|
if (difference->lang_code_ != language_code_ && difference->lang_code_ != base_language_code_) {
|
2020-06-23 00:02:46 +02:00
|
|
|
LOG(WARNING) << "Ignore difference for language pack " << difference->lang_code_ << ", because using language pack "
|
|
|
|
<< language_code_ << " based on " << base_language_code_;
|
2018-07-05 02:52:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-08-24 04:26:19 +02:00
|
|
|
if (is_custom_language_code(difference->lang_code_) || difference->lang_code_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(ERROR) << "Ignore difference for language pack " << difference->lang_code_;
|
2018-08-24 04:26:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-17 04:58:34 +02:00
|
|
|
|
2019-02-11 01:45:09 +01:00
|
|
|
Language *language = get_language(database_, language_pack_, difference->lang_code_);
|
2018-07-17 15:49:57 +02:00
|
|
|
int32 version = language == nullptr ? static_cast<int32>(-1) : language->version_.load();
|
2018-07-17 04:58:34 +02:00
|
|
|
if (difference->version_ <= version) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Skip applying already applied language pack updates";
|
2018-07-05 02:52:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-17 04:58:34 +02:00
|
|
|
if (version == -1 || version < difference->from_version_) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Can't apply language pack difference";
|
2019-02-11 01:45:09 +01:00
|
|
|
return on_language_pack_version_changed(difference->lang_code_ != language_code_, difference->version_);
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
2018-07-05 02:52:58 +02:00
|
|
|
|
2018-07-17 04:58:34 +02:00
|
|
|
on_get_language_pack_strings(language_pack_, std::move(difference->lang_code_), difference->version_, true,
|
|
|
|
vector<string>(), std::move(difference->strings_),
|
|
|
|
Promise<td_api::object_ptr<td_api::languagePackStrings>>());
|
2018-07-05 02:52:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::inc_generation() {
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_empty("language_pack_version");
|
|
|
|
G()->set_option_empty("base_language_pack_version");
|
2019-02-11 01:45:09 +01:00
|
|
|
|
|
|
|
if (!language_pack_.empty() && !language_code_.empty()) {
|
2019-02-12 04:28:14 +01:00
|
|
|
LOG(INFO) << "Add main language " << language_code_;
|
2019-02-11 01:45:09 +01:00
|
|
|
CHECK(check_language_code_name(language_code_));
|
|
|
|
auto language = add_language(database_, language_pack_, language_code_);
|
|
|
|
on_language_pack_version_changed(false, std::numeric_limits<int32>::max());
|
2019-07-11 03:22:17 +02:00
|
|
|
repair_chosen_language_info();
|
2019-02-11 01:45:09 +01:00
|
|
|
|
2019-02-12 04:28:14 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
|
|
|
base_language_code_ = language->base_language_code_;
|
|
|
|
}
|
2019-02-11 01:45:09 +01:00
|
|
|
if (!check_language_code_name(base_language_code_)) {
|
2019-02-11 18:57:35 +01:00
|
|
|
LOG(ERROR) << "Have invalid base language pack ID \"" << base_language_code_ << '"';
|
2019-02-11 01:45:09 +01:00
|
|
|
base_language_code_.clear();
|
|
|
|
}
|
|
|
|
if (!base_language_code_.empty()) {
|
2019-02-12 04:28:14 +01:00
|
|
|
CHECK(base_language_code_ != language_code_);
|
|
|
|
LOG(INFO) << "Add base language " << base_language_code_;
|
2019-02-11 01:45:09 +01:00
|
|
|
add_language(database_, language_pack_, base_language_code_);
|
|
|
|
on_language_pack_version_changed(true, std::numeric_limits<int32>::max());
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 04:28:14 +01:00
|
|
|
LOG(INFO) << "Finished to apply new language pack";
|
2018-07-05 02:52:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 15:15:19 +02:00
|
|
|
LanguagePackManager::Language *LanguagePackManager::get_language(LanguageDatabase *database,
|
|
|
|
const string &language_pack,
|
2018-07-05 20:28:11 +02:00
|
|
|
const string &language_code) {
|
2018-07-20 15:15:19 +02:00
|
|
|
std::unique_lock<std::mutex> lock(database->mutex_);
|
|
|
|
auto it = database->language_packs_.find(language_pack);
|
|
|
|
if (it == database->language_packs_.end()) {
|
2018-07-05 20:28:11 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
LanguagePack *pack = it->second.get();
|
|
|
|
lock.unlock();
|
|
|
|
return get_language(pack, language_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguagePackManager::Language *LanguagePackManager::get_language(LanguagePack *language_pack,
|
|
|
|
const string &language_code) {
|
|
|
|
CHECK(language_pack != nullptr);
|
|
|
|
std::lock_guard<std::mutex> lock(language_pack->mutex_);
|
|
|
|
auto it = language_pack->languages_.find(language_code);
|
|
|
|
if (it == language_pack->languages_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
2018-07-22 02:29:48 +02:00
|
|
|
static string get_database_table_name(const string &language_pack, const string &language_code) {
|
2018-08-01 01:34:59 +02:00
|
|
|
return PSTRING() << "\"kv_" << language_pack << '_' << language_code << '"';
|
2018-07-22 02:29:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 15:15:19 +02:00
|
|
|
LanguagePackManager::Language *LanguagePackManager::add_language(LanguageDatabase *database,
|
|
|
|
const string &language_pack,
|
2018-07-05 20:28:11 +02:00
|
|
|
const string &language_code) {
|
2018-07-20 15:15:19 +02:00
|
|
|
std::lock_guard<std::mutex> packs_lock(database->mutex_);
|
|
|
|
auto pack_it = database->language_packs_.find(language_pack);
|
|
|
|
if (pack_it == database->language_packs_.end()) {
|
2018-08-06 19:18:28 +02:00
|
|
|
auto pack = make_unique<LanguagePack>();
|
|
|
|
if (!database->database_.empty()) {
|
|
|
|
pack->pack_kv_.init_with_connection(database->database_.clone(), get_database_table_name(language_pack, "0"))
|
|
|
|
.ensure();
|
2019-02-11 18:57:35 +01:00
|
|
|
bool need_drop_server = false;
|
2018-08-06 19:18:28 +02:00
|
|
|
for (auto &lang : pack->pack_kv_.get_all()) {
|
2019-02-11 18:57:35 +01:00
|
|
|
auto as_bool = [](Slice data) {
|
|
|
|
if (data == "true") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (data != "false") {
|
|
|
|
LOG(ERROR) << "Have invalid boolean value \"" << data << "\" in the database";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-09-04 14:01:08 +02:00
|
|
|
if (lang.first == "!server") {
|
2019-02-11 18:57:35 +01:00
|
|
|
// legacy info format, drop cache
|
|
|
|
need_drop_server = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (lang.first == "!server2") {
|
2018-09-04 14:01:08 +02:00
|
|
|
auto all_infos = full_split(lang.second, '\x00');
|
2019-02-11 18:57:35 +01:00
|
|
|
if (all_infos.size() % 11 == 0) {
|
|
|
|
for (size_t i = 0; i < all_infos.size(); i += 11) {
|
2022-02-09 22:59:52 +01:00
|
|
|
if (all_infos[i].empty()) {
|
|
|
|
LOG(ERROR) << "Have empty info about a language pack";
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
LanguageInfo info;
|
2019-02-11 18:57:35 +01:00
|
|
|
info.name_ = std::move(all_infos[i + 1]);
|
|
|
|
info.native_name_ = std::move(all_infos[i + 2]);
|
|
|
|
info.base_language_code_ = std::move(all_infos[i + 3]);
|
|
|
|
info.plural_code_ = std::move(all_infos[i + 4]);
|
|
|
|
info.is_official_ = as_bool(all_infos[i + 5]);
|
|
|
|
info.is_rtl_ = as_bool(all_infos[i + 6]);
|
|
|
|
info.is_beta_ = as_bool(all_infos[i + 7]);
|
2019-02-12 02:15:45 +01:00
|
|
|
info.is_from_database_ = true;
|
2019-02-11 18:57:35 +01:00
|
|
|
info.total_string_count_ = to_integer<int32>(all_infos[i + 8]);
|
|
|
|
info.translated_string_count_ = to_integer<int32>(all_infos[i + 9]);
|
|
|
|
info.translation_url_ = std::move(all_infos[i + 10]);
|
2022-02-11 17:27:32 +01:00
|
|
|
pack->all_server_language_pack_infos_.emplace(all_infos[i], td::make_unique<LanguageInfo>(info));
|
2018-09-04 14:01:08 +02:00
|
|
|
pack->server_language_pack_infos_.emplace_back(std::move(all_infos[i]), std::move(info));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Have wrong language pack info \"" << lang.second << "\" in the database";
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-11 18:57:35 +01:00
|
|
|
auto all_infos = full_split(lang.second, '\x00');
|
|
|
|
if (all_infos.size() < 2) {
|
|
|
|
LOG(ERROR) << "Have wrong custom language pack info \"" << lang.second << '"';
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
auto &info = pack->custom_language_pack_infos_[lang.first];
|
2019-02-11 18:57:35 +01:00
|
|
|
info.name_ = std::move(all_infos[0]);
|
|
|
|
info.native_name_ = std::move(all_infos[1]);
|
2019-02-12 04:28:14 +01:00
|
|
|
if (all_infos.size() > 2) {
|
2019-02-11 18:57:35 +01:00
|
|
|
CHECK(all_infos.size() == 10);
|
|
|
|
info.base_language_code_ = std::move(all_infos[2]);
|
|
|
|
info.plural_code_ = std::move(all_infos[3]);
|
|
|
|
info.is_official_ = as_bool(all_infos[4]);
|
|
|
|
info.is_rtl_ = as_bool(all_infos[5]);
|
|
|
|
info.is_beta_ = as_bool(all_infos[6]);
|
|
|
|
info.total_string_count_ = to_integer<int32>(all_infos[7]);
|
|
|
|
info.translated_string_count_ = to_integer<int32>(all_infos[8]);
|
|
|
|
info.translation_url_ = std::move(all_infos[9]);
|
|
|
|
}
|
2019-02-12 04:28:14 +01:00
|
|
|
info.is_from_database_ = true;
|
2019-02-11 18:57:35 +01:00
|
|
|
}
|
|
|
|
if (need_drop_server) {
|
2019-02-25 19:55:45 +01:00
|
|
|
LOG(INFO) << "Drop old server language pack info cache";
|
2019-02-11 18:57:35 +01:00
|
|
|
pack->pack_kv_.erase("!server");
|
2018-08-06 19:18:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pack_it = database->language_packs_.emplace(language_pack, std::move(pack)).first;
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> languages_lock(pack->mutex_);
|
|
|
|
auto code_it = pack->languages_.find(language_code);
|
|
|
|
if (code_it == pack->languages_.end()) {
|
2018-07-23 13:45:31 +02:00
|
|
|
auto language = make_unique<Language>();
|
|
|
|
if (!database->database_.empty()) {
|
|
|
|
language->kv_
|
|
|
|
.init_with_connection(database->database_.clone(), get_database_table_name(language_pack, language_code))
|
|
|
|
.ensure();
|
|
|
|
language->version_ = load_database_language_version(&language->kv_);
|
2018-08-23 02:45:04 +02:00
|
|
|
language->key_count_ = load_database_language_key_count(&language->kv_);
|
2019-02-11 01:45:09 +01:00
|
|
|
language->base_language_code_ = load_database_language_base_language_code(&language->kv_);
|
2019-02-25 19:55:45 +01:00
|
|
|
LOG(INFO) << "Loaded language " << language_code << " with version " << language->version_.load()
|
|
|
|
<< ", key count " << language->key_count_.load() << " and base language "
|
|
|
|
<< language->base_language_code_;
|
2018-07-23 13:45:31 +02:00
|
|
|
}
|
|
|
|
code_it = pack->languages_.emplace(language_code, std::move(language)).first;
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
return code_it->second.get();
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
bool LanguagePackManager::language_has_string_unsafe(const Language *language, const string &key) {
|
2018-07-05 20:28:11 +02:00
|
|
|
return language->ordinary_strings_.count(key) != 0 || language->pluralized_strings_.count(key) != 0 ||
|
|
|
|
language->deleted_strings_.count(key) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LanguagePackManager::language_has_strings(Language *language, const vector<string> &keys) {
|
|
|
|
if (language == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2018-07-23 13:45:31 +02:00
|
|
|
if (language->is_full_) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
if (keys.empty()) {
|
2018-08-24 03:53:14 +02:00
|
|
|
return false; // language is already checked to be not full
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
for (auto &key : keys) {
|
|
|
|
if (!language_has_string_unsafe(language, key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-07 18:12:48 +02:00
|
|
|
void LanguagePackManager::load_language_string_unsafe(Language *language, const string &key, const string &value) {
|
2018-07-23 13:45:31 +02:00
|
|
|
CHECK(is_valid_key(key));
|
|
|
|
if (value[0] == '1') {
|
|
|
|
language->ordinary_strings_.emplace(key, value.substr(1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-07 18:12:48 +02:00
|
|
|
if (value[0] == '2') {
|
|
|
|
auto all = full_split(Slice(value).substr(1), '\x00');
|
|
|
|
if (all.size() == 6) {
|
|
|
|
language->pluralized_strings_.emplace(
|
2022-02-11 17:27:32 +01:00
|
|
|
key, td::make_unique<PluralizedString>(all[0].str(), all[1].str(), all[2].str(), all[3].str(), all[4].str(),
|
|
|
|
all[5].str()));
|
2018-09-07 18:12:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_IF(ERROR, !value.empty() && value != "3") << "Have invalid value \"" << value << '"';
|
|
|
|
if (!language->is_full_) {
|
|
|
|
language->deleted_strings_.insert(key);
|
|
|
|
}
|
2018-07-23 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LanguagePackManager::load_language_strings(LanguageDatabase *database, Language *language,
|
|
|
|
const vector<string> &keys) {
|
|
|
|
if (language == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> database_lock(database->mutex_);
|
|
|
|
std::lock_guard<std::mutex> language_lock(language->mutex_);
|
|
|
|
if (language->is_full_) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "The language pack is already full in memory";
|
2018-07-23 13:45:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (language->kv_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "The language pack has no database";
|
2018-07-23 13:45:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Begin to load a language pack from database";
|
2018-07-23 13:45:31 +02:00
|
|
|
if (keys.empty()) {
|
2018-08-23 02:45:04 +02:00
|
|
|
if (language->version_ == -1 && language->was_loaded_full_) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "The language pack has already been loaded";
|
2018-07-23 13:45:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto all_strings = language->kv_.get_all();
|
|
|
|
for (auto &str : all_strings) {
|
2018-08-23 02:45:04 +02:00
|
|
|
if (str.first[0] == '!') {
|
2018-07-23 13:45:31 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!language_has_string_unsafe(language, str.first)) {
|
2018-08-20 00:03:03 +02:00
|
|
|
LOG(DEBUG) << "Load string with key " << str.first << " from database";
|
2018-07-23 13:45:31 +02:00
|
|
|
load_language_string_unsafe(language, str.first, str.second);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
language->was_loaded_full_ = true;
|
|
|
|
|
|
|
|
if (language->version_ == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:45:31 +02:00
|
|
|
language->is_full_ = true;
|
|
|
|
language->deleted_strings_.clear();
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
|
|
|
|
bool have_all = true;
|
2018-07-23 13:45:31 +02:00
|
|
|
for (auto &key : keys) {
|
|
|
|
if (!language_has_string_unsafe(language, key)) {
|
|
|
|
auto value = language->kv_.get(key);
|
|
|
|
if (value.empty()) {
|
|
|
|
if (language->version_ == -1) {
|
2018-08-20 00:03:03 +02:00
|
|
|
LOG(DEBUG) << "Have no string with key " << key << " in the database";
|
2018-08-23 02:45:04 +02:00
|
|
|
have_all = false;
|
|
|
|
continue;
|
2018-07-23 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// have full language in the database, so this string is just deleted
|
|
|
|
}
|
2018-08-20 00:03:03 +02:00
|
|
|
LOG(DEBUG) << "Load string with key " << key << " from database";
|
2018-07-23 13:45:31 +02:00
|
|
|
load_language_string_unsafe(language, key, value);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
return have_all;
|
2018-07-23 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
td_api::object_ptr<td_api::LanguagePackStringValue> LanguagePackManager::get_language_pack_string_value_object(
|
|
|
|
const string &value) {
|
|
|
|
return td_api::make_object<td_api::languagePackStringValueOrdinary>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::LanguagePackStringValue> LanguagePackManager::get_language_pack_string_value_object(
|
|
|
|
const PluralizedString &value) {
|
|
|
|
return td_api::make_object<td_api::languagePackStringValuePluralized>(
|
|
|
|
value.zero_value_, value.one_value_, value.two_value_, value.few_value_, value.many_value_, value.other_value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::LanguagePackStringValue> LanguagePackManager::get_language_pack_string_value_object() {
|
|
|
|
return td_api::make_object<td_api::languagePackStringValueDeleted>();
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::languagePackString> LanguagePackManager::get_language_pack_string_object(
|
2022-02-07 22:04:34 +01:00
|
|
|
const string &key, const string &value) {
|
|
|
|
return td_api::make_object<td_api::languagePackString>(key, get_language_pack_string_value_object(value));
|
2022-02-07 20:41:07 +01:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
td_api::object_ptr<td_api::languagePackString> LanguagePackManager::get_language_pack_string_object(
|
2022-02-07 22:04:34 +01:00
|
|
|
const string &key, const PluralizedString &value) {
|
|
|
|
return td_api::make_object<td_api::languagePackString>(key, get_language_pack_string_value_object(value));
|
2022-02-07 20:41:07 +01:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
|
2022-02-07 22:04:34 +01:00
|
|
|
td_api::object_ptr<td_api::languagePackString> LanguagePackManager::get_language_pack_string_object(const string &key) {
|
|
|
|
return td_api::make_object<td_api::languagePackString>(key, get_language_pack_string_value_object());
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
td_api::object_ptr<td_api::LanguagePackStringValue> LanguagePackManager::get_language_pack_string_value_object(
|
2018-08-23 02:45:04 +02:00
|
|
|
const Language *language, const string &key) {
|
2018-07-23 19:29:14 +02:00
|
|
|
CHECK(language != nullptr);
|
|
|
|
auto ordinary_it = language->ordinary_strings_.find(key);
|
|
|
|
if (ordinary_it != language->ordinary_strings_.end()) {
|
2018-08-22 23:29:26 +02:00
|
|
|
return get_language_pack_string_value_object(ordinary_it->second);
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
auto pluralized_it = language->pluralized_strings_.find(key);
|
|
|
|
if (pluralized_it != language->pluralized_strings_.end()) {
|
2022-02-11 17:27:32 +01:00
|
|
|
return get_language_pack_string_value_object(*pluralized_it->second);
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
LOG_IF(ERROR, !language->is_full_ && language->deleted_strings_.count(key) == 0) << "Have no string for key " << key;
|
2018-08-23 15:13:15 +02:00
|
|
|
return get_language_pack_string_value_object();
|
2018-08-22 23:29:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
td_api::object_ptr<td_api::languagePackString> LanguagePackManager::get_language_pack_string_object(
|
|
|
|
const Language *language, const string &key) {
|
2018-08-22 23:29:26 +02:00
|
|
|
return td_api::make_object<td_api::languagePackString>(key, get_language_pack_string_value_object(language, key));
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:28:11 +02:00
|
|
|
td_api::object_ptr<td_api::languagePackStrings> LanguagePackManager::get_language_pack_strings_object(
|
|
|
|
Language *language, const vector<string> &keys) {
|
|
|
|
CHECK(language != nullptr);
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2018-08-22 23:29:26 +02:00
|
|
|
vector<td_api::object_ptr<td_api::languagePackString>> strings;
|
2018-07-05 20:28:11 +02:00
|
|
|
if (keys.empty()) {
|
|
|
|
for (auto &str : language->ordinary_strings_) {
|
2022-02-07 20:41:07 +01:00
|
|
|
strings.push_back(get_language_pack_string_object(str.first, str.second));
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
for (auto &str : language->pluralized_strings_) {
|
2022-02-11 17:27:32 +01:00
|
|
|
strings.push_back(get_language_pack_string_object(str.first, *str.second));
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto &key : keys) {
|
2018-07-23 19:29:14 +02:00
|
|
|
strings.push_back(get_language_pack_string_object(language, key));
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return td_api::make_object<td_api::languagePackStrings>(std::move(strings));
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:01:08 +02:00
|
|
|
void LanguagePackManager::get_languages(bool only_local,
|
|
|
|
Promise<td_api::object_ptr<td_api::localizationTargetInfo>> promise) {
|
2018-08-24 04:26:19 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 14:01:08 +02:00
|
|
|
if (only_local) {
|
|
|
|
return on_get_languages(vector<tl_object_ptr<telegram_api::langPackLanguage>>(), language_pack_, true,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
auto request_promise = PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_,
|
|
|
|
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLanguages>(std::move(r_query)));
|
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_languages, std::move(result), std::move(language_pack), false,
|
|
|
|
std::move(promise));
|
2018-08-23 02:45:04 +02:00
|
|
|
});
|
2020-03-15 23:01:14 +01:00
|
|
|
send_with_promise(G()->net_query_creator().create_unauth(telegram_api::langpack_getLanguages(language_pack_)),
|
|
|
|
std::move(request_promise));
|
2018-08-23 02:45:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
void LanguagePackManager::search_language_info(string language_code,
|
|
|
|
Promise<td_api::object_ptr<td_api::languagePackInfo>> promise) {
|
|
|
|
if (language_pack_.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto request_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code,
|
|
|
|
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLanguage>(std::move(r_query)));
|
|
|
|
LOG(INFO) << "Receive " << to_string(result);
|
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_language, std::move(result), std::move(language_pack),
|
2019-02-12 02:15:45 +01:00
|
|
|
std::move(language_code), std::move(promise));
|
|
|
|
});
|
2020-03-15 23:01:14 +01:00
|
|
|
send_with_promise(
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::langpack_getLanguage(language_pack_, language_code)),
|
|
|
|
std::move(request_promise));
|
2019-02-12 02:15:45 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 03:22:17 +02:00
|
|
|
void LanguagePackManager::repair_chosen_language_info() {
|
|
|
|
CHECK(!language_pack_.empty() && !language_code_.empty());
|
|
|
|
if (is_custom_language_code(language_code_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
std::lock_guard<std::mutex> languages_lock(pack->mutex_);
|
|
|
|
for (auto &server_info : pack->server_language_pack_infos_) {
|
|
|
|
if (server_info.first == language_code_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Repair info about language " << language_code_;
|
|
|
|
search_language_info(language_code_, Auto());
|
|
|
|
}
|
|
|
|
|
2019-02-11 18:57:35 +01:00
|
|
|
td_api::object_ptr<td_api::languagePackInfo> LanguagePackManager::get_language_pack_info_object(
|
|
|
|
const string &language_code, const LanguageInfo &info) {
|
2019-02-23 16:29:04 +01:00
|
|
|
return td_api::make_object<td_api::languagePackInfo>(language_code, info.base_language_code_, info.name_,
|
|
|
|
info.native_name_, info.plural_code_, info.is_official_,
|
|
|
|
info.is_rtl_, info.is_beta_, false, info.total_string_count_,
|
|
|
|
info.translated_string_count_, 0, info.translation_url_);
|
2019-02-11 18:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string LanguagePackManager::get_language_info_string(const LanguageInfo &info) {
|
|
|
|
return PSTRING() << info.name_ << '\x00' << info.native_name_ << '\x00' << info.base_language_code_ << '\x00'
|
|
|
|
<< info.plural_code_ << '\x00' << info.is_official_ << '\x00' << info.is_rtl_ << '\x00'
|
|
|
|
<< info.is_beta_ << '\x00' << info.total_string_count_ << '\x00' << info.translated_string_count_
|
|
|
|
<< '\x00' << info.translation_url_;
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
void LanguagePackManager::on_get_language_info(const string &language_pack,
|
|
|
|
td_api::languagePackInfo *language_pack_info) {
|
|
|
|
CHECK(language_pack_info != nullptr);
|
|
|
|
auto language = add_language(database_, language_pack, language_pack_info->id_);
|
|
|
|
language_pack_info->local_string_count_ = language->key_count_;
|
|
|
|
SqliteKeyValue *kv = nullptr;
|
|
|
|
bool was_updated_base_language_code = false;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
|
|
|
if (language_pack_info->base_language_pack_id_ != language->base_language_code_) {
|
|
|
|
language->base_language_code_ = language_pack_info->base_language_pack_id_;
|
2022-02-09 22:59:52 +01:00
|
|
|
if (language_pack == language_pack_ && language_pack_info->id_ == language_code_) {
|
2019-02-12 02:15:45 +01:00
|
|
|
base_language_code_ = language->base_language_code_;
|
|
|
|
was_updated_base_language_code = true;
|
|
|
|
}
|
|
|
|
if (!language->kv_.empty()) {
|
|
|
|
kv = &language->kv_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was_updated_base_language_code) {
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_empty("base_language_pack_version");
|
2019-02-12 02:15:45 +01:00
|
|
|
if (!base_language_code_.empty()) {
|
|
|
|
add_language(database_, language_pack_, base_language_code_);
|
|
|
|
on_language_pack_version_changed(true, std::numeric_limits<int32>::max());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kv != nullptr) {
|
|
|
|
std::lock_guard<std::mutex> lock(database_->mutex_);
|
|
|
|
kv->set("!base_language_code", language_pack_info->base_language_pack_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::save_server_language_pack_infos(LanguagePack *pack) {
|
|
|
|
// mutexes are locked by the caller
|
|
|
|
if (pack->pack_kv_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-12 04:28:14 +01:00
|
|
|
LOG(INFO) << "Save changes server language pack infos";
|
2019-02-12 02:15:45 +01:00
|
|
|
vector<string> all_strings;
|
|
|
|
all_strings.reserve(2 * pack->server_language_pack_infos_.size());
|
|
|
|
for (auto &info : pack->server_language_pack_infos_) {
|
|
|
|
all_strings.push_back(info.first);
|
|
|
|
all_strings.push_back(get_language_info_string(info.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
pack->pack_kv_.set("!server2", implode(all_strings, '\x00'));
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
void LanguagePackManager::on_get_languages(vector<tl_object_ptr<telegram_api::langPackLanguage>> languages,
|
2018-09-04 14:01:08 +02:00
|
|
|
string language_pack, bool only_local,
|
2018-09-04 03:29:26 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::localizationTargetInfo>> promise) {
|
|
|
|
auto results = td_api::make_object<td_api::localizationTargetInfo>();
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<string> added_languages;
|
2019-02-11 18:57:35 +01:00
|
|
|
|
2019-02-23 16:29:04 +01:00
|
|
|
auto add_language_info = [&results, &added_languages](const string &language_code, const LanguageInfo &info,
|
|
|
|
bool is_installed) {
|
2022-03-11 19:38:48 +01:00
|
|
|
if (!language_code.empty() && added_languages.insert(language_code).second) {
|
2019-02-11 18:57:35 +01:00
|
|
|
results->language_packs_.push_back(get_language_pack_info_object(language_code, info));
|
2019-02-23 16:29:04 +01:00
|
|
|
results->language_packs_.back()->is_installed_ = is_installed;
|
2019-02-11 18:57:35 +01:00
|
|
|
}
|
|
|
|
};
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack);
|
|
|
|
if (pack_it != database_->language_packs_.end()) {
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
2019-02-11 19:53:10 +01:00
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
2018-09-04 14:01:08 +02:00
|
|
|
for (auto &info : pack->custom_language_pack_infos_) {
|
2019-02-23 16:29:04 +01:00
|
|
|
add_language_info(info.first, info.second, true);
|
2018-08-23 02:45:04 +02:00
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
if (only_local) {
|
|
|
|
for (auto &info : pack->server_language_pack_infos_) {
|
2019-02-23 16:29:04 +01:00
|
|
|
add_language_info(info.first, info.second, false);
|
2018-09-04 14:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
2018-08-06 19:18:28 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 14:01:08 +02:00
|
|
|
vector<std::pair<string, LanguageInfo>> all_server_infos;
|
2018-08-23 02:45:04 +02:00
|
|
|
for (auto &language : languages) {
|
2019-02-12 02:15:45 +01:00
|
|
|
auto r_info = get_language_info(language.get());
|
|
|
|
if (r_info.is_error()) {
|
2019-02-11 01:45:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
|
2019-02-23 16:29:04 +01:00
|
|
|
add_language_info(language->lang_code_, r_info.ok(), false);
|
2019-02-12 02:15:45 +01:00
|
|
|
all_server_infos.emplace_back(std::move(language->lang_code_), r_info.move_as_ok());
|
|
|
|
}
|
2019-02-11 18:57:35 +01:00
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
for (auto &language_pack_info : results->language_packs_) {
|
|
|
|
on_get_language_info(language_pack, language_pack_info.get());
|
2018-08-23 02:45:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 14:01:08 +02:00
|
|
|
if (!only_local) {
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack);
|
|
|
|
if (pack_it != database_->language_packs_.end()) {
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
2019-02-11 19:53:10 +01:00
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
2018-09-04 14:01:08 +02:00
|
|
|
if (pack->server_language_pack_infos_ != all_server_infos) {
|
2019-02-11 19:53:10 +01:00
|
|
|
for (auto &info : all_server_infos) {
|
2022-02-11 17:27:32 +01:00
|
|
|
pack->all_server_language_pack_infos_[info.first] = td::make_unique<LanguageInfo>(info.second);
|
2019-02-11 19:53:10 +01:00
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
pack->server_language_pack_infos_ = std::move(all_server_infos);
|
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
save_server_language_pack_infos(pack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
promise.set_value(std::move(results));
|
|
|
|
}
|
2018-09-04 14:01:08 +02:00
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
void LanguagePackManager::on_get_language(tl_object_ptr<telegram_api::langPackLanguage> lang_pack_language,
|
|
|
|
string language_pack, string language_code,
|
|
|
|
Promise<td_api::object_ptr<td_api::languagePackInfo>> promise) {
|
|
|
|
CHECK(lang_pack_language != nullptr);
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, language_info, get_language_info(lang_pack_language.get()));
|
|
|
|
auto result = get_language_pack_info_object(lang_pack_language->lang_code_, language_info);
|
2019-02-12 02:15:45 +01:00
|
|
|
|
|
|
|
on_get_language_info(language_pack, result.get());
|
|
|
|
|
|
|
|
// updating languages cache
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack);
|
|
|
|
if (pack_it != database_->language_packs_.end()) {
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
2019-02-23 16:29:04 +01:00
|
|
|
|
|
|
|
result->is_installed_ = pack->custom_language_pack_infos_.count(lang_pack_language->lang_code_) != 0 ||
|
|
|
|
pack->custom_language_pack_infos_.count(language_code) != 0;
|
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
bool is_changed = false;
|
|
|
|
for (auto &info : pack->server_language_pack_infos_) {
|
|
|
|
if (info.first == lang_pack_language->lang_code_ || info.first == language_code) {
|
2024-04-27 21:49:59 +02:00
|
|
|
if (!(info.second == language_info)) {
|
2019-02-12 02:15:45 +01:00
|
|
|
LOG(INFO) << "Language pack " << info.first << " was changed";
|
|
|
|
is_changed = true;
|
2024-04-27 21:49:59 +02:00
|
|
|
info.second = language_info;
|
2018-09-04 14:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 17:27:32 +01:00
|
|
|
pack->all_server_language_pack_infos_[lang_pack_language->lang_code_] =
|
2024-04-27 21:49:59 +02:00
|
|
|
td::make_unique<LanguageInfo>(std::move(language_info));
|
2019-02-12 02:15:45 +01:00
|
|
|
|
|
|
|
if (is_changed) {
|
|
|
|
save_server_language_pack_infos(pack);
|
|
|
|
}
|
2019-02-23 16:29:04 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Failed to find localization target " << language_pack;
|
2018-09-04 14:01:08 +02:00
|
|
|
}
|
2019-02-12 02:15:45 +01:00
|
|
|
|
|
|
|
promise.set_value(std::move(result));
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::get_language_pack_strings(string language_code, vector<string> keys,
|
|
|
|
Promise<td_api::object_ptr<td_api::languagePackStrings>> promise) {
|
2018-08-24 04:26:19 +02:00
|
|
|
if (!check_language_code_name(language_code) || language_code.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID is invalid"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2018-07-23 13:45:31 +02:00
|
|
|
for (auto &key : keys) {
|
|
|
|
if (!is_valid_key(key)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Invalid key name"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
Language *language = add_language(database_, language_pack_, language_code);
|
2018-07-05 20:28:11 +02:00
|
|
|
if (language_has_strings(language, keys)) {
|
|
|
|
return promise.set_value(get_language_pack_strings_object(language, keys));
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
if (load_language_strings(database_, language, keys)) {
|
|
|
|
return promise.set_value(get_language_pack_strings_object(language, keys));
|
2018-07-23 13:45:31 +02:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
|
2018-08-24 04:26:19 +02:00
|
|
|
if (is_custom_language_code(language_code)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack not found"));
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:28:11 +02:00
|
|
|
if (keys.empty()) {
|
2018-08-24 03:53:14 +02:00
|
|
|
auto &queries = get_all_language_pack_strings_queries_[language_pack_][language_code].queries_;
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() != 1) {
|
|
|
|
// send request only once
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code](
|
|
|
|
Result<td_api::object_ptr<td_api::languagePackStrings>> r_strings) mutable {
|
2019-03-20 19:13:45 +01:00
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_all_language_pack_strings, std::move(language_pack),
|
|
|
|
std::move(language_code), std::move(r_strings));
|
2018-08-24 03:53:14 +02:00
|
|
|
});
|
2018-07-03 19:28:00 +02:00
|
|
|
auto request_promise =
|
2018-07-05 20:28:11 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code,
|
2018-08-24 03:53:14 +02:00
|
|
|
promise = std::move(result_promise)](Result<NetQueryPtr> r_query) mutable {
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getLangPack>(std::move(r_query)));
|
2019-03-09 17:45:45 +01:00
|
|
|
to_lower_inplace(result->lang_code_);
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Receive language pack " << result->lang_code_ << " from version " << result->from_version_
|
|
|
|
<< " with version " << result->version_ << " of size " << result->strings_.size();
|
2018-07-05 20:28:11 +02:00
|
|
|
LOG_IF(ERROR, result->lang_code_ != language_code)
|
|
|
|
<< "Receive strings for " << result->lang_code_ << " instead of " << language_code;
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG_IF(ERROR, result->from_version_ != 0) << "Receive language pack from version " << result->from_version_;
|
2018-07-05 20:28:11 +02:00
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_language_pack_strings, std::move(language_pack),
|
2018-07-17 04:58:34 +02:00
|
|
|
std::move(language_code), result->version_, false, vector<string>(), std::move(result->strings_),
|
2018-07-05 20:28:11 +02:00
|
|
|
std::move(promise));
|
2018-07-03 19:28:00 +02:00
|
|
|
});
|
2020-03-15 23:01:14 +01:00
|
|
|
send_with_promise(
|
|
|
|
G()->net_query_creator().create_unauth(telegram_api::langpack_getLangPack(language_pack_, language_code)),
|
|
|
|
std::move(request_promise));
|
2018-07-03 19:28:00 +02:00
|
|
|
} else {
|
|
|
|
auto request_promise =
|
2018-07-05 20:28:11 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), language_pack = language_pack_, language_code, keys,
|
|
|
|
promise = std::move(promise)](Result<NetQueryPtr> r_query) mutable {
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, result, fetch_result<telegram_api::langpack_getStrings>(std::move(r_query)));
|
2018-07-05 20:28:11 +02:00
|
|
|
send_closure(actor_id, &LanguagePackManager::on_get_language_pack_strings, std::move(language_pack),
|
2024-04-27 21:49:59 +02:00
|
|
|
std::move(language_code), -1, false, std::move(keys), std::move(result), std::move(promise));
|
2018-07-03 19:28:00 +02:00
|
|
|
});
|
2020-03-15 23:01:14 +01:00
|
|
|
send_with_promise(G()->net_query_creator().create_unauth(
|
|
|
|
telegram_api::langpack_getStrings(language_pack_, language_code, std::move(keys))),
|
2018-07-03 19:28:00 +02:00
|
|
|
std::move(request_promise));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:45:09 +01:00
|
|
|
void LanguagePackManager::load_empty_language_pack(const string &language_code) {
|
|
|
|
if (is_custom_language_code(language_code)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
get_language_pack_strings(language_code, vector<string>(), Auto());
|
|
|
|
}
|
|
|
|
|
2019-02-23 18:27:14 +01:00
|
|
|
void LanguagePackManager::synchronize_language_pack(string language_code, Promise<Unit> promise) {
|
|
|
|
if (!check_language_code_name(language_code) || language_code.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID is invalid"));
|
|
|
|
}
|
|
|
|
if (language_pack_.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
|
|
|
}
|
|
|
|
if (is_custom_language_code(language_code)) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
Language *language = add_language(database_, language_pack_, language_code);
|
|
|
|
load_language_strings(database_, language, vector<string>());
|
|
|
|
|
|
|
|
int32 version = language->version_.load();
|
|
|
|
if (version == -1) {
|
|
|
|
version = 0;
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
send_language_get_difference_query(language, std::move(language_code), version, std::move(promise));
|
2019-02-23 18:27:14 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 03:53:14 +02:00
|
|
|
static td_api::object_ptr<td_api::LanguagePackStringValue> copy_language_pack_string_value(
|
|
|
|
const td_api::LanguagePackStringValue *value) {
|
|
|
|
switch (value->get_id()) {
|
|
|
|
case td_api::languagePackStringValueOrdinary::ID: {
|
|
|
|
auto old_value = static_cast<const td_api::languagePackStringValueOrdinary *>(value);
|
|
|
|
return make_tl_object<td_api::languagePackStringValueOrdinary>(old_value->value_);
|
|
|
|
}
|
|
|
|
case td_api::languagePackStringValuePluralized::ID: {
|
|
|
|
auto old_value = static_cast<const td_api::languagePackStringValuePluralized *>(value);
|
|
|
|
return make_tl_object<td_api::languagePackStringValuePluralized>(
|
|
|
|
std::move(old_value->zero_value_), std::move(old_value->one_value_), std::move(old_value->two_value_),
|
|
|
|
std::move(old_value->few_value_), std::move(old_value->many_value_), std::move(old_value->other_value_));
|
|
|
|
}
|
|
|
|
case td_api::languagePackStringValueDeleted::ID:
|
|
|
|
return make_tl_object<td_api::languagePackStringValueDeleted>();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::on_get_all_language_pack_strings(
|
|
|
|
string language_pack, string language_code, Result<td_api::object_ptr<td_api::languagePackStrings>> r_strings) {
|
|
|
|
auto &queries = get_all_language_pack_strings_queries_[language_pack][language_code].queries_;
|
|
|
|
auto promises = std::move(queries);
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
auto it = get_all_language_pack_strings_queries_.find(language_pack);
|
|
|
|
it->second.erase(language_code);
|
|
|
|
if (it->second.empty()) {
|
|
|
|
get_all_language_pack_strings_queries_.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_strings.is_error()) {
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(promises, r_strings.move_as_error());
|
2018-08-24 03:53:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto strings = r_strings.move_as_ok();
|
|
|
|
size_t left_non_empty_promise_count = 0;
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
if (promise) {
|
|
|
|
left_non_empty_promise_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
if (promise) {
|
|
|
|
if (left_non_empty_promise_count == 1) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Set last non-empty promise";
|
2018-08-24 03:53:14 +02:00
|
|
|
promise.set_value(std::move(strings));
|
|
|
|
} else {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Set non-empty promise";
|
2018-08-24 03:53:14 +02:00
|
|
|
vector<td_api::object_ptr<td_api::languagePackString>> strings_copy;
|
|
|
|
for (auto &result : strings->strings_) {
|
|
|
|
CHECK(result != nullptr);
|
|
|
|
strings_copy.push_back(td_api::make_object<td_api::languagePackString>(
|
|
|
|
result->key_, copy_language_pack_string_value(result->value_.get())));
|
|
|
|
}
|
|
|
|
promise.set_value(td_api::make_object<td_api::languagePackStrings>(std::move(strings_copy)));
|
|
|
|
}
|
|
|
|
left_non_empty_promise_count--;
|
|
|
|
} else {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Set empty promise";
|
2018-08-24 03:53:14 +02:00
|
|
|
promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(left_non_empty_promise_count == 0);
|
|
|
|
}
|
|
|
|
|
2018-07-23 19:29:14 +02:00
|
|
|
td_api::object_ptr<td_api::Object> LanguagePackManager::get_language_pack_string(const string &database_path,
|
|
|
|
const string &language_pack,
|
|
|
|
const string &language_code,
|
|
|
|
const string &key) {
|
|
|
|
if (!check_language_pack_name(language_pack) || language_pack.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return td_api::make_object<td_api::error>(400, "Localization target is invalid");
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
if (!check_language_code_name(language_code) || language_code.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return td_api::make_object<td_api::error>(400, "Language pack ID is invalid");
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
if (!is_valid_key(key)) {
|
|
|
|
return td_api::make_object<td_api::error>(400, "Key is invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> language_databases_lock(language_database_mutex_);
|
|
|
|
LanguageDatabase *database = add_language_database(database_path);
|
|
|
|
CHECK(database != nullptr);
|
|
|
|
language_databases_lock.unlock();
|
|
|
|
|
|
|
|
Language *language = add_language(database, language_pack, language_code);
|
|
|
|
vector<string> keys{key};
|
|
|
|
if (language_has_strings(language, keys)) {
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2018-08-22 23:29:26 +02:00
|
|
|
return get_language_pack_string_value_object(language, key);
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
if (load_language_strings(database, language, keys)) {
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2018-08-22 23:29:26 +02:00
|
|
|
return get_language_pack_string_value_object(language, key);
|
2018-07-23 19:29:14 +02:00
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::error>(404, "Not Found");
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:22:19 +02:00
|
|
|
bool LanguagePackManager::is_valid_key(Slice key) {
|
|
|
|
for (auto c : key) {
|
|
|
|
if (!is_alnum(c) && c != '_' && c != '.' && c != '-') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !key.empty();
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
void LanguagePackManager::save_strings_to_database(SqliteKeyValue *kv, int32 new_version, bool new_is_full,
|
2021-10-19 17:11:16 +02:00
|
|
|
int32 new_key_count, vector<std::pair<string, string>> &&strings) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Save to database a language pack with new version " << new_version << " and " << strings.size()
|
2018-08-23 02:45:04 +02:00
|
|
|
<< " new strings";
|
2018-07-22 19:22:19 +02:00
|
|
|
if (new_version == -1 && strings.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(database_->mutex_);
|
2018-08-23 02:45:04 +02:00
|
|
|
CHECK(kv != nullptr);
|
2018-07-23 13:45:31 +02:00
|
|
|
if (kv->empty()) {
|
2018-08-20 00:03:03 +02:00
|
|
|
LOG(DEBUG) << "There is no associated database key-value";
|
2018-07-23 13:45:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-07-22 19:22:19 +02:00
|
|
|
auto old_version = load_database_language_version(kv);
|
2018-08-22 22:11:54 +02:00
|
|
|
if (old_version > new_version || (old_version == new_version && strings.empty())) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Language pack version doesn't increased from " << old_version;
|
2018-07-22 19:22:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
kv->begin_write_transaction().ensure();
|
2021-10-19 17:11:16 +02:00
|
|
|
for (const auto &str : strings) {
|
2018-07-22 19:22:19 +02:00
|
|
|
if (!is_valid_key(str.first)) {
|
|
|
|
LOG(ERROR) << "Have invalid key \"" << str.first << '"';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
if (new_is_full && str.second == "3") {
|
2018-07-23 13:45:31 +02:00
|
|
|
kv->erase(str.first);
|
|
|
|
} else {
|
|
|
|
kv->set(str.first, str.second);
|
|
|
|
}
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Save language pack string with key " << str.first << " to database";
|
2018-07-22 19:22:19 +02:00
|
|
|
}
|
|
|
|
if (old_version != new_version) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Set language pack version in database to " << new_version;
|
2018-07-22 19:22:19 +02:00
|
|
|
kv->set("!version", to_string(new_version));
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
if (new_key_count != -1) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(DEBUG) << "Set language pack key count in database to " << new_key_count;
|
2018-08-23 02:45:04 +02:00
|
|
|
kv->set("!key_count", to_string(new_key_count));
|
|
|
|
}
|
2018-07-24 02:13:24 +02:00
|
|
|
kv->commit_transaction().ensure();
|
2018-07-22 19:22:19 +02:00
|
|
|
}
|
|
|
|
|
2018-07-03 19:28:00 +02:00
|
|
|
void LanguagePackManager::on_get_language_pack_strings(
|
2021-10-19 17:11:16 +02:00
|
|
|
string language_pack, string language_code, int32 version, bool is_diff, vector<string> &&keys,
|
2018-07-05 20:28:11 +02:00
|
|
|
vector<tl_object_ptr<telegram_api::LangPackString>> results,
|
2018-07-03 19:28:00 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::languagePackStrings>> promise) {
|
2018-07-20 15:15:19 +02:00
|
|
|
Language *language = get_language(database_, language_pack, language_code);
|
2018-07-17 04:58:34 +02:00
|
|
|
bool is_version_changed = false;
|
2018-07-22 19:22:19 +02:00
|
|
|
int32 new_database_version = -1;
|
2018-08-23 02:45:04 +02:00
|
|
|
int32 new_key_count = -1;
|
|
|
|
bool new_is_full = false;
|
2018-07-22 19:22:19 +02:00
|
|
|
vector<std::pair<string, string>> database_strings;
|
2018-08-22 22:11:54 +02:00
|
|
|
if (language == nullptr || language->version_ < version || !keys.empty()) {
|
2018-07-05 20:28:11 +02:00
|
|
|
if (language == nullptr) {
|
2018-07-20 15:15:19 +02:00
|
|
|
language = add_language(database_, language_pack, language_code);
|
2018-07-05 20:28:11 +02:00
|
|
|
CHECK(language != nullptr);
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
load_language_strings(database_, language, keys);
|
|
|
|
|
2018-07-05 20:28:11 +02:00
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
2018-08-23 02:45:04 +02:00
|
|
|
int32 key_count_delta = 0;
|
2018-07-05 20:28:11 +02:00
|
|
|
if (language->version_ < version || !keys.empty()) {
|
2023-03-22 19:40:05 +01:00
|
|
|
auto is_first = language->version_ == -1;
|
2018-08-22 23:29:26 +02:00
|
|
|
vector<td_api::object_ptr<td_api::languagePackString>> strings;
|
2019-02-23 18:27:14 +01:00
|
|
|
if (language->version_ < version) {
|
2018-09-04 03:29:26 +02:00
|
|
|
LOG(INFO) << "Set language pack " << language_code << " version to " << version;
|
2018-07-05 20:28:11 +02:00
|
|
|
language->version_ = version;
|
2018-07-22 19:22:19 +02:00
|
|
|
new_database_version = version;
|
2018-07-17 04:58:34 +02:00
|
|
|
is_version_changed = true;
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
2018-07-17 04:58:34 +02:00
|
|
|
|
2018-07-05 20:28:11 +02:00
|
|
|
for (auto &result : results) {
|
|
|
|
CHECK(result != nullptr);
|
|
|
|
switch (result->get_id()) {
|
2018-07-03 19:28:00 +02:00
|
|
|
case telegram_api::langPackString::ID: {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto str = telegram_api::move_object_as<telegram_api::langPackString>(result);
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!is_valid_key(str->key_)) {
|
|
|
|
LOG(ERROR) << "Receive invalid key \"" << str->key_ << '"';
|
|
|
|
break;
|
|
|
|
}
|
2018-07-22 19:22:19 +02:00
|
|
|
auto it = language->ordinary_strings_.find(str->key_);
|
|
|
|
if (it == language->ordinary_strings_.end()) {
|
2018-08-23 02:45:04 +02:00
|
|
|
key_count_delta++;
|
2018-07-22 19:22:19 +02:00
|
|
|
it = language->ordinary_strings_.emplace(str->key_, std::move(str->value_)).first;
|
|
|
|
} else {
|
|
|
|
it->second = std::move(str->value_);
|
|
|
|
}
|
2018-08-24 15:16:13 +02:00
|
|
|
key_count_delta -= static_cast<int32>(language->pluralized_strings_.erase(str->key_));
|
2018-07-06 01:42:25 +02:00
|
|
|
language->deleted_strings_.erase(str->key_);
|
2018-07-17 04:58:34 +02:00
|
|
|
if (is_diff) {
|
2022-02-07 20:41:07 +01:00
|
|
|
strings.push_back(get_language_pack_string_object(it->first, it->second));
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
database_strings.emplace_back(std::move(str->key_), PSTRING() << '1' << it->second);
|
2018-07-05 20:28:11 +02:00
|
|
|
break;
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
|
|
|
case telegram_api::langPackStringPluralized::ID: {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto str = telegram_api::move_object_as<telegram_api::langPackStringPluralized>(result);
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!is_valid_key(str->key_)) {
|
|
|
|
LOG(ERROR) << "Receive invalid key \"" << str->key_ << '"';
|
|
|
|
break;
|
|
|
|
}
|
2022-02-11 17:27:32 +01:00
|
|
|
auto value = td::make_unique<PluralizedString>(std::move(str->zero_value_), std::move(str->one_value_),
|
|
|
|
std::move(str->two_value_), std::move(str->few_value_),
|
|
|
|
std::move(str->many_value_), std::move(str->other_value_));
|
2018-07-22 19:22:19 +02:00
|
|
|
auto it = language->pluralized_strings_.find(str->key_);
|
|
|
|
if (it == language->pluralized_strings_.end()) {
|
2018-08-23 02:45:04 +02:00
|
|
|
key_count_delta++;
|
2018-07-22 19:22:19 +02:00
|
|
|
it = language->pluralized_strings_.emplace(str->key_, std::move(value)).first;
|
|
|
|
} else {
|
|
|
|
it->second = std::move(value);
|
|
|
|
}
|
2018-08-24 15:16:13 +02:00
|
|
|
key_count_delta -= static_cast<int32>(language->ordinary_strings_.erase(str->key_));
|
2018-07-06 01:42:25 +02:00
|
|
|
language->deleted_strings_.erase(str->key_);
|
2018-07-17 04:58:34 +02:00
|
|
|
if (is_diff) {
|
2022-02-11 17:27:32 +01:00
|
|
|
strings.push_back(get_language_pack_string_object(it->first, *it->second));
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
2018-07-22 19:22:19 +02:00
|
|
|
database_strings.emplace_back(
|
2021-10-19 17:11:16 +02:00
|
|
|
std::move(str->key_), PSTRING()
|
2022-02-11 17:27:32 +01:00
|
|
|
<< '2' << it->second->zero_value_ << '\x00' << it->second->one_value_
|
|
|
|
<< '\x00' << it->second->two_value_ << '\x00' << it->second->few_value_
|
|
|
|
<< '\x00' << it->second->many_value_ << '\x00' << it->second->other_value_);
|
2018-07-05 20:28:11 +02:00
|
|
|
break;
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
|
|
|
case telegram_api::langPackStringDeleted::ID: {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto str = telegram_api::move_object_as<telegram_api::langPackStringDeleted>(result);
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!is_valid_key(str->key_)) {
|
|
|
|
LOG(ERROR) << "Receive invalid key \"" << str->key_ << '"';
|
|
|
|
break;
|
|
|
|
}
|
2018-08-24 15:16:13 +02:00
|
|
|
key_count_delta -= static_cast<int32>(language->ordinary_strings_.erase(str->key_));
|
|
|
|
key_count_delta -= static_cast<int32>(language->pluralized_strings_.erase(str->key_));
|
2021-10-19 17:11:16 +02:00
|
|
|
language->deleted_strings_.insert(str->key_);
|
2018-07-17 04:58:34 +02:00
|
|
|
if (is_diff) {
|
|
|
|
strings.push_back(get_language_pack_string_object(str->key_));
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
database_strings.emplace_back(std::move(str->key_), "3");
|
2018-07-05 20:28:11 +02:00
|
|
|
break;
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-07-05 20:28:11 +02:00
|
|
|
break;
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
2018-07-23 13:45:31 +02:00
|
|
|
if (!language->is_full_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
for (const auto &key : keys) {
|
2018-07-23 13:45:31 +02:00
|
|
|
if (!language_has_string_unsafe(language, key)) {
|
|
|
|
LOG(ERROR) << "Doesn't receive key " << key << " from server";
|
|
|
|
language->deleted_strings_.insert(key);
|
|
|
|
if (is_diff) {
|
|
|
|
strings.push_back(get_language_pack_string_object(key));
|
|
|
|
}
|
|
|
|
database_strings.emplace_back(key, "3");
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
if (key_count_delta != 0) {
|
|
|
|
new_key_count = language->key_count_ + key_count_delta;
|
|
|
|
language->key_count_ = new_key_count;
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:45:31 +02:00
|
|
|
if (keys.empty() && !is_diff) {
|
|
|
|
CHECK(new_database_version >= 0);
|
|
|
|
language->is_full_ = true;
|
|
|
|
language->deleted_strings_.clear();
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
new_is_full = language->is_full_;
|
2023-03-22 19:40:05 +01:00
|
|
|
|
|
|
|
if (is_diff || (new_is_full && is_first)) {
|
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
td_api::make_object<td_api::updateLanguagePackStrings>(language_pack, language_code, std::move(strings)));
|
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 02:45:04 +02:00
|
|
|
if (is_custom_language_code(language_code) && new_database_version == -1) {
|
|
|
|
new_database_version = 1;
|
|
|
|
}
|
2018-07-22 19:22:19 +02:00
|
|
|
|
2018-08-23 02:45:04 +02:00
|
|
|
save_strings_to_database(&language->kv_, new_database_version, new_is_full, new_key_count,
|
|
|
|
std::move(database_strings));
|
2018-07-22 19:22:19 +02:00
|
|
|
|
2018-07-17 04:58:34 +02:00
|
|
|
if (is_diff) {
|
|
|
|
CHECK(language != nullptr);
|
2019-02-23 18:27:14 +01:00
|
|
|
vector<Promise<Unit>> get_difference_queries;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
|
|
|
if (language->has_get_difference_query_) {
|
|
|
|
language->has_get_difference_query_ = false;
|
|
|
|
get_difference_queries = std::move(language->get_difference_queries_);
|
|
|
|
reset_to_empty(language->get_difference_queries_);
|
|
|
|
is_version_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &query : get_difference_queries) {
|
|
|
|
query.set_value(Unit());
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-11 01:45:09 +01:00
|
|
|
if (is_version_changed && language_pack == language_pack_ &&
|
|
|
|
(language_code == language_code_ || language_code == base_language_code_)) {
|
|
|
|
send_closure_later(actor_id(this), &LanguagePackManager::on_language_pack_version_changed,
|
|
|
|
language_code != language_code_, -1);
|
2018-07-17 04:58:34 +02:00
|
|
|
}
|
2018-07-05 20:28:11 +02:00
|
|
|
|
2018-07-17 04:58:34 +02:00
|
|
|
if (promise) {
|
|
|
|
promise.set_value(get_language_pack_strings_object(language, keys));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 18:27:14 +01:00
|
|
|
void LanguagePackManager::on_failed_get_difference(string language_pack, string language_code, Status error) {
|
2018-07-20 15:15:19 +02:00
|
|
|
Language *language = get_language(database_, language_pack, language_code);
|
2018-07-17 04:58:34 +02:00
|
|
|
CHECK(language != nullptr);
|
2019-02-23 18:27:14 +01:00
|
|
|
vector<Promise<Unit>> get_difference_queries;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(language->mutex_);
|
|
|
|
if (language->has_get_difference_query_) {
|
|
|
|
language->has_get_difference_query_ = false;
|
|
|
|
if (language_pack == language_pack_ &&
|
|
|
|
(language_code == language_code_ || language_code == base_language_code_)) {
|
|
|
|
send_closure_later(actor_id(this), &LanguagePackManager::on_language_pack_version_changed,
|
|
|
|
language_code != language_code_, -1);
|
|
|
|
}
|
|
|
|
get_difference_queries = std::move(language->get_difference_queries_);
|
|
|
|
reset_to_empty(language->get_difference_queries_);
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(get_difference_queries, std::move(error));
|
2018-07-03 19:28:00 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 19:53:10 +01:00
|
|
|
void LanguagePackManager::add_custom_server_language(string language_code, Promise<Unit> &&promise) {
|
|
|
|
if (language_pack_.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
|
|
|
}
|
|
|
|
if (!check_language_code_name(language_code)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID must contain only letters, digits and hyphen"));
|
|
|
|
}
|
|
|
|
if (is_custom_language_code(language_code)) {
|
|
|
|
return promise.set_error(
|
|
|
|
Status::Error(400, "Custom local language pack can't be added through addCustomServerLanguagePack"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_language(database_, language_pack_, language_code) == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Language pack not found"));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
|
|
|
auto it = pack->all_server_language_pack_infos_.find(language_code);
|
|
|
|
if (it == pack->all_server_language_pack_infos_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Language pack info not found"));
|
|
|
|
}
|
|
|
|
auto &info = pack->custom_language_pack_infos_[language_code];
|
2022-02-11 17:27:32 +01:00
|
|
|
info = *it->second;
|
2019-02-11 19:53:10 +01:00
|
|
|
if (!pack->pack_kv_.empty()) {
|
|
|
|
pack->pack_kv_.set(language_code, get_language_info_string(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:11:54 +02:00
|
|
|
Result<tl_object_ptr<telegram_api::LangPackString>> LanguagePackManager::convert_to_telegram_api(
|
2018-08-22 23:29:26 +02:00
|
|
|
tl_object_ptr<td_api::languagePackString> &&str) {
|
2018-08-22 22:11:54 +02:00
|
|
|
if (str == nullptr) {
|
2021-09-28 16:31:00 +02:00
|
|
|
return Status::Error(400, "Language pack strings must be non-empty");
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
string key = std::move(str->key_);
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!is_valid_key(key)) {
|
|
|
|
return Status::Error(400, "Key is invalid");
|
|
|
|
}
|
2018-08-22 23:29:26 +02:00
|
|
|
|
|
|
|
if (str->value_ == nullptr) {
|
|
|
|
return make_tl_object<telegram_api::langPackStringDeleted>(std::move(key));
|
|
|
|
}
|
|
|
|
switch (str->value_->get_id()) {
|
|
|
|
case td_api::languagePackStringValueOrdinary::ID: {
|
|
|
|
auto value = static_cast<td_api::languagePackStringValueOrdinary *>(str->value_.get());
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!clean_input_string(value->value_)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return Status::Error(400, "Language pack string value must be encoded in UTF-8");
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::langPackString>(std::move(key), std::move(value->value_));
|
|
|
|
}
|
2018-08-22 23:29:26 +02:00
|
|
|
case td_api::languagePackStringValuePluralized::ID: {
|
|
|
|
auto value = static_cast<td_api::languagePackStringValuePluralized *>(str->value_.get());
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!clean_input_string(value->zero_value_) || !clean_input_string(value->one_value_) ||
|
|
|
|
!clean_input_string(value->two_value_) || !clean_input_string(value->few_value_) ||
|
|
|
|
!clean_input_string(value->many_value_) || !clean_input_string(value->other_value_)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return Status::Error(400, "Language pack string value must be encoded in UTF-8");
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::langPackStringPluralized>(
|
|
|
|
31, std::move(key), std::move(value->zero_value_), std::move(value->one_value_), std::move(value->two_value_),
|
|
|
|
std::move(value->few_value_), std::move(value->many_value_), std::move(value->other_value_));
|
|
|
|
}
|
2018-08-22 23:29:26 +02:00
|
|
|
case td_api::languagePackStringValueDeleted::ID:
|
2018-08-22 22:11:54 +02:00
|
|
|
// there is no reason to save deleted strings in a custom language pack to database
|
|
|
|
return make_tl_object<telegram_api::langPackStringDeleted>(std::move(key));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:15:45 +01:00
|
|
|
Result<LanguagePackManager::LanguageInfo> LanguagePackManager::get_language_info(
|
|
|
|
telegram_api::langPackLanguage *language) {
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!check_language_code_name(language->lang_code_) || language->lang_code_.empty()) {
|
2019-02-12 02:15:45 +01:00
|
|
|
LOG(ERROR) << "Receive unsupported language pack ID " << language->lang_code_ << " from server";
|
|
|
|
return Status::Error(500, "Unsupported language pack ID");
|
|
|
|
}
|
|
|
|
if (is_custom_language_code(language->lang_code_)) {
|
|
|
|
LOG(ERROR) << "Receive custom language pack ID \"" << language->lang_code_ << "\" from server";
|
|
|
|
return Status::Error(500, "Unallowed custom language pack ID");
|
|
|
|
}
|
2019-03-09 17:45:45 +01:00
|
|
|
to_lower_inplace(language->lang_code_);
|
2019-02-12 02:15:45 +01:00
|
|
|
|
|
|
|
LanguageInfo info;
|
|
|
|
info.name_ = std::move(language->name_);
|
|
|
|
info.native_name_ = std::move(language->native_name_);
|
|
|
|
info.base_language_code_ = std::move(language->base_lang_code_);
|
|
|
|
info.plural_code_ = std::move(language->plural_code_);
|
2021-11-01 19:53:23 +01:00
|
|
|
info.is_official_ = language->official_;
|
|
|
|
info.is_rtl_ = language->rtl_;
|
|
|
|
info.is_beta_ = language->beta_;
|
2019-02-12 02:15:45 +01:00
|
|
|
info.is_from_database_ = false;
|
|
|
|
info.total_string_count_ = language->strings_count_;
|
|
|
|
info.translated_string_count_ = language->translated_count_;
|
|
|
|
info.translation_url_ = language->translations_url_;
|
|
|
|
|
|
|
|
if (!check_language_code_name(info.base_language_code_)) {
|
|
|
|
LOG(ERROR) << "Have invalid base language pack ID \"" << info.base_language_code_ << '"';
|
|
|
|
info.base_language_code_.clear();
|
|
|
|
}
|
|
|
|
if (is_custom_language_code(info.base_language_code_)) {
|
|
|
|
LOG(ERROR) << "Receive custom base language pack ID \"" << info.base_language_code_ << "\" from server";
|
|
|
|
info.base_language_code_.clear();
|
|
|
|
}
|
|
|
|
if (info.base_language_code_ == language->lang_code_) {
|
|
|
|
LOG(ERROR) << "Receive language pack \"" << info.base_language_code_ << "\"based on self";
|
|
|
|
info.base_language_code_.clear();
|
|
|
|
}
|
|
|
|
return std::move(info);
|
|
|
|
}
|
|
|
|
|
2019-02-11 18:57:35 +01:00
|
|
|
Result<LanguagePackManager::LanguageInfo> LanguagePackManager::get_language_info(
|
|
|
|
td_api::languagePackInfo *language_pack_info) {
|
|
|
|
if (language_pack_info == nullptr) {
|
2020-05-18 21:54:18 +02:00
|
|
|
return Status::Error(400, "Language pack info must be non-empty");
|
2019-02-11 18:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!clean_input_string(language_pack_info->id_)) {
|
|
|
|
return Status::Error(400, "Language pack ID must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(language_pack_info->base_language_pack_id_)) {
|
|
|
|
return Status::Error(400, "Base language pack ID must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(language_pack_info->name_)) {
|
|
|
|
return Status::Error(400, "Language pack name must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(language_pack_info->native_name_)) {
|
|
|
|
return Status::Error(400, "Language pack native name must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(language_pack_info->plural_code_)) {
|
|
|
|
return Status::Error(400, "Language pack plural code must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(language_pack_info->translation_url_)) {
|
2023-01-15 10:00:26 +01:00
|
|
|
return Status::Error(400, "Language pack translation URL must be encoded in UTF-8");
|
2019-02-11 18:57:35 +01:00
|
|
|
}
|
|
|
|
if (language_pack_info->total_string_count_ < 0) {
|
|
|
|
language_pack_info->total_string_count_ = 0;
|
|
|
|
}
|
|
|
|
if (language_pack_info->translated_string_count_ < 0) {
|
|
|
|
language_pack_info->translated_string_count_ = 0;
|
|
|
|
}
|
|
|
|
if (!check_language_code_name(language_pack_info->id_)) {
|
|
|
|
return Status::Error(400, "Language pack ID must contain only letters, digits and hyphen");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_custom_language_code(language_pack_info->id_)) {
|
|
|
|
language_pack_info->base_language_pack_id_.clear();
|
|
|
|
language_pack_info->is_official_ = false;
|
|
|
|
language_pack_info->is_rtl_ = false;
|
|
|
|
language_pack_info->is_beta_ = false;
|
|
|
|
language_pack_info->translation_url_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageInfo info;
|
|
|
|
info.name_ = std::move(language_pack_info->name_);
|
|
|
|
info.native_name_ = std::move(language_pack_info->native_name_);
|
|
|
|
info.base_language_code_ = std::move(language_pack_info->base_language_pack_id_);
|
|
|
|
info.plural_code_ = std::move(language_pack_info->plural_code_);
|
|
|
|
info.is_official_ = language_pack_info->is_official_;
|
|
|
|
info.is_rtl_ = language_pack_info->is_rtl_;
|
|
|
|
info.is_beta_ = language_pack_info->is_beta_;
|
2019-02-12 02:15:45 +01:00
|
|
|
info.is_from_database_ = true;
|
2019-02-11 18:57:35 +01:00
|
|
|
info.total_string_count_ = language_pack_info->total_string_count_;
|
|
|
|
info.translated_string_count_ = language_pack_info->translated_string_count_;
|
|
|
|
info.translation_url_ = std::move(language_pack_info->translation_url_);
|
|
|
|
|
|
|
|
return std::move(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::set_custom_language(td_api::object_ptr<td_api::languagePackInfo> &&language_pack_info,
|
2018-08-22 23:29:26 +02:00
|
|
|
vector<tl_object_ptr<td_api::languagePackString>> strings,
|
2018-08-06 16:22:22 +02:00
|
|
|
Promise<Unit> &&promise) {
|
2018-08-24 04:26:19 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2019-02-11 18:57:35 +01:00
|
|
|
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, language_info, get_language_info(language_pack_info.get()));
|
2019-02-11 18:57:35 +01:00
|
|
|
auto language_code = std::move(language_pack_info->id_);
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!is_custom_language_code(language_code)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack ID must begin with 'X'"));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
2018-08-06 16:22:22 +02:00
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
|
|
|
|
for (auto &str : strings) {
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, result, convert_to_telegram_api(std::move(str)));
|
|
|
|
server_strings.push_back(std::move(result));
|
2018-08-06 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:18:28 +02:00
|
|
|
// TODO atomic replace
|
2018-08-06 16:22:22 +02:00
|
|
|
do_delete_language(language_code).ensure();
|
|
|
|
on_get_language_pack_strings(language_pack_, language_code, 1, false, vector<string>(), std::move(server_strings),
|
|
|
|
Auto());
|
2018-08-06 19:18:28 +02:00
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
2019-02-11 19:53:10 +01:00
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
2018-09-04 14:01:08 +02:00
|
|
|
auto &info = pack->custom_language_pack_infos_[language_code];
|
2024-04-27 21:49:59 +02:00
|
|
|
info = std::move(language_info);
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!pack->pack_kv_.empty()) {
|
2019-02-11 18:57:35 +01:00
|
|
|
pack->pack_kv_.set(language_code, get_language_info_string(info));
|
2018-08-31 19:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2019-02-11 18:57:35 +01:00
|
|
|
void LanguagePackManager::edit_custom_language_info(td_api::object_ptr<td_api::languagePackInfo> &&language_pack_info,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-08-31 19:18:12 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-31 19:18:12 +02:00
|
|
|
}
|
2019-02-11 18:57:35 +01:00
|
|
|
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, language_info, get_language_info(language_pack_info.get()));
|
2019-02-11 18:57:35 +01:00
|
|
|
auto language_code = std::move(language_pack_info->id_);
|
2018-08-31 19:18:12 +02:00
|
|
|
if (!is_custom_language_code(language_code)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack ID must begin with 'X'"));
|
2018-08-31 19:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
|
|
|
LanguagePack *pack = pack_it->second.get();
|
2019-02-11 19:53:10 +01:00
|
|
|
std::lock_guard<std::mutex> pack_lock(pack->mutex_);
|
2018-09-04 14:01:08 +02:00
|
|
|
auto language_info_it = pack->custom_language_pack_infos_.find(language_code);
|
|
|
|
if (language_info_it == pack->custom_language_pack_infos_.end()) {
|
2018-08-31 19:18:12 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack is not found"));
|
|
|
|
}
|
|
|
|
auto &info = language_info_it->second;
|
2024-04-27 21:49:59 +02:00
|
|
|
info = std::move(language_info);
|
2018-08-31 19:18:12 +02:00
|
|
|
if (!pack->pack_kv_.empty()) {
|
2019-02-11 18:57:35 +01:00
|
|
|
pack->pack_kv_.set(language_code, get_language_info_string(info));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::set_custom_language_string(string language_code,
|
2018-08-22 23:29:26 +02:00
|
|
|
tl_object_ptr<td_api::languagePackString> str,
|
2018-08-22 22:11:54 +02:00
|
|
|
Promise<Unit> &&promise) {
|
2018-08-24 04:26:19 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2018-08-22 22:11:54 +02:00
|
|
|
if (!check_language_code_name(language_code)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID must contain only letters, digits and hyphen"));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
if (!is_custom_language_code(language_code)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack ID must begin with 'X'"));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (get_language(database_, language_pack_, language_code) == nullptr) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom language pack not found"));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
2018-08-22 23:29:26 +02:00
|
|
|
if (str == nullptr) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Language pack strings must not be null"));
|
2018-08-22 22:11:54 +02:00
|
|
|
}
|
2018-08-06 19:18:28 +02:00
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
vector<string> keys{str->key_};
|
|
|
|
|
2024-04-27 21:49:59 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, lang_pack_string, convert_to_telegram_api(std::move(str)));
|
2018-08-22 22:11:54 +02:00
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::LangPackString>> server_strings;
|
2024-04-27 21:49:59 +02:00
|
|
|
server_strings.push_back(std::move(lang_pack_string));
|
2018-08-22 22:11:54 +02:00
|
|
|
|
2018-08-22 23:29:26 +02:00
|
|
|
on_get_language_pack_strings(language_pack_, language_code, 1, true, std::move(keys), std::move(server_strings),
|
2018-08-22 22:11:54 +02:00
|
|
|
Auto());
|
2018-08-06 16:22:22 +02:00
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::delete_language(string language_code, Promise<Unit> &&promise) {
|
2018-08-24 04:26:19 +02:00
|
|
|
if (language_pack_.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Option \"localization_target\" needs to be set first"));
|
2018-08-24 04:26:19 +02:00
|
|
|
}
|
2018-08-06 19:18:28 +02:00
|
|
|
if (!check_language_code_name(language_code)) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID is invalid"));
|
2018-08-06 19:18:28 +02:00
|
|
|
}
|
|
|
|
if (language_code.empty()) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Language pack ID is empty"));
|
2018-08-06 19:18:28 +02:00
|
|
|
}
|
2019-02-11 01:45:09 +01:00
|
|
|
if (language_code_ == language_code || base_language_code_ == language_code) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Currently used language pack can't be deleted"));
|
2018-08-06 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto status = do_delete_language(language_code);
|
|
|
|
if (status.is_error()) {
|
|
|
|
promise.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
Status LanguagePackManager::do_delete_language(const string &language_code) {
|
2018-08-06 19:18:28 +02:00
|
|
|
add_language(database_, language_pack_, language_code);
|
|
|
|
|
2018-08-06 16:22:22 +02:00
|
|
|
std::lock_guard<std::mutex> packs_lock(database_->mutex_);
|
|
|
|
auto pack_it = database_->language_packs_.find(language_pack_);
|
2018-08-06 19:18:28 +02:00
|
|
|
CHECK(pack_it != database_->language_packs_.end());
|
2018-08-06 16:22:22 +02:00
|
|
|
LanguagePack *pack = pack_it->second.get();
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> languages_lock(pack->mutex_);
|
|
|
|
auto code_it = pack->languages_.find(language_code);
|
2018-08-06 19:18:28 +02:00
|
|
|
CHECK(code_it != pack->languages_.end());
|
2018-08-06 16:22:22 +02:00
|
|
|
auto language = code_it->second.get();
|
|
|
|
if (language->has_get_difference_query_) {
|
2018-09-04 03:29:26 +02:00
|
|
|
return Status::Error(400, "Language pack can't be deleted now, try again later");
|
2018-08-06 16:22:22 +02:00
|
|
|
}
|
|
|
|
if (!language->kv_.empty()) {
|
|
|
|
language->kv_.drop().ignore();
|
2018-08-06 19:18:28 +02:00
|
|
|
CHECK(language->kv_.empty());
|
2018-08-20 00:03:03 +02:00
|
|
|
CHECK(!database_->database_.empty());
|
|
|
|
language->kv_
|
|
|
|
.init_with_connection(database_->database_.clone(), get_database_table_name(language_pack_, language_code))
|
|
|
|
.ensure();
|
2018-08-06 16:22:22 +02:00
|
|
|
}
|
2018-08-06 19:18:28 +02:00
|
|
|
std::lock_guard<std::mutex> language_lock(language->mutex_);
|
|
|
|
language->version_ = -1;
|
2018-08-23 02:45:04 +02:00
|
|
|
language->key_count_ = load_database_language_key_count(&language->kv_);
|
2018-08-06 19:18:28 +02:00
|
|
|
language->is_full_ = false;
|
|
|
|
language->ordinary_strings_.clear();
|
|
|
|
language->pluralized_strings_.clear();
|
|
|
|
language->deleted_strings_.clear();
|
|
|
|
|
2019-02-11 18:57:35 +01:00
|
|
|
if (!pack->pack_kv_.empty()) {
|
|
|
|
pack->pack_kv_.erase(language_code);
|
2018-08-06 19:18:28 +02:00
|
|
|
}
|
2019-02-11 18:57:35 +01:00
|
|
|
pack->custom_language_pack_infos_.erase(language_code);
|
2018-08-06 19:18:28 +02:00
|
|
|
|
2018-08-06 16:22:22 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:28:00 +02:00
|
|
|
void LanguagePackManager::on_result(NetQueryPtr query) {
|
|
|
|
auto token = get_link_token();
|
|
|
|
container_.extract(token).set_value(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise) {
|
|
|
|
auto id = container_.create(std::move(promise));
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguagePackManager::hangup() {
|
|
|
|
container_.for_each(
|
2021-10-07 15:36:21 +02:00
|
|
|
[](auto id, Promise<NetQueryPtr> &promise) { promise.set_error(Global::request_aborted_error()); });
|
2018-07-03 19:28:00 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2018-07-19 00:14:20 +02:00
|
|
|
int32 LanguagePackManager::manager_count_ = 0;
|
2018-07-20 15:15:19 +02:00
|
|
|
std::mutex LanguagePackManager::language_database_mutex_;
|
2022-11-23 17:37:32 +01:00
|
|
|
std::unordered_map<string, unique_ptr<LanguagePackManager::LanguageDatabase>, Hash<string>>
|
|
|
|
LanguagePackManager::language_databases_;
|
2018-07-05 20:28:11 +02:00
|
|
|
|
2018-07-03 19:28:00 +02:00
|
|
|
} // namespace td
|