Finished localization

This commit is contained in:
2022-12-16 08:05:01 -08:00
parent 5b48fb3901
commit b17592f4fd
15 changed files with 173 additions and 58 deletions

View File

@ -7,5 +7,5 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
LanguageManager.cpp
LocaleManager.cpp
)

View File

@ -1,19 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LanguageManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LanguageManager::LanguageManager(DawnGame *game) {
this->game = game;
auto lang = this->game->assetManager.get<LanguageAsset>("language_en");
while(!lang->loaded) {
lang->updateAsync();
}
std::cout << lang->getValue("test2");
}

View File

@ -1,29 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "asset/AssetManager.hpp"
namespace Dawn {
class DawnGame;
struct LocalizedString {
std::string data;
};
class LanguageManager {
private:
DawnGame *game;
LanguageAsset *asset;
public:
Event<> eventLanguageChanged;
LanguageManager(DawnGame *game);
struct LocalizedString getString(std::string key);
};
}

View File

@ -0,0 +1,70 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LocaleManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LocaleManager::LocaleManager(DawnGame *game) {
this->game = game;
this->locale.language = "en";
}
void LocaleManager::init() {
// Begin loading
if(this->currentlyLoadedAsset == nullptr && this->loadingAsset == nullptr) {
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
}
}
void LocaleManager::setLocale(struct Locale locale) {
auto oldLocale = this->getLocale();
this->locale = locale;
// Did the language change?
if(this->locale.language != oldLocale.language) {
// Yes, switch languages.
// Are we already loading something? Stop it.
if(this->loadingAsset != nullptr) {
this->loadingAsset->eventLoaded.removeListener(this, &LocaleManager::onLanguageLoaded);
this->game->assetManager.queueUnload(this->loadingAsset);
this->loadingAsset = nullptr;
}
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
}
this->eventLocaleChanged.invoke();
}
struct Locale LocaleManager::getLocale() {
return this->locale;
}
std::string LocaleManager::getString(std::string key) {
assertNotNull(this->currentlyLoadedAsset);
return this->currentlyLoadedAsset->getValue(key);
}
void LocaleManager::onLanguageLoaded() {
// Unload the previously loaded language
if(this->currentlyLoadedAsset != nullptr) {
this->game->assetManager.queueUnload(this->currentlyLoadedAsset);
}
// Swap loaded languages
this->currentlyLoadedAsset = this->loadingAsset;
this->loadingAsset = nullptr;
// Notify.
this->eventLanguageUpdated.invoke();
}

View File

@ -0,0 +1,68 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "asset/AssetManager.hpp"
namespace Dawn {
class DawnGame;
struct Locale {
std::string language;
};
class LocaleManager {
private:
DawnGame *game;
LanguageAsset *asset;
struct Locale locale;
LanguageAsset *currentlyLoadedAsset = nullptr;
LanguageAsset *loadingAsset = nullptr;
/** Listens for when the pending language loads. */
void onLanguageLoaded();
public:
Event<> eventLocaleChanged;
Event<> eventLanguageUpdated;
/**
* Initializes the Locale Manager Instance. Locale Manager is responsible
* for handling anything that will change depending on which region the
* player is in.
*
* @param game Game instance this manager belongs to.
*/
LocaleManager(DawnGame *game);
/**
* Initializes the LocaleManager and loads the default language.
*/
void init();
/**
* Change the locale and begin loading the new language.
*
* @param locale Locale to switch to.
*/
void setLocale(struct Locale locale);
/**
* Gets the current locale.
*
* @return Current locale.
*/
struct Locale getLocale();
/**
* Returns a language string from the language CSV file.
*
* @param key Key of the string to get.
* @return The translated string.
*/
std::string getString(std::string key);
};
}