Localization.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Resource/Localization.h"
  24. #include "../Resource/ResourceCache.h"
  25. #include "../Resource/JSONFile.h"
  26. #include "../Resource/ResourceEvents.h"
  27. #include "../IO/Log.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. Localization::Localization(Context* context) :
  32. Object(context),
  33. languageIndex_(-1)
  34. {
  35. }
  36. Localization::~Localization()
  37. {
  38. }
  39. int Localization::GetLanguageIndex(const String& language)
  40. {
  41. if (language.Empty())
  42. {
  43. ATOMIC_LOGWARNING("Localization::GetLanguageIndex(language): language name is empty");
  44. return -1;
  45. }
  46. if (GetNumLanguages() == 0)
  47. {
  48. ATOMIC_LOGWARNING("Localization::GetLanguageIndex(language): no loaded languages");
  49. return -1;
  50. }
  51. for (int i = 0; i < GetNumLanguages(); i++)
  52. {
  53. if (languages_[i] == language)
  54. return i;
  55. }
  56. return -1;
  57. }
  58. String Localization::GetLanguage()
  59. {
  60. if (languageIndex_ == -1)
  61. {
  62. ATOMIC_LOGWARNING("Localization::GetLanguage(): no loaded languages");
  63. return String::EMPTY;
  64. }
  65. return languages_[languageIndex_];
  66. }
  67. String Localization::GetLanguage(int index)
  68. {
  69. if (GetNumLanguages() == 0)
  70. {
  71. ATOMIC_LOGWARNING("Localization::GetLanguage(index): no loaded languages");
  72. return String::EMPTY;
  73. }
  74. if (index < 0 || index >= GetNumLanguages())
  75. {
  76. ATOMIC_LOGWARNING("Localization::GetLanguage(index): index out of range");
  77. return String::EMPTY;
  78. }
  79. return languages_[index];
  80. }
  81. void Localization::SetLanguage(int index)
  82. {
  83. if (GetNumLanguages() == 0)
  84. {
  85. ATOMIC_LOGWARNING("Localization::SetLanguage(index): no loaded languages");
  86. return;
  87. }
  88. if (index < 0 || index >= GetNumLanguages())
  89. {
  90. ATOMIC_LOGWARNING("Localization::SetLanguage(index): index out of range");
  91. return;
  92. }
  93. if (index != languageIndex_)
  94. {
  95. languageIndex_ = index;
  96. VariantMap& eventData = GetEventDataMap();
  97. SendEvent(E_CHANGELANGUAGE, eventData);
  98. }
  99. }
  100. void Localization::SetLanguage(const String& language)
  101. {
  102. if (language.Empty())
  103. {
  104. ATOMIC_LOGWARNING("Localization::SetLanguage(language): language name is empty");
  105. return;
  106. }
  107. if (GetNumLanguages() == 0)
  108. {
  109. ATOMIC_LOGWARNING("Localization::SetLanguage(language): no loaded languages");
  110. return;
  111. }
  112. int index = GetLanguageIndex(language);
  113. if (index == -1)
  114. {
  115. ATOMIC_LOGWARNING("Localization::SetLanguage(language): language not found");
  116. return;
  117. }
  118. SetLanguage(index);
  119. }
  120. String Localization::Get(const String& id)
  121. {
  122. if (id.Empty())
  123. return String::EMPTY;
  124. if (GetNumLanguages() == 0)
  125. {
  126. ATOMIC_LOGWARNING("Localization::Get(id): no loaded languages");
  127. return id;
  128. }
  129. String result = strings_[StringHash(GetLanguage())][StringHash(id)];
  130. if (result.Empty())
  131. {
  132. ATOMIC_LOGWARNING("Localization::Get(\"" + id + "\") not found translation, language=\"" + GetLanguage() + "\"");
  133. return id;
  134. }
  135. return result;
  136. }
  137. void Localization::Reset()
  138. {
  139. languages_.Clear();
  140. languageIndex_ = -1;
  141. strings_.Clear();
  142. }
  143. void Localization::LoadJSON(const JSONValue& source)
  144. {
  145. for (JSONObject::ConstIterator i = source.Begin(); i != source.End(); ++i)
  146. {
  147. String id = i->first_;
  148. if (id.Empty())
  149. {
  150. ATOMIC_LOGWARNING("Localization::LoadJSON(source): string ID is empty");
  151. continue;
  152. }
  153. const JSONValue& langs = i->second_;
  154. for (JSONObject::ConstIterator j = langs.Begin(); j != langs.End(); ++j)
  155. {
  156. const String& lang = j->first_;
  157. if (lang.Empty())
  158. {
  159. ATOMIC_LOGWARNING("Localization::LoadJSON(source): language name is empty, string ID=\"" + id + "\"");
  160. continue;
  161. }
  162. const String& string = j->second_.GetString();
  163. if (string.Empty())
  164. {
  165. ATOMIC_LOGWARNING(
  166. "Localization::LoadJSON(source): translation is empty, string ID=\"" + id + "\", language=\"" + lang + "\"");
  167. continue;
  168. }
  169. if (strings_[StringHash(lang)][StringHash(id)] != String::EMPTY)
  170. {
  171. ATOMIC_LOGWARNING(
  172. "Localization::LoadJSON(source): override translation, string ID=\"" + id + "\", language=\"" + lang + "\"");
  173. }
  174. strings_[StringHash(lang)][StringHash(id)] = string;
  175. if (!languages_.Contains(lang))
  176. languages_.Push(lang);
  177. if (languageIndex_ == -1)
  178. languageIndex_ = 0;
  179. }
  180. }
  181. }
  182. void Localization::LoadJSONFile(const String& name)
  183. {
  184. ResourceCache* cache = GetSubsystem<ResourceCache>();
  185. JSONFile* jsonFile = cache->GetResource<JSONFile>(name);
  186. if (jsonFile)
  187. LoadJSON(jsonFile->GetRoot());
  188. }
  189. }