Subversion Repository Public Repository

Divide-Framework

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

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

Diff revisions: vs.
  @@ -9,8 +9,12 @@
9 9 namespace Locale {
10 10
11 11 namespace {
12 + CSimpleIni g_languageFile;
12 13 constexpr char* g_languageFileExtension = ".ini";
13 14
15 + /// External modification monitoring system
16 + std::unique_ptr<FW::FileWatcher> s_LanguageFileWatcher = nullptr;
17 +
14 18 class UpdateListener : public FW::FileWatchListener
15 19 {
16 20 public:
  @@ -25,7 +29,7 @@
25 29 case FW::Actions::Add: break;
26 30 case FW::Actions::Delete: break;
27 31 case FW::Actions::Modified:
28 - onLanguageFileModify(filename.c_str());
32 + detail::onLanguageFileModify(filename.c_str());
29 33 break;
30 34
31 35 default:
  @@ -35,25 +39,28 @@
35 39 } s_fileWatcherListener;
36 40 };
37 41
38 - static CSimpleIni g_languageFile;
39 - /// Is everything loaded and ready for use?
40 - static bool g_initialized = false;
41 -
42 - static std::unique_ptr<FW::FileWatcher> g_LanguageFileWatcher = nullptr;
42 + namespace detail {
43 + void onLanguageFileModify(const char* languageFile) {
44 + // If we modify our currently active language, reinit the Locale system
45 + if (strcmp((s_localeFile + g_languageFileExtension).c_str(), languageFile) == 0) {
46 + changeLanguage(s_localeFile);
47 + }
48 + }
49 + };
43 50
44 51 ErrorCode init(const stringImpl& newLanguage) {
45 52 clear();
46 - if (!g_LanguageFileWatcher) {
47 - g_LanguageFileWatcher.reset(new FW::FileWatcher());
48 - g_LanguageFileWatcher->addWatch(Paths::g_LocalisationPath.c_str(), &s_fileWatcherListener);
53 + if (!s_LanguageFileWatcher) {
54 + s_LanguageFileWatcher.reset(new FW::FileWatcher());
55 + s_LanguageFileWatcher->addWatch(Paths::g_LocalisationPath.c_str(), &s_fileWatcherListener);
49 56 }
50 57
51 - g_localeFile = newLanguage;
58 + detail::s_localeFile = newLanguage;
52 59 // Use SimpleIni library for cross-platform INI parsing
53 60 g_languageFile.SetUnicode();
54 61 g_languageFile.SetMultiLine(true);
55 62
56 - stringImpl file = Paths::g_LocalisationPath + g_localeFile + g_languageFileExtension;
63 + stringImpl file = Paths::g_LocalisationPath + detail::s_localeFile + g_languageFileExtension;
57 64
58 65 if (g_languageFile.LoadFile(file.c_str()) != SI_OK) {
59 66 return ErrorCode::NO_LANGUAGE_INI;
  @@ -69,24 +76,24 @@
69 76 for (; keyValuePairIt != keyValue->end(); ++keyValuePairIt) {
70 77 stringImpl value(keyValuePairIt->second);
71 78
72 - hashAlg::emplace(g_languageTable,
79 + hashAlg::emplace(detail::s_languageTable,
73 80 _ID_RT(keyValuePairIt->first.pItem),
74 81 value);
75 82 }
76 83
77 - g_initialized = true;
84 + detail::s_initialized = true;
78 85 return ErrorCode::NO_ERR;
79 86 }
80 87
81 88 void clear() {
82 - g_languageTable.clear();
89 + detail::s_languageTable.clear();
83 90 g_languageFile.Reset();
84 - g_initialized = false;
91 + detail::s_initialized = false;
85 92 }
86 93
87 94 void idle() {
88 - if (g_LanguageFileWatcher) {
89 - g_LanguageFileWatcher->update();
95 + if (s_LanguageFileWatcher) {
96 + s_LanguageFileWatcher->update();
90 97 }
91 98 }
92 99
  @@ -95,14 +102,18 @@
95 102 void changeLanguage(const stringImpl& newLanguage) {
96 103 /// Set the new language code
97 104 init(newLanguage);
105 +
106 + for (const DELEGATE_CBK_PARAM<const char* /*new language*/>& languageChangeCbk : detail::s_languageChangeCallbacks) {
107 + languageChangeCbk(newLanguage.c_str());
108 + }
98 109 }
99 110
100 111 const char* get(U64 key, const char* defaultValue) {
101 - if (g_initialized) {
112 + if (detail::s_initialized) {
102 113 typedef hashMapImpl<U64, stringImpl>::const_iterator citer;
103 114 // When we ask for a string for the given key, we check our language cache first
104 - citer entry = g_languageTable.find(key);
105 - if (entry != std::cend(g_languageTable)) {
115 + citer entry = detail::s_languageTable.find(key);
116 + if (entry != std::cend(detail::s_languageTable)) {
106 117 // Usually, the entire language table is loaded.
107 118 return entry->second.c_str();
108 119 }
  @@ -116,11 +127,8 @@
116 127 return get(key, "key not found");
117 128 }
118 129
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 - }
130 + void addChangeLanguageCallback(const DELEGATE_CBK_PARAM<const char* /*new language*/>& cbk) {
131 + detail::s_languageChangeCallbacks.emplace_back(cbk);
124 132 }
125 133
126 134 }; // namespace Locale