Localization.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  23. #include "../Core/Context.h"
  24. #include "../Resource/JSONValue.h"
  25. namespace Atomic
  26. {
  27. /// %Localization subsystem. Stores all the strings in all languages.
  28. class ATOMIC_API Localization : public Object
  29. {
  30. ATOMIC_OBJECT(Localization, Object);
  31. public:
  32. /// Construct.
  33. Localization(Context* context);
  34. /// Destruct. Free all resources.
  35. virtual ~Localization();
  36. /// Return the number of languages.
  37. int GetNumLanguages() const { return (int)languages_.Size(); }
  38. /// Return the index number of current language. The index is determined by the order of loading.
  39. int GetLanguageIndex() const { return languageIndex_; }
  40. /// Return the index number of language. The index is determined by the order of loading.
  41. int GetLanguageIndex(const String& language);
  42. /// Return the name of current language.
  43. String GetLanguage();
  44. /// Return the name of language.
  45. String GetLanguage(int index);
  46. /// Set current language.
  47. void SetLanguage(int index);
  48. /// Set current language.
  49. void SetLanguage(const String& language);
  50. /// 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.
  51. String Get(const String& id);
  52. /// Clear all loaded strings.
  53. void Reset();
  54. /// Load strings from JSONValue.
  55. void LoadJSON(const JSONValue& source);
  56. /// Load strings from JSONFile. The file should be UTF8 without BOM.
  57. void LoadJSONFile(const String& name);
  58. private:
  59. /// Language names.
  60. Vector<String> languages_;
  61. /// Index of current language.
  62. int languageIndex_;
  63. /// Storage strings: <Language <StringId, Value> >.
  64. HashMap<StringHash, HashMap<StringHash, String> > strings_;
  65. };
  66. }