Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon titles and descriptions made translatable #4984

Merged
merged 6 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/core/about.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,9 @@
[entry]
name = "Anja Keicher (ayne)"
[/entry]
[entry]
name = "Artem Khrapov (kabachuha)"
[/entry]
[entry]
name = "Astrid Halberkamp"
[/entry]
Expand Down
2 changes: 1 addition & 1 deletion src/addon/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ bool addons_client::try_fetch_addon(const addon_info & addon)
config archive;

if(!(
download_addon(archive, addon.id, addon.title, !is_addon_installed(addon.id)) &&
download_addon(archive, addon.id, addon.display_title_full(), !is_addon_installed(addon.id)) &&
install_addon(archive, addon)
)) {
const std::string& server_error = get_last_server_error();
Expand Down
84 changes: 81 additions & 3 deletions src/addon/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "config.hpp"
#include "font/pango/escape.hpp"
#include "gettext.hpp"
#include "language.hpp"
#include "picture.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
Expand Down Expand Up @@ -57,6 +58,20 @@ namespace {
}
}

void addon_info_translation::read(const config& cfg)
{
this->supported = cfg["supported"].to_bool(true);
this->title = cfg["title"].str();
this->description = cfg["description"].str();
}

void addon_info_translation::write(config& cfg) const
{
cfg["supported"] = this->supported;
cfg["title"] = this->title;
cfg["description"] = this->description;
}

void addon_info::read(const config& cfg)
{
this->id = cfg["name"].str();
Expand All @@ -73,7 +88,9 @@ void addon_info::read(const config& cfg)
const config::const_child_itors& locales_as_configs = cfg.child_range("translation");

for(const config& locale : locales_as_configs) {
this->locales.push_back(locale["language"].str());
if(locale["supported"].to_bool(true))
this->locales.emplace_back(locale["language"].str());
this->info_translations.emplace(locale["language"].str(), addon_info_translation(locale));
}

this->core = cfg["core"].str();
Expand All @@ -100,8 +117,10 @@ void addon_info::write(config& cfg) const
cfg["uploads"] = this->uploads;
cfg["type"] = get_addon_type_string(this->type);

for (const std::string& locale_id : this->locales) {
cfg.add_child("translation")["language"] = locale_id;
for(const std::pair<std::string, addon_info_translation> &element : this->info_translations) {
config* locale = &cfg.add_child("translation");
(*locale)["language"] = element.first;
element.second.write(*locale);
kabachuha marked this conversation as resolved.
Show resolved Hide resolved
}

cfg["core"] = this->core;
Expand Down Expand Up @@ -132,6 +151,65 @@ std::string addon_info::display_title() const
}
}

addon_info_translation addon_info_translation::invalid = {false, "", ""};

addon_info_translation addon_info::translated_info() const
{
std::string locale = get_language().localename;

if(locale != "en_US") {
std::map<std::string, addon_info_translation>::const_iterator info;

info = info_translations.find(locale);
if(info != info_translations.end()) {
return info->second;
}

info = info_translations.find(locale.substr(0, 2));
if(info != info_translations.end()) {
return info->second;
kabachuha marked this conversation as resolved.
Show resolved Hide resolved
}
}

return addon_info_translation::invalid;
}

std::string addon_info::display_title_translated() const
{
addon_info_translation info = this->translated_info();

if(info.valid()) {
return info.title;
}

return "";
}

std::string addon_info::display_title_translated_or_original() const
{
std::string title = display_title_translated();
return title.empty() ? display_title() : title;
}

std::string addon_info::description_translated() const
{
addon_info_translation info = this->translated_info();

if(info.valid() && !info.description.empty()) {
return info.description;
}

return this->description;
}

std::string addon_info::display_title_full() const
{
std::string local_title = display_title_translated();
if(local_title.empty())
return display_title();
return local_title + " (" + display_title() + ")";
}

std::string addon_info::display_icon() const
{
std::string ret = icon;
Expand Down
67 changes: 67 additions & 0 deletions src/addon/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,62 @@
#include <set>
#include <map>

struct addon_info_translation;
struct addon_info;
class config;
typedef std::map<std::string, addon_info> addons_list;

struct addon_info_translation
{
static addon_info_translation invalid;

bool supported;
std::string title;
std::string description;

addon_info_translation()
: supported(true)
, title()
, description()
{}

addon_info_translation(bool sup, std::string titl, std::string desc)
: supported(sup)
, title(titl)
, description(desc)
{
}

explicit addon_info_translation(const config& cfg)
: supported(true)
, title()
, description()
{
this->read(cfg);
}

addon_info_translation(const addon_info_translation&) = default;

addon_info_translation& operator=(const addon_info_translation& o)
{
if(this != &o) {
this->supported = o.supported;
this->title = o.title;
this->description = o.description;
}
return *this;
}

void read(const config& cfg);

void write(config& cfg) const;

bool valid()
{
return !title.empty();
}
};

struct addon_info
{
std::string id;
Expand Down Expand Up @@ -61,6 +113,8 @@ struct addon_info
// not previously published.
bool local_only;

std::map<std::string, addon_info_translation> info_translations;

addon_info()
: id(), title(), description(), icon()
, version(), author(), size(), downloads()
Expand All @@ -71,6 +125,7 @@ struct addon_info
, updated()
, created()
, local_only(false)
, info_translations()
{}

explicit addon_info(const config& cfg)
Expand All @@ -83,6 +138,7 @@ struct addon_info
, updated()
, created()
, local_only(false)
, info_translations()
{
this->read(cfg);
}
Expand All @@ -109,6 +165,7 @@ struct addon_info
this->updated = o.updated;
this->created = o.created;
this->local_only = o.local_only;
this->info_translations = o.info_translations;
}
return *this;
}
Expand Down Expand Up @@ -138,6 +195,16 @@ struct addon_info
*/
std::string display_title() const;

addon_info_translation translated_info() const;

std::string display_title_translated() const;

std::string display_title_translated_or_original() const;

std::string display_title_full() const;

std::string description_translated() const;

/** Get an icon path fixed for display (e.g. when TC is missing, or the image doesn't exist). */
std::string display_icon() const;

Expand Down
14 changes: 12 additions & 2 deletions src/campaign_server/addon_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,29 @@ std::string format_addon_feedback_url(const std::string& format, const config& p
return std::string();
}

void support_translation(config& addon, const std::string locale_id)
{
config* locale = &addon.find_child("translation", "language", locale_id);
if(!*locale) {
locale = &addon.add_child("translation");
(*locale)["language"] = locale_id;
}
(*locale)["supported"] = true;
}

void find_translations(const config& base_dir, config& addon)
{
for(const config& file : base_dir.child_range("file")) {
const std::string& fn = file["name"].str();
if(filesystem::ends_with(fn, ".po")) {
addon.add_child("translation")["language"] = filesystem::base_name(fn, true);
support_translation(addon, filesystem::base_name(fn, true));
}
}

for(const config &dir : base_dir.child_range("dir"))
{
if(dir["name"] == "LC_MESSAGES") {
addon.add_child("translation")["language"] = base_dir["name"];
support_translation(addon, base_dir["name"]);
} else {
find_translations(dir, addon);
}
Expand Down
1 change: 1 addition & 0 deletions src/campaign_server/addon_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ inline bool is_text_markup_char(char c)
*/
std::string format_addon_feedback_url(const std::string& format, const config& params);

void support_translation(config& addon, std::string locale_id);

/**
* Scans an add-on archive directory for translations.
Expand Down
19 changes: 17 additions & 2 deletions src/campaign_server/campaign_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void server::handle_request_campaign_list(const server::request& req)

for(const config& j : i.child_range("translation"))
{
if(j["language"] == lang) {
if(j["language"] == lang && j["supported"].to_bool(true)) {//for old addons
found = true;
break;
}
Expand Down Expand Up @@ -881,6 +881,22 @@ void server::handle_upload(const server::request& req)
(*campaign).add_child("feedback", url_params);
}

(*campaign).clear_children("translation");
for(const config& locale_params : upload.child_range("translation")) {
if(!locale_params["language"].empty()) {
config* locale = &(*campaign).add_child("translation");
(*locale)["language"] = locale_params["language"].str();
(*locale)["supported"] = false;

if(!locale_params["title"].empty()) {
(*locale)["title"] = locale_params["title"].str();
}
if(!locale_params["description"].empty()) {
(*locale)["description"] = locale_params["description"].str();
}
}
kabachuha marked this conversation as resolved.
Show resolved Hide resolved
}

kabachuha marked this conversation as resolved.
Show resolved Hide resolved
const std::string& filename = (*campaign)["filename"].str();
data["title"] = (*campaign)["title"];
data["name"] = "";
Expand All @@ -893,7 +909,6 @@ void server::handle_upload(const server::request& req)
data["icon"] = (*campaign)["icon"];
data["type"] = (*campaign)["type"];
data["tags"] = (*campaign)["tags"];
(*campaign).clear_children("translation");
find_translations(data, *campaign);

add_license(data);
Expand Down
21 changes: 15 additions & 6 deletions src/gui/dialogs/addon/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ namespace {
break;
}
}
for(const config& child : cfg.child_range("translation")) {
for(const auto& attribute : child.attribute_range()) {
std::string val = attribute.second.str();
if(translation::ci_search(val, filter)) {
found = true;
break;
}
}
}
if(!found) {
return false;
}
Expand Down Expand Up @@ -159,7 +168,7 @@ namespace {
str += ", ";
}

str += addon_list::colorize_addon_state_string(dep.display_title(), depstate.state);
str += addon_list::colorize_addon_state_string(dep.display_title_translated_or_original(), depstate.state);
}

return str;
Expand Down Expand Up @@ -672,14 +681,14 @@ void addon_manager::uninstall_addon(const addon_info& addon, window& window)
if(have_addon_pbl_info(addon.id) || have_addon_in_vcs_tree(addon.id)) {
show_error_message(
_("The following add-on appears to have publishing or version control information stored locally, and will not be removed:") + " " +
addon.display_title());
addon.display_title_full());
return;
}

bool success = remove_local_addon(addon.id);

if(!success) {
gui2::show_error_message(_("The following add-on could not be deleted properly:") + " " + addon.display_title());
gui2::show_error_message(_("The following add-on could not be deleted properly:") + " " + addon.display_title_full());
} else {
need_wml_cache_refresh_ = true;

Expand Down Expand Up @@ -805,7 +814,7 @@ void addon_manager::execute_default_action(const addon_info& addon, window& wind
break;
case ADDON_INSTALLED:
if(!tracking_info_[addon.id].can_publish) {
utils::string_map symbols{ { "addon", addon.display_title() } };
utils::string_map symbols{ { "addon", addon.display_title_full() } };
int res = gui2::show_message(_("Uninstall add-on"),
VGETTEXT("Do you want to uninstall '$addon|'?", symbols),
gui2::dialogs::message::ok_cancel_buttons);
Expand Down Expand Up @@ -874,8 +883,8 @@ void addon_manager::on_addon_select(window& window)

find_widget<drawing>(parent, "image", false).set_label(info->display_icon());

find_widget<styled_widget>(parent, "title", false).set_label(info->display_title());
find_widget<styled_widget>(parent, "description", false).set_label(info->description);
find_widget<styled_widget>(parent, "title", false).set_label(info->display_title_translated_or_original());
find_widget<styled_widget>(parent, "description", false).set_label(info->description_translated());
find_widget<styled_widget>(parent, "version", false).set_label(info->version.str());
find_widget<styled_widget>(parent, "author", false).set_label(info->author);
find_widget<styled_widget>(parent, "type", false).set_label(info->display_type());
Expand Down