Silicon
A realtime platform for creating interactive media.
Localization.cpp
Go to the documentation of this file.
1// BSD 2-Clause License
2//
3// Copyright (c) 2022, Matthew McCall
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//
28// Created by Matthew McCall on 11/20/22.
29//
30
31#include <filesystem>
32#include <unordered_map>
33#include <vector>
34
35#include "spdlog/fmt/fmt.h"
36
37#include "boost/assert.hpp"
38
39#include "tinyxml2.h"
40
42#include "Silicon/Log.hpp"
43
44namespace {
45
48
49}
50
51namespace Si {
52
53void SetLocale(Locale locale)
54{
55 for (const auto &filePath : s_localizationFiles[locale]) {
56 tinyxml2::XMLDocument doc;
57 doc.LoadFile(filePath.c_str());
58 BOOST_ASSERT_MSG(!doc.Error(), doc.ErrorStr());
59
60 tinyxml2::XMLElement *plist = doc.FirstChildElement("plist");
61 BOOST_ASSERT_MSG(plist, "plist not found in localization file!");
62
63 tinyxml2::XMLElement *rootDict = plist->FirstChildElement("dict");
64 BOOST_ASSERT_MSG(rootDict, "Dictionary not found in localization file!");
65
66 tinyxml2::XMLElement *keyElement = rootDict->FirstChildElement("key");
67
68 while (keyElement) {
69 tinyxml2::XMLText *keyTextNode = keyElement->FirstChild()->ToText();
70 tinyxml2::XMLElement *stringElement = keyElement->NextSiblingElement("string");
71 BOOST_ASSERT_MSG(stringElement, fmt::format("String for key '{}' not found in localization file!", keyTextNode->Value()).c_str());
72
73 tinyxml2::XMLText *stringTextNode = stringElement->FirstChild()->ToText();
74 s_localeTable[keyTextNode->Value()] = stringTextNode->Value();
75
76 keyElement = stringElement->NextSiblingElement("key");
77 }
78 }
79}
80
82{
83 if (s_localeTable.find(key) == s_localeTable.end()) {
84 Engine::Warn("Localization key '{}' not found!", key);
85 return key;
86 }
87
88 return s_localeTable.at(key);
89}
90
91bool AddLocalizationFile(Locale locale, const std::string &filename)
92{
93 std::filesystem::path filePath {filename};
94
95 if (!std::filesystem::exists(filePath)) {
96 Engine::Error("Localization file '{}' does not exist!", filename);
97 return false;
98 }
99
100 s_localizationFiles[locale].push_back(filename);
101
102 return true;
103}
104
105}
T at(T... args)
T exists(T... args)
void Error(Args &&... args)
Definition: Log.hpp:103
void Warn(Args &&... args)
Definition: Log.hpp:97
Definition: Allocator.hpp:36
std::string GetLocalized(const std::string &key)
void SetLocale(Locale locale)
bool AddLocalizationFile(Locale locale, const std::string &filename)