LocalizationManagerBus.inl 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // Returns the string version of any type convertible by AZStd::to_string()
  9. // otherwise throws a compile error.
  10. template <typename T>
  11. AZStd::string LocalizationHelpers::DataToString(T t)
  12. {
  13. return AZStd::to_string(t);
  14. }
  15. // Need a function to specifically handle AZStd::string, since AZStd::to_string() doesn't handle it.
  16. AZStd::string LocalizationHelpers::DataToString(AZStd::string str)
  17. {
  18. return str;
  19. }
  20. // Need a function to specifically handle const char*, since AZStd::to_string() doesn't handle it.
  21. AZStd::string LocalizationHelpers::DataToString(const char* str)
  22. {
  23. return str;
  24. }
  25. // Base case of the recursive function to convert a data list to strings
  26. template<typename T>
  27. void LocalizationHelpers::ConvertValuesToStrings(AZStd::vector<AZStd::string>& values, T t)
  28. {
  29. values.push_back(DataToString(t));
  30. }
  31. // Recursive function to convert a data list to strings
  32. template<typename T, typename... Args>
  33. void LocalizationHelpers::ConvertValuesToStrings(AZStd::vector<AZStd::string>& values, T t, Args... args)
  34. {
  35. values.push_back(DataToString(t));
  36. ConvertValuesToStrings(values, args...);
  37. }
  38. // Check if a key string is in a list of substitution strings
  39. bool LocalizationHelpers::IsKeyInList(const AZStd::vector<AZStd::string>& keys, const AZStd::string& target, int& index)
  40. {
  41. for (index = 0; index < keys.size(); ++index)
  42. {
  43. if (keys[index] == target)
  44. {
  45. return true;
  46. }
  47. }
  48. index = -1;
  49. return false;
  50. }
  51. // Summary:
  52. // Parse localized string and substitute data in for each key that is surrounded by curly braces. Number of arguments
  53. // after 'const AZStd::vector<AZStd::string>& keys' should be equal to the number of strings in 'keys'.
  54. // ex:
  55. // float distance = GetWinDistance();
  56. // AZStd::string winState = IsPlayerFirstPlace() ? "won" : "lost";
  57. // LocalizationManagerRequests::Broadcast(&LocalizationManagerRequests::Events::LocalizeAndSubstitute<float, AZStd::string>
  58. // , "@QUICKRESULTS_DISTANCEDIFFERENCE, outLocalizedString
  59. // , MakeLocKeyString("race_result", "distance_ahead")
  60. // , winState, distance);
  61. //
  62. // where "@QUICKRESULTS_DISTANCEDIFFERENCE" would be localized to "You {race_result} by {distance_ahead} meters!" and then
  63. // "{race_result}" would be replaced by 'winState" and "{distance_ahead}" would be replaced by the 'distance' argument as a string.
  64. template<typename T, typename... Args>
  65. void LocalizationManagerRequests::LocalizeAndSubstitute(const AZStd::string& locString, AZStd::string& outLocalizedString, const AZStd::vector<AZStd::string>& keys, T t, Args... args)
  66. {
  67. AZStd::vector<AZStd::string> values;
  68. LocalizationHelpers::ConvertValuesToStrings(values, t, args...);
  69. outLocalizedString = locString;
  70. LocalizationManagerRequestBus::Broadcast(&LocalizationManagerRequestBus::Events::LocalizeAndSubstituteInternal, outLocalizedString, keys, values);
  71. }