TranslationHelper.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "TranslationHelper.h"
  9. #include <AzCore/IO/FileIO.h>
  10. #include <AzCore/Settings/SettingsRegistry.h>
  11. #include <AzFramework/Gem/GemInfo.h>
  12. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  13. namespace ScriptCanvasEditor::TranslationHelper
  14. {
  15. AZStd::string SanitizeText(const AZStd::string& text)
  16. {
  17. AZStd::string result = text;
  18. AZ::StringFunc::Replace(result, "+", "");
  19. AZ::StringFunc::Replace(result, "-", "");
  20. AZ::StringFunc::Replace(result, "*", "");
  21. AZ::StringFunc::Replace(result, "/", "");
  22. AZ::StringFunc::Replace(result, "(", "");
  23. AZ::StringFunc::Replace(result, ")", "");
  24. AZ::StringFunc::Replace(result, "{", "");
  25. AZ::StringFunc::Replace(result, "}", "");
  26. AZ::StringFunc::Replace(result, ":", "");
  27. AZ::StringFunc::Replace(result, "<", "");
  28. AZ::StringFunc::Replace(result, ">", "");
  29. AZ::StringFunc::Replace(result, ",", "");
  30. AZ::StringFunc::Replace(result, ".", "");
  31. AZ::StringFunc::Replace(result, "=", "");
  32. AZ::StringFunc::Replace(result, "!", "");
  33. AZ::StringFunc::Strip(result, " ");
  34. return result;
  35. }
  36. AZStd::string SanitizeCustomNodeFileName(const AZStd::string& nodeName, const AZ::Uuid& nodeUuid)
  37. {
  38. AZStd::string sanitizedNodeName = SanitizeText(nodeName);
  39. AZ::Uuid::FixedString nodeUuidName = nodeUuid.ToFixedString(false);
  40. AZStd::string result = AZStd::string::format("%s_%s", sanitizedNodeName.c_str(), nodeUuidName.c_str());
  41. AZ::StringFunc::Path::Normalize(result);
  42. return result;
  43. }
  44. AZStd::string GetSafeTypeName(ScriptCanvas::Data::Type dataType)
  45. {
  46. if (!dataType.IsValid())
  47. {
  48. return "";
  49. }
  50. AZStd::string azType = dataType.GetAZType().ToString<AZStd::string>();
  51. GraphCanvas::TranslationKey key;
  52. key << "BehaviorType" << azType << "details";
  53. GraphCanvas::TranslationRequests::Details details;
  54. details.m_name = ScriptCanvas::Data::GetName(dataType);
  55. GraphCanvas::TranslationRequestBus::BroadcastResult(details, &GraphCanvas::TranslationRequests::GetDetails, key, details);
  56. return details.m_name;
  57. }
  58. AZ::IO::Path GetGemAssetPath(const AZStd::string& gemName)
  59. {
  60. if (auto settingsRegistry = AZ::Interface<AZ::SettingsRegistryInterface>::Get(); settingsRegistry != nullptr)
  61. {
  62. AZ::IO::Path gemSourceAssetDirectories;
  63. AZStd::vector<AzFramework::GemInfo> gemInfos;
  64. if (AzFramework::GetGemsInfo(gemInfos, *settingsRegistry))
  65. {
  66. auto FindGemByName = [gemName](const AzFramework::GemInfo& gemInfo)
  67. {
  68. return gemInfo.m_gemName == gemName;
  69. };
  70. // Gather unique list of Gem Paths from the Settings Registry
  71. auto foundIt = AZStd::find_if(gemInfos.begin(), gemInfos.end(), FindGemByName);
  72. if (foundIt != gemInfos.end())
  73. {
  74. const AzFramework::GemInfo& gemInfo = *foundIt;
  75. for (const AZ::IO::Path& absoluteSourcePath : gemInfo.m_absoluteSourcePaths)
  76. {
  77. gemSourceAssetDirectories = (absoluteSourcePath / gemInfo.GetGemAssetFolder());
  78. }
  79. return AZ::IO::Path(gemSourceAssetDirectories.c_str());
  80. }
  81. }
  82. }
  83. return AZ::IO::Path();
  84. }
  85. AZ::IO::Path GetTranslationDefaultFolderPath()
  86. {
  87. AZ::IO::Path folderPath = GetGemAssetPath("ScriptCanvas") / "TranslationAssets";
  88. return folderPath;
  89. }
  90. AZ::IO::Path GetTranslationFilePath(const AZStd::string& fileName)
  91. {
  92. // Check asset safe folders where all loaded translation files
  93. AZStd::vector<AZStd::string> scanFolders;
  94. bool success = false;
  95. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
  96. success, &AzToolsFramework::AssetSystem::AssetSystemRequest::GetAssetSafeFolders, scanFolders);
  97. auto fileIO = AZ::IO::FileIOBase::GetInstance();
  98. if (fileIO && success && !fileName.empty())
  99. {
  100. AZStd::string fileNameWithExtension = AZStd::string::format("%s.names", fileName.c_str());
  101. for (const AZStd::string& assetSafeFolder : scanFolders)
  102. {
  103. AZStd::deque<AZStd::string> foldersToSearch;
  104. foldersToSearch.push_back(assetSafeFolder);
  105. AZ::IO::Result searchResult = AZ::IO::ResultCode::Success;
  106. AZStd::vector<AZStd::string> filesFound;
  107. while (foldersToSearch.size() && searchResult == AZ::IO::ResultCode::Success)
  108. {
  109. AZStd::string folderName = foldersToSearch.front();
  110. foldersToSearch.pop_front();
  111. searchResult = fileIO->FindFiles(folderName.c_str(), "*",
  112. [&](const char* path)
  113. {
  114. AZ::IO::Path currentPath = path;
  115. if (fileIO->IsDirectory(path))
  116. {
  117. foldersToSearch.push_back(path);
  118. }
  119. else if (currentPath.HasFilename()
  120. && strcmp(currentPath.Filename().LexicallyNormal().c_str(), fileNameWithExtension.c_str()) == 0)
  121. {
  122. filesFound.push_back(path);
  123. foldersToSearch.clear();
  124. return false;
  125. }
  126. return true;
  127. });
  128. }
  129. if (filesFound.size() == 1)
  130. {
  131. AZ::IO::FixedMaxPath resolvedPath("");
  132. fileIO->ResolvePath(resolvedPath, AZ::IO::PathView(filesFound.front()));
  133. return AZ::IO::Path(resolvedPath);
  134. }
  135. }
  136. }
  137. AZ_Warning("ScriptCanvas", false, AZStd::string::format("No matching translation file found. Please generate translation file first.").c_str());
  138. return AZ::IO::Path();
  139. }
  140. } // namespace ScriptCanvasEditor::TranslationHelper