| 626 | |
| 627 | |
| 628 | static void loadTranslations() { |
| 629 | INFO("Loading translations"); |
| 630 | translations.clear(); |
| 631 | std::string translationsDir = asset::system("translations"); |
| 632 | |
| 633 | for (std::string filename : system::getEntries(translationsDir)) { |
| 634 | if (system::getExtension(filename) != ".json") |
| 635 | continue; |
| 636 | std::string language = system::getStem(filename); |
| 637 | std::string path = system::join(translationsDir, filename); |
| 638 | |
| 639 | INFO("Loading translation %s from %s", language.c_str(), path.c_str()); |
| 640 | FILE* file = std::fopen(path.c_str(), "r"); |
| 641 | if (!file) { |
| 642 | WARN("Cannot open translation file %s", path.c_str()); |
| 643 | continue; |
| 644 | } |
| 645 | DEFER({std::fclose(file);}); |
| 646 | |
| 647 | json_error_t error; |
| 648 | json_t* rootJ = json_loadf(file, 0, &error); |
| 649 | if (!rootJ) { |
| 650 | WARN("Translation file %s has invalid JSON at %d:%d %s", path.c_str(), error.line, error.column, error.text); |
| 651 | continue; |
| 652 | } |
| 653 | DEFER({json_decref(rootJ);}); |
| 654 | |
| 655 | auto& translation = translations[language]; |
| 656 | |
| 657 | // Load JSON |
| 658 | const char* id; |
| 659 | json_t* strJ; |
| 660 | json_object_foreach(rootJ, id, strJ) { |
| 661 | if (json_is_string(strJ)) { |
| 662 | translation[id] = std::string(json_string_value(strJ), json_string_length(strJ)); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | |
| 669 | std::string translate(const std::string& id) { |
no test coverage detected