Localization.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // Copyright (c) 2008-2020 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. #pragma once
  23. #include "../Core/Context.h"
  24. #include "../Resource/JSONValue.h"
  25. namespace Urho3D
  26. {
  27. /// %Localization subsystem. Stores all the strings in all languages.
  28. class URHO3D_API Localization : public Object
  29. {
  30. URHO3D_OBJECT(Localization, Object);
  31. public:
  32. /// Construct.
  33. explicit Localization(Context* context);
  34. /// Destruct. Free all resources.
  35. ~Localization() override;
  36. /// Return the number of languages.
  37. /// @property
  38. int GetNumLanguages() const { return (int)languages_.Size(); }
  39. /// Return the index number of current language. The index is determined by the order of loading.
  40. /// @property
  41. int GetLanguageIndex() const { return languageIndex_; }
  42. /// Return the index number of language. The index is determined by the order of loading.
  43. int GetLanguageIndex(const String& language);
  44. /// Return the name of current language.
  45. /// @property
  46. String GetLanguage();
  47. /// Return the name of language.
  48. String GetLanguage(int index);
  49. /// Set current language.
  50. void SetLanguage(int index);
  51. /// Set current language.
  52. void SetLanguage(const String& language);
  53. /// Return a string in the current language. Returns String::EMPTY if id is empty. Returns id if translation is not found and logs a warning.
  54. String Get(const String& id);
  55. /// Clear all loaded strings.
  56. void Reset();
  57. /// Load strings from JSONFile. The file should be UTF8 without BOM.
  58. void LoadJSONFile(const String& name, const String& language = String::EMPTY);
  59. /// Load strings from JSONValue.
  60. void LoadMultipleLanguageJSON(const JSONValue& source);
  61. /// Load strings from JSONValue for specific language.
  62. void LoadSingleLanguageJSON(const JSONValue& source, const String& language = String::EMPTY);
  63. private:
  64. /// Language names.
  65. Vector<String> languages_;
  66. /// Index of current language.
  67. int languageIndex_;
  68. /// Storage strings: <Language <StringId, Value> >.
  69. HashMap<StringHash, HashMap<StringHash, String> > strings_;
  70. };
  71. }