Localization.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Context.h"
  5. #include "../Resource/JSONValue.h"
  6. namespace Urho3D
  7. {
  8. /// %Localization subsystem. Stores all the strings in all languages.
  9. class URHO3D_API Localization : public Object
  10. {
  11. URHO3D_OBJECT(Localization, Object);
  12. public:
  13. /// Construct.
  14. explicit Localization(Context* context);
  15. /// Destruct. Free all resources.
  16. ~Localization() override;
  17. /// Return the number of languages.
  18. /// @property
  19. int GetNumLanguages() const { return (int)languages_.Size(); }
  20. /// Return the index number of current language. The index is determined by the order of loading.
  21. /// @property
  22. int GetLanguageIndex() const { return languageIndex_; }
  23. /// Return the index number of language. The index is determined by the order of loading.
  24. int GetLanguageIndex(const String& language);
  25. /// Return the name of current language.
  26. /// @property
  27. String GetLanguage();
  28. /// Return the name of language.
  29. String GetLanguage(int index);
  30. /// Set current language.
  31. void SetLanguage(int index);
  32. /// Set current language.
  33. void SetLanguage(const String& language);
  34. /// 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.
  35. String Get(const String& id);
  36. /// Clear all loaded strings.
  37. void Reset();
  38. /// Load strings from JSONFile. The file should be UTF8 without BOM.
  39. void LoadJSONFile(const String& name, const String& language = String::EMPTY);
  40. /// Load strings from JSONValue.
  41. void LoadMultipleLanguageJSON(const JSONValue& source);
  42. /// Load strings from JSONValue for specific language.
  43. void LoadSingleLanguageJSON(const JSONValue& source, const String& language = String::EMPTY);
  44. private:
  45. /// Language names.
  46. Vector<String> languages_;
  47. /// Index of current language.
  48. int languageIndex_;
  49. /// Storage strings: <Language <StringId, Value>>.
  50. HashMap<StringHash, HashMap<StringHash, String>> strings_;
  51. };
  52. }