Localization.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2008-2015 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 Urho3D
  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. LOGWARNING("Localization::GetLanguageIndex(language): language name is empty");
  44. return -1;
  45. }
  46. if (GetNumLanguages() == 0)
  47. {
  48. 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. 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. LOGWARNING("Localization::GetLanguage(index): no loaded languages");
  72. return String::EMPTY;
  73. }
  74. if (index < 0 || index >= GetNumLanguages())
  75. {
  76. 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. LOGWARNING("Localization::SetLanguage(index): no loaded languages");
  86. return;
  87. }
  88. if (index < 0 || index >= GetNumLanguages())
  89. {
  90. 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. LOGWARNING("Localization::SetLanguage(language): language name is empty");
  105. return;
  106. }
  107. if (GetNumLanguages() == 0)
  108. {
  109. LOGWARNING("Localization::SetLanguage(language): no loaded languages");
  110. return;
  111. }
  112. int index = GetLanguageIndex(language);
  113. if (index == -1)
  114. {
  115. 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. 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. 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. Vector<String> ids = source.GetChildNames();
  146. for (unsigned i = 0; i < ids.Size(); i++)
  147. {
  148. String id = ids[i];
  149. if (id.Empty())
  150. {
  151. LOGWARNING("Localization::LoadJSON(source): string ID is empty");
  152. continue;
  153. }
  154. JSONValue value = source.GetChild(id);
  155. Vector<String> langs = value.GetValueNames();
  156. for (unsigned j = 0; j < langs.Size(); j++)
  157. {
  158. String lang = langs[j];
  159. if (lang.Empty())
  160. {
  161. LOGWARNING("Localization::LoadJSON(source): language name is empty, string ID=\"" + id + "\"");
  162. continue;
  163. }
  164. String string = value.GetString(lang);
  165. if (string.Empty())
  166. {
  167. LOGWARNING("Localization::LoadJSON(source): translation is empty, string ID=\"" + id +
  168. "\", language=\"" + lang + "\"");
  169. continue;
  170. }
  171. strings_[StringHash(lang)][StringHash(id)] = string;
  172. if (!languages_.Contains(lang))
  173. languages_.Push(lang);
  174. if (languageIndex_ == -1)
  175. languageIndex_ = 0;
  176. }
  177. }
  178. }
  179. void Localization::LoadJSONFile(const String &name)
  180. {
  181. ResourceCache* cache = GetSubsystem<ResourceCache>();
  182. JSONFile* jsonFile = cache->GetResource<JSONFile>(name);
  183. if (jsonFile)
  184. LoadJSON(jsonFile->GetRoot());
  185. }
  186. }