Localization.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. assert(GetNumLanguages() > 0 && !language.Empty());
  42. for (int i = 0; i < GetNumLanguages(); i++)
  43. {
  44. if (languages_[i] == language)
  45. return i;
  46. }
  47. return -1;
  48. }
  49. String Localization::GetLanguage()
  50. {
  51. assert(languageIndex_ != -1);
  52. return languages_[languageIndex_];
  53. }
  54. String Localization::GetLanguage(int index)
  55. {
  56. assert(index >= 0 && index < GetNumLanguages());
  57. return languages_[index];
  58. }
  59. void Localization::SetLanguage(int index)
  60. {
  61. assert(index >= 0 && index < GetNumLanguages());
  62. if (index != languageIndex_)
  63. {
  64. languageIndex_ = index;
  65. VariantMap& eventData = GetEventDataMap();
  66. SendEvent(E_CHANGELANGUAGE, eventData);
  67. }
  68. }
  69. void Localization::SetLanguage(const String &language)
  70. {
  71. SetLanguage(GetLanguageIndex(language));
  72. }
  73. String Localization::Get(const String &id)
  74. {
  75. if (id.Empty())
  76. return String::EMPTY;
  77. String result = strings_[StringHash(GetLanguage())][StringHash(id)];
  78. if (result.Empty())
  79. {
  80. LOGWARNING("Localization::Get(\"" + id + "\") not found translation, language=\"" + GetLanguage() + "\"");
  81. return id;
  82. }
  83. return result;
  84. }
  85. void Localization::Reset()
  86. {
  87. languages_.Clear();
  88. languageIndex_ = -1;
  89. strings_.Clear();
  90. }
  91. void Localization::LoadJSON(const JSONValue &source)
  92. {
  93. Vector<String> ids = source.GetChildNames();
  94. for (unsigned i = 0; i < ids.Size(); i++)
  95. {
  96. String id = ids[i];
  97. JSONValue value = source.GetChild(id);
  98. Vector<String> langs = value.GetValueNames();
  99. for (unsigned j = 0; j < langs.Size(); j++)
  100. {
  101. String lang = langs[j];
  102. String string = value.GetString(lang);
  103. strings_[StringHash(lang)][StringHash(id)] = string;
  104. if (!languages_.Contains(lang))
  105. languages_.Push(lang);
  106. if (languageIndex_ == -1)
  107. languageIndex_ = 0;
  108. }
  109. }
  110. }
  111. void Localization::LoadJSONFile(const String &name)
  112. {
  113. ResourceCache* cache = GetSubsystem<ResourceCache>();
  114. JSONFile* jsonFile = cache->GetResource<JSONFile>(name);
  115. if (jsonFile)
  116. LoadJSON(jsonFile->GetRoot());
  117. }
  118. }