Allow localizing the application name with project translations

This commit is contained in:
Haoyu Qiu 2025-11-05 12:36:16 +08:00
parent 6fd949a6dc
commit b8a8f8b35a
12 changed files with 100 additions and 57 deletions

View file

@ -32,7 +32,7 @@
#include "core/io/json.h"
#include "core/io/plist.h"
#include "core/string/translation.h"
#include "core/string/translation_server.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
@ -1915,42 +1915,51 @@ Error EditorExportPlatformAppleEmbedded::_export_project_helper(const Ref<Editor
return ERR_FILE_NOT_FOUND;
}
Dictionary appnames = get_project_setting(p_preset, "application/config/name_localized");
Dictionary camera_usage_descriptions = p_preset->get("privacy/camera_usage_description_localized");
Dictionary microphone_usage_descriptions = p_preset->get("privacy/microphone_usage_description_localized");
Dictionary photolibrary_usage_descriptions = p_preset->get("privacy/photolibrary_usage_description_localized");
Vector<String> translations = get_project_setting(p_preset, "internationalization/locale/translations");
if (translations.size() > 0) {
const String project_name = get_project_setting(p_preset, "application/config/name");
const Dictionary appnames = get_project_setting(p_preset, "application/config/name_localized");
const StringName domain_name = "godot.project_name_localization";
Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(domain_name);
TranslationServer::get_singleton()->load_project_translations(domain);
const Vector<String> locales = domain->get_loaded_locales();
if (!locales.is_empty()) {
{
String fname = binary_dir + "/en.lproj";
tmp_app_path->make_dir_recursive(fname);
Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
f->store_line("/* Localized versions of Info.plist keys */");
f->store_line("");
f->store_line("CFBundleDisplayName = \"" + get_project_setting(p_preset, "application/config/name").operator String() + "\";");
f->store_line("CFBundleDisplayName = \"" + project_name + "\";");
f->store_line("NSCameraUsageDescription = \"" + p_preset->get("privacy/camera_usage_description").operator String() + "\";");
f->store_line("NSMicrophoneUsageDescription = \"" + p_preset->get("privacy/microphone_usage_description").operator String() + "\";");
f->store_line("NSPhotoLibraryUsageDescription = \"" + p_preset->get("privacy/photolibrary_usage_description").operator String() + "\";");
}
HashSet<String> languages;
for (const String &E : translations) {
Ref<Translation> tr = ResourceLoader::load(E);
if (tr.is_valid() && tr->get_locale() != "en") {
languages.insert(tr->get_locale());
for (const String &lang : locales) {
if (lang == "en") {
continue;
}
}
for (const String &lang : languages) {
String fname = binary_dir + "/" + lang + ".lproj";
tmp_app_path->make_dir_recursive(fname);
Ref<FileAccess> f = FileAccess::open(fname + "/InfoPlist.strings", FileAccess::WRITE);
f->store_line("/* Localized versions of Info.plist keys */");
f->store_line("");
if (appnames.has(lang)) {
if (appnames.is_empty()) {
domain->set_locale_override(lang);
const String &name = domain->translate(project_name, String());
if (name != project_name) {
f->store_line("CFBundleDisplayName = \"" + name + "\";");
}
} else if (appnames.has(lang)) {
f->store_line("CFBundleDisplayName = \"" + appnames[lang].operator String() + "\";");
}
if (camera_usage_descriptions.has(lang)) {
f->store_line("NSCameraUsageDescription = \"" + camera_usage_descriptions[lang].operator String() + "\";");
}