MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / enumerateLanguages

Function enumerateLanguages

src/OpenLoco/src/Localisation/Languages.cpp:15–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13 static std::vector<LanguageDescriptor> _languageDescriptors;
14
15 void enumerateLanguages()
16 {
17 // (Re-)initialise the languages table.
18 _languageDescriptors.clear();
19 LanguageDescriptor undefinedLanguage = { "", "", "", LocoLanguageId::english_uk };
20 _languageDescriptors.emplace_back(undefinedLanguage);
21
22 // Search the languages dir for YAML language files.
23 fs::path languageDir = Environment::getPath(Environment::PathId::languageFiles);
24 for (auto& entry : fs::directory_iterator(languageDir))
25 {
26 const auto filePath = entry.path();
27
28 const auto fileExt = filePath.extension();
29 if (fileExt != ".yml")
30 {
31 continue;
32 }
33
34 std::ifstream stream(filePath);
35 if (!stream.is_open())
36 {
37 continue;
38 }
39
40 // Read only the header of the file to speed up the indexing process.
41 std::string headerYaml;
42 for (std::string line; line != "strings:" && !stream.eof(); std::getline(stream, line))
43 {
44 headerYaml += line + '\n';
45 }
46
47 YAML::Node node = YAML::Load(headerYaml);
48 if (!node.IsMap())
49 {
50 continue;
51 }
52
53 YAML::Node header = node["header"];
54
55 // Create a language descriptor for this language file.
56 LanguageDescriptor language;
57 language.locale = header["locale"].as<std::string>();
58 language.englishName = header["english_name"].as<std::string>();
59 language.nativeName = convertUnicodeToLoco(header["native_name"].as<std::string>());
60 language.locoOriginalId = (LocoLanguageId)header["loco_original_id"].as<size_t>();
61
62 // Store it in the languages map.
63 _languageDescriptors.emplace_back(language);
64 }
65
66 // Sort by native name.
67 std::ranges::sort(_languageDescriptors, {}, &LanguageDescriptor::nativeName);
68 }
69
70 std::span<const LanguageDescriptor> getLanguageDescriptors()
71 {

Callers 1

initialiseFunction · 0.85

Calls 3

getPathFunction · 0.85
convertUnicodeToLocoFunction · 0.85
clearMethod · 0.45

Tested by

no test coverage detected