/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #pragma once #include #include #include #include #include //////////////////////////////////////////////////////////////////////////////////////////////// // Forward declarations //////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // Localization Utility classes/functions. ////////////////////////////////////////////////////////////////////////// template AZStd::vector MakeLocKeyList(Args... args) { return {args...}; } // Helper functions for Localization and data-string substitution struct LocalizationHelpers { // Returns the string version of any type convertible by std::to_string() // otherwise throws a compile error. template static AZStd::string DataToString(T t); // Need a function to specifically handle std::string, since std::to_string() doesn't handle it. static inline AZStd::string DataToString(AZStd::string str); // Need a function to specifically handle const char*, since std::to_string() doesn't handle it. static inline AZStd::string DataToString(const char* str); // Base case of the recursive function to convert a data list to strings template static void ConvertValuesToStrings(AZStd::vector& values, T t); // Recursive function to convert a data list to strings template static void ConvertValuesToStrings(AZStd::vector& values, T t, Args... args); // Check if a key string is in a list of substitution strings static inline bool IsKeyInList(const AZStd::vector& keys, const AZStd::string& target, int& index); }; ////////////////////////////////////////////////////////////////////////// // Localization manager bus interface. ////////////////////////////////////////////////////////////////////////// // Summary: // Interface to the Localization Manager. class LocalizationManagerRequests : public AZ::EBusTraits { public: virtual bool SetLanguage(const char* sLanguage) = 0; virtual const char* GetLanguage() = 0; virtual int GetLocalizationFormat() const = 0; // Get Localized subtitle file for current language // Summary: // Provides the asset path of a video subtitle file based on the input video path name. Input localVideoPath // should contain the game-specific path after the current language folder. // ex: // input: localVideoPath = "/VideoSubtitleSrt/100/101/101VT_01.bnk" // output: return "Localization/en-US/VideoSubtitleSrt/100/101/101VT_01.srt" // // NOTE: System expects that video file has the same name as the subtitle file (other than the file extension). virtual AZStd::string GetLocalizedSubtitleFilePath(const AZStd::string& localVideoPath, const AZStd::string& subtitleFileExtension) const = 0; // Get the given xml for the current language // Summary: // Provides the asset path of a localization xml file given the local path (e.g. the path starting from // your language folder). virtual AZStd::string GetLocalizedLocXMLFilePath(const AZStd::string& localXmlPath) const = 0; virtual bool LoadExcelXmlSpreadsheet(const char* sFileName, bool bReload = false) = 0; virtual void ReloadData() = 0; // Summary: // Translate a string into the currently selected language. // Description: // Processes the input string and translates all labels contained into the currently selected language. // Parameters: // sString - String to be translated. // outLocalizedString - Translated version of the string. // bEnglish - if true, translates the string into the always present English language. // Returns: // true if localization was successful, false otherwise virtual bool LocalizeString_ch(const char* sString, string& outLocalizedString, bool bEnglish = false) = 0; // Summary: // Same as LocalizeString( const char* sString, string& outLocalizedString, bool bEnglish=false ) // but at the moment this is faster. virtual bool LocalizeString_s(const string& sString, string& outLocalizedString, bool bEnglish = false) = 0; // Set up system for passing in placeholder data for localized strings // Summary: // Parse localized string and substitute data in for each key that is surrounded by curly braces // ie. {player_name} virtual void LocalizeAndSubstituteInternal(AZStd::string& locString, const AZStd::vector& keys, const AZStd::vector& values) = 0; // Summary: // Parse localized string and substitute data in for each key that is surrounded by curly braces. Number of arguments // after 'const AZStd::vector& keys' should be equal to the number of strings in 'keys'. // ex: // float distance = GetWinDistance(); // AZStd::string winState = IsPlayerFirstPlace() ? "won" : "lost"; // LocalizationManagerRequests::Broadcast(&LocalizationManagerRequests::Events::LocalizeAndSubstitute // , "@QUICKRESULTS_DISTANCEDIFFERENCE, outLocalizedString // , MakeLocKeyString("race_result", "distance_ahead") // , winState, distance); // // where "@QUICKRESULTS_DISTANCEDIFFERENCE" would be localized to "You {race_result} by {distance_ahead} meters!" and then // "{race_result}" would be replaced by 'winState" and "{distance_ahead}" would be replaced by the 'distance' argument as a string. template void LocalizeAndSubstitute(const AZStd::string& locString, AZStd::string& outLocalizedString, const AZStd::vector& keys, T t, Args... args); // Summary: // Return the localized version corresponding to a label. // Description: // A label has to start with '@' sign. // Parameters: // sLabel - Label to be translated, must start with '@' sign. // outLocalizedString - Localized version of the label. // bEnglish - if true, returns the always present English version of the label. // Returns: // True if localization was successful, false otherwise. virtual bool LocalizeLabel(const char* sLabel, string& outLocalizedString, bool bEnglish = false) = 0; // Summary: // Return number of localization entries. virtual int GetLocalizedStringCount() = 0; // Summary: // Get the english localization info structure corresponding to a key. // Parameters: // sKey - Key to be looked up. Key = Label without '@' sign. // sLocalizedString - Corresponding english language string. // Returns: // True if successful, false otherwise (key not found). virtual bool GetEnglishString(const char* sKey, string& sLocalizedString) = 0; // Summary: // Get Subtitle for Key or Label . // Parameters: // sKeyOrLabel - Key or Label to be used for subtitle lookup. Key = Label without '@' sign. // outSubtitle - Subtitle (untouched if Key/Label not found). // bForceSubtitle - If true, get subtitle (sLocalized or sEnglish) even if not specified in Data file. // Returns: // True if subtitle found (and outSubtitle filled in), false otherwise. virtual bool GetSubtitle(const char* sKeyOrLabel, string& outSubtitle, bool bForceSubtitle = false) = 0; // Description: // These methods format outString depending on sString with ordered arguments // FormatStringMessage(outString, "This is %2 and this is %1", "second", "first"); // Arguments: // outString - This is first and this is second. virtual void FormatStringMessage_List(string& outString, const string& sString, const char** sParams, int nParams) = 0; virtual void FormatStringMessage(string& outString, const string& sString, const char* param1, const char* param2 = 0, const char* param3 = 0, const char* param4 = 0) = 0; virtual void LocalizeTime(time_t t, bool bMakeLocalTime, bool bShowSeconds, string& outTimeString) = 0; virtual void LocalizeDate(time_t t, bool bMakeLocalTime, bool bShort, bool bIncludeWeekday, string& outDateString) = 0; virtual void LocalizeDuration(int seconds, string& outDurationString) = 0; virtual void LocalizeNumber(int number, string& outNumberString) = 0; virtual void LocalizeNumber_Decimal(float number, int decimals, string& outNumberString) = 0; // Summary: // Returns true if the project has localization configured for use, false otherwise. virtual bool ProjectUsesLocalization() const = 0; // }; using LocalizationManagerRequestBus = AZ::EBus; // Summary: // Simple bus that notifies listeners that the language (g_language) has changed. class LanguageChangeNotification : public AZ::EBusTraits { public: static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; virtual ~LanguageChangeNotification() = default; virtual void LanguageChanged() = 0; }; using LanguageChangeNotificationBus = AZ::EBus; // Set up system for passing in placeholder data for localized strings #include "LocalizationManagerBus.inl"