瀏覽代碼

ResourceLoader: Add language code matching for localized resources

Near matching was not implemented like in TranslationServer, so a
resource remapped for 'ru' (but not 'ru_RU') would not be used as
fallback if the system locale was 'ru_RU'.

Fixes #34058.
Rémi Verschelde 5 年之前
父節點
當前提交
95242b7faf
共有 2 個文件被更改,包括 41 次插入15 次删除
  1. 38 15
      core/io/resource_loader.cpp
  2. 3 0
      core/translation.cpp

+ 38 - 15
core/io/resource_loader.cpp

@@ -734,26 +734,49 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem
 
 	String new_path = p_path;
 
-	if (translation_remaps.has(new_path)) {
+	if (translation_remaps.has(p_path)) {
+		// translation_remaps has the following format:
+		//   { "res://path.png": PoolStringArray( "res://path-ru.png:ru", "res://path-de.png:de" ) }
+
+		// To find the path of the remapped resource, we extract the locale name after
+		// the last ':' to match the project locale.
+		// We also fall back in case of regional locales as done in TranslationServer::translate
+		// (e.g. 'ru_RU' -> 'ru' if the former has no specific mapping).
 
-		Vector<String> &v = *translation_remaps.getptr(new_path);
 		String locale = TranslationServer::get_singleton()->get_locale();
-		if (r_translation_remapped) {
-			*r_translation_remapped = true;
-		}
-		for (int i = 0; i < v.size(); i++) {
+		ERR_FAIL_COND_V_MSG(locale.length() < 2, p_path, "Could not remap path '" + p_path + "' for translation as configured locale '" + locale + "' is invalid.");
+		String lang = TranslationServer::get_language_code(locale);
 
-			int split = v[i].find_last(":");
-			if (split == -1)
-				continue;
-			String l = v[i].right(split + 1).strip_edges();
-			if (l == String())
+		Vector<String> &res_remaps = *translation_remaps.getptr(new_path);
+		bool near_match = false;
+
+		for (int i = 0; i < res_remaps.size(); i++) {
+			int split = res_remaps[i].find_last(":");
+			if (split == -1) {
 				continue;
+			}
 
-			if (l.begins_with(locale)) {
-				new_path = v[i].left(split);
+			String l = res_remaps[i].right(split + 1).strip_edges();
+			if (l == locale) { // Exact match.
+				new_path = res_remaps[i].left(split);
 				break;
+			} else if (near_match) {
+				continue; // Already found near match, keep going for potential exact match.
 			}
+
+			// No exact match (e.g. locale 'ru_RU' but remap is 'ru'), let's look further
+			// for a near match (same language code, i.e. first 2 or 3 letters before
+			// regional code, if included).
+			if (TranslationServer::get_language_code(l) == lang) {
+				// Language code matches, that's a near match. Keep looking for exact match.
+				near_match = true;
+				new_path = res_remaps[i].left(split);
+				continue;
+			}
+		}
+
+		if (r_translation_remapped) {
+			*r_translation_remapped = true;
 		}
 	}
 
@@ -761,8 +784,8 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem
 		new_path = path_remaps[new_path];
 	}
 
-	if (new_path == p_path) { //did not remap
-		//try file remap
+	if (new_path == p_path) { // Did not remap.
+		// Try file remap.
 		Error err;
 		FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err);
 

+ 3 - 0
core/translation.cpp

@@ -1065,6 +1065,9 @@ StringName TranslationServer::translate(const StringName &p_message) const {
 	// form. If not found, we fall back to a near match (another locale with
 	// same language code).
 
+	// Note: ResourceLoader::_path_remap reproduces this locale near matching
+	// logic, so be sure to propagate changes there when changing things here.
+
 	StringName res;
 	String lang = get_language_code(locale);
 	bool near_match = false;