Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 835 vs 840 for /trunk/Source Code/Utility/Localization.cpp

Diff revisions: vs.
  @@ -3,22 +3,57 @@
3 3 #include "Core/Headers/ErrorCodes.h"
4 4 #include <SimpleINI/include/SimpleIni.h>
5 5
6 + #include "simplefilewatcher/includes/FileWatcher.h"
7 +
6 8 namespace Divide {
7 9 namespace Locale {
8 10
11 + namespace {
12 + constexpr char* g_languageFileExtension = ".ini";
13 +
14 + class UpdateListener : public FW::FileWatchListener
15 + {
16 + public:
17 + UpdateListener()
18 + {
19 + }
20 +
21 + void handleFileAction(FW::WatchID watchid, const FW::String& dir, const FW::String& filename, FW::Action action)
22 + {
23 + switch (action)
24 + {
25 + case FW::Actions::Add: break;
26 + case FW::Actions::Delete: break;
27 + case FW::Actions::Modified:
28 + onLanguageFileModify(filename.c_str());
29 + break;
30 +
31 + default:
32 + DIVIDE_UNEXPECTED_CALL("Unknown file event!");
33 + }
34 + };
35 + } s_fileWatcherListener;
36 + };
37 +
9 38 static CSimpleIni g_languageFile;
10 39 /// Is everything loaded and ready for use?
11 40 static bool g_initialized = false;
12 41
42 + static std::unique_ptr<FW::FileWatcher> g_LanguageFileWatcher = nullptr;
43 +
13 44 ErrorCode init(const stringImpl& newLanguage) {
14 45 clear();
46 + if (!g_LanguageFileWatcher) {
47 + g_LanguageFileWatcher.reset(new FW::FileWatcher());
48 + g_LanguageFileWatcher->addWatch(Paths::g_LocalisationPath.c_str(), &s_fileWatcherListener);
49 + }
15 50
16 51 g_localeFile = newLanguage;
17 52 // Use SimpleIni library for cross-platform INI parsing
18 53 g_languageFile.SetUnicode();
19 54 g_languageFile.SetMultiLine(true);
20 55
21 - stringImpl file = "localisation/" + g_localeFile + ".ini";
56 + stringImpl file = Paths::g_LocalisationPath + g_localeFile + g_languageFileExtension;
22 57
23 58 if (g_languageFile.LoadFile(file.c_str()) != SI_OK) {
24 59 return ErrorCode::NO_LANGUAGE_INI;
  @@ -49,6 +84,12 @@
49 84 g_initialized = false;
50 85 }
51 86
87 + void idle() {
88 + if (g_LanguageFileWatcher) {
89 + g_LanguageFileWatcher->update();
90 + }
91 + }
92 +
52 93 /// Altough the language can be set at compile time, in-game options may support
53 94 /// language changes
54 95 void changeLanguage(const stringImpl& newLanguage) {
  @@ -75,6 +116,12 @@
75 116 return get(key, "key not found");
76 117 }
77 118
119 + void onLanguageFileModify(const char* languageFile) {
120 + // If we modify our currently active language, reinit the Locale system
121 + if (strcmp((g_localeFile + g_languageFileExtension).c_str(), languageFile) == 0) {
122 + init(g_localeFile);
123 + }
124 + }
78 125
79 126 }; // namespace Locale
80 127 }; // namespace Divide