MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / InitializeLanguagePacks

Function InitializeLanguagePacks

src/strings.cpp:2249–2291  ·  view source on GitHub ↗

* Make a list of the available language packs. Put the data in * #_languages list. */

Source from the content-addressed store, hash-verified

2247 * #_languages list.
2248 */
2249void InitializeLanguagePacks()
2250{
2251 for (Searchpath sp : _valid_searchpaths) {
2252 FillLanguageList(FioGetDirectory(sp, LANG_DIR));
2253 }
2254 if (_languages.empty()) UserError("No available language packs (invalid versions?)");
2255
2256 /* Acquire the locale of the current system */
2257 auto str_lang = GetCurrentLocale("LC_MESSAGES");
2258 std::string_view lang = str_lang.has_value() ? std::string_view{*str_lang} : "en_GB";
2259
2260 const LanguageMetadata *chosen_language = nullptr; ///< Matching the language in the configuration file or the current locale
2261 const LanguageMetadata *language_fallback = nullptr; ///< Using pt_PT for pt_BR locale when pt_BR is not available
2262 const LanguageMetadata *en_GB_fallback = _languages.data(); ///< Fallback when no locale-matching language has been found
2263
2264 /* Find a proper language. */
2265 for (const LanguageMetadata &lng : _languages) {
2266 /* We are trying to find a default language. The priority is by
2267 * configuration file, local environment and last, if nothing found,
2268 * English. */
2269 if (_config_language_file == FS2OTTD(lng.file.filename().native())) {
2270 chosen_language = &lng;
2271 break;
2272 }
2273
2274 std::string_view iso_code = lng.isocode;
2275 if (iso_code == "en_GB") en_GB_fallback = &lng;
2276
2277 /* Only auto-pick finished translations */
2278 if (!lng.IsReasonablyFinished()) continue;
2279
2280 if (iso_code.starts_with(lang.substr(0, 5))) chosen_language = &lng;
2281 if (iso_code.starts_with(lang.substr(0, 2))) language_fallback = &lng;
2282 }
2283
2284 /* We haven't found the language in the config nor the one in the locale.
2285 * Now we set it to one of the fallback languages */
2286 if (chosen_language == nullptr) {
2287 chosen_language = (language_fallback != nullptr) ? language_fallback : en_GB_fallback;
2288 }
2289
2290 if (!ReadLanguagePack(chosen_language)) UserError("Can't read language pack '{}'", FS2OTTD(chosen_language->file.native()));
2291}
2292
2293/**
2294 * Get the ISO language code of the currently loaded language.

Callers 1

openttd_mainFunction · 0.85

Calls 9

FillLanguageListFunction · 0.85
FioGetDirectoryFunction · 0.85
ReadLanguagePackFunction · 0.85
IsReasonablyFinishedMethod · 0.80
substrMethod · 0.80
GetCurrentLocaleFunction · 0.70
FS2OTTDFunction · 0.70
emptyMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected