LocalizationManagerBus.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/std/string/string.h>
  12. #include <AzCore/std/string/conversions.h>
  13. ////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Forward declarations
  15. ////////////////////////////////////////////////////////////////////////////////////////////////
  16. //////////////////////////////////////////////////////////////////////////
  17. // Localization Utility classes/functions.
  18. //////////////////////////////////////////////////////////////////////////
  19. template<typename... Args>
  20. AZStd::vector<AZStd::string> MakeLocKeyList(Args... args)
  21. {
  22. return {args...};
  23. }
  24. // Helper functions for Localization and data-string substitution
  25. struct LocalizationHelpers
  26. {
  27. // Returns the string version of any type convertible by std::to_string()
  28. // otherwise throws a compile error.
  29. template <typename T>
  30. static AZStd::string DataToString(T t);
  31. // Need a function to specifically handle std::string, since std::to_string() doesn't handle it.
  32. static inline AZStd::string DataToString(AZStd::string str);
  33. // Need a function to specifically handle const char*, since std::to_string() doesn't handle it.
  34. static inline AZStd::string DataToString(const char* str);
  35. // Base case of the recursive function to convert a data list to strings
  36. template<typename T>
  37. static void ConvertValuesToStrings(AZStd::vector<AZStd::string>& values, T t);
  38. // Recursive function to convert a data list to strings
  39. template<typename T, typename... Args>
  40. static void ConvertValuesToStrings(AZStd::vector<AZStd::string>& values, T t, Args... args);
  41. // Check if a key string is in a list of substitution strings
  42. static inline bool IsKeyInList(const AZStd::vector<AZStd::string>& keys, const AZStd::string& target, int& index);
  43. };
  44. //////////////////////////////////////////////////////////////////////////
  45. // Localization manager bus interface.
  46. //////////////////////////////////////////////////////////////////////////
  47. // Summary:
  48. // Interface to the Localization Manager.
  49. class LocalizationManagerRequests
  50. : public AZ::EBusTraits
  51. {
  52. public:
  53. virtual bool SetLanguage(const char* sLanguage) = 0;
  54. virtual const char* GetLanguage() = 0;
  55. virtual int GetLocalizationFormat() const = 0;
  56. // Get Localized subtitle file for current language
  57. // Summary:
  58. // Provides the asset path of a video subtitle file based on the input video path name. Input localVideoPath
  59. // should contain the game-specific path after the current language folder.
  60. // ex:
  61. // input: localVideoPath = "/VideoSubtitleSrt/100/101/101VT_01.bnk"
  62. // output: return "Localization/en-US/VideoSubtitleSrt/100/101/101VT_01.srt"
  63. //
  64. // NOTE: System expects that video file has the same name as the subtitle file (other than the file extension).
  65. virtual AZStd::string GetLocalizedSubtitleFilePath(const AZStd::string& localVideoPath, const AZStd::string& subtitleFileExtension) const = 0;
  66. // Get the given xml for the current language
  67. // Summary:
  68. // Provides the asset path of a localization xml file given the local path (e.g. the path starting from
  69. // your language folder).
  70. virtual AZStd::string GetLocalizedLocXMLFilePath(const AZStd::string& localXmlPath) const = 0;
  71. virtual bool LoadExcelXmlSpreadsheet(const char* sFileName, bool bReload = false) = 0;
  72. virtual void ReloadData() = 0;
  73. // Summary:
  74. // Translate a string into the currently selected language.
  75. // Description:
  76. // Processes the input string and translates all labels contained into the currently selected language.
  77. // Parameters:
  78. // sString - String to be translated.
  79. // outLocalizedString - Translated version of the string.
  80. // bEnglish - if true, translates the string into the always present English language.
  81. // Returns:
  82. // true if localization was successful, false otherwise
  83. virtual bool LocalizeString_ch(const char* sString, AZStd::string& outLocalizedString, bool bEnglish = false) = 0;
  84. // Summary:
  85. // Same as LocalizeString( const char* sString, AZStd::string& outLocalizedString, bool bEnglish=false )
  86. // but at the moment this is faster.
  87. virtual bool LocalizeString_s(const AZStd::string& sString, AZStd::string& outLocalizedString, bool bEnglish = false) = 0;
  88. // Set up system for passing in placeholder data for localized strings
  89. // Summary:
  90. // Parse localized string and substitute data in for each key that is surrounded by curly braces
  91. // ie. {player_name}
  92. virtual void LocalizeAndSubstituteInternal(AZStd::string& locString, const AZStd::vector<AZStd::string>& keys, const AZStd::vector<AZStd::string>& values) = 0;
  93. // Summary:
  94. // Parse localized string and substitute data in for each key that is surrounded by curly braces. Number of arguments
  95. // after 'const AZStd::vector<AZStd::string>& keys' should be equal to the number of strings in 'keys'.
  96. // ex:
  97. // float distance = GetWinDistance();
  98. // AZStd::string winState = IsPlayerFirstPlace() ? "won" : "lost";
  99. // LocalizationManagerRequests::Broadcast(&LocalizationManagerRequests::Events::LocalizeAndSubstitute<float, AZStd::string>
  100. // , "@QUICKRESULTS_DISTANCEDIFFERENCE, outLocalizedString
  101. // , MakeLocKeyString("race_result", "distance_ahead")
  102. // , winState, distance);
  103. //
  104. // where "@QUICKRESULTS_DISTANCEDIFFERENCE" would be localized to "You {race_result} by {distance_ahead} meters!" and then
  105. // "{race_result}" would be replaced by 'winState" and "{distance_ahead}" would be replaced by the 'distance' argument as a string.
  106. template<typename T, typename... Args>
  107. void LocalizeAndSubstitute(const AZStd::string& locString, AZStd::string& outLocalizedString, const AZStd::vector<AZStd::string>& keys, T t, Args... args);
  108. // Summary:
  109. // Return the localized version corresponding to a label.
  110. // Description:
  111. // A label has to start with '@' sign.
  112. // Parameters:
  113. // sLabel - Label to be translated, must start with '@' sign.
  114. // outLocalizedString - Localized version of the label.
  115. // bEnglish - if true, returns the always present English version of the label.
  116. // Returns:
  117. // True if localization was successful, false otherwise.
  118. virtual bool LocalizeLabel(const char* sLabel, AZStd::string& outLocalizedString, bool bEnglish = false) = 0;
  119. // Summary:
  120. // Return number of localization entries.
  121. virtual int GetLocalizedStringCount() = 0;
  122. // Summary:
  123. // Get the english localization info structure corresponding to a key.
  124. // Parameters:
  125. // sKey - Key to be looked up. Key = Label without '@' sign.
  126. // sLocalizedString - Corresponding english language string.
  127. // Returns:
  128. // True if successful, false otherwise (key not found).
  129. virtual bool GetEnglishString(const char* sKey, AZStd::string& sLocalizedString) = 0;
  130. // Summary:
  131. // Get Subtitle for Key or Label .
  132. // Parameters:
  133. // sKeyOrLabel - Key or Label to be used for subtitle lookup. Key = Label without '@' sign.
  134. // outSubtitle - Subtitle (untouched if Key/Label not found).
  135. // bForceSubtitle - If true, get subtitle (sLocalized or sEnglish) even if not specified in Data file.
  136. // Returns:
  137. // True if subtitle found (and outSubtitle filled in), false otherwise.
  138. virtual bool GetSubtitle(const char* sKeyOrLabel, AZStd::string& outSubtitle, bool bForceSubtitle = false) = 0;
  139. // Description:
  140. // These methods format outString depending on sString with ordered arguments
  141. // FormatStringMessage(outString, "This is %2 and this is %1", "second", "first");
  142. // Arguments:
  143. // outString - This is first and this is second.
  144. virtual void FormatStringMessage_List(AZStd::string& outString, const AZStd::string& sString, const char** sParams, int nParams) = 0;
  145. virtual void FormatStringMessage(AZStd::string& outString, const AZStd::string& sString, const char* param1, const char* param2 = 0, const char* param3 = 0, const char* param4 = 0) = 0;
  146. virtual void LocalizeTime(time_t t, bool bMakeLocalTime, bool bShowSeconds, AZStd::string& outTimeString) = 0;
  147. virtual void LocalizeDate(time_t t, bool bMakeLocalTime, bool bShort, bool bIncludeWeekday, AZStd::string& outDateString) = 0;
  148. virtual void LocalizeDuration(int seconds, AZStd::string& outDurationString) = 0;
  149. virtual void LocalizeNumber(int number, AZStd::string& outNumberString) = 0;
  150. virtual void LocalizeNumber_Decimal(float number, int decimals, AZStd::string& outNumberString) = 0;
  151. // Summary:
  152. // Returns true if the project has localization configured for use, false otherwise.
  153. virtual bool ProjectUsesLocalization() const = 0;
  154. // </interfuscator:shuffle>
  155. };
  156. using LocalizationManagerRequestBus = AZ::EBus<LocalizationManagerRequests>;
  157. // Summary:
  158. // Simple bus that notifies listeners that the language (g_language) has changed.
  159. class LanguageChangeNotification
  160. : public AZ::EBusTraits
  161. {
  162. public:
  163. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
  164. virtual ~LanguageChangeNotification() = default;
  165. virtual void LanguageChanged() = 0;
  166. };
  167. using LanguageChangeNotificationBus = AZ::EBus<LanguageChangeNotification>;
  168. // Set up system for passing in placeholder data for localized strings
  169. #include "LocalizationManagerBus.inl"