-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTolgeeProviderLocalizationServiceOperations.cpp
More file actions
194 lines (151 loc) · 8.06 KB
/
TolgeeProviderLocalizationServiceOperations.cpp
File metadata and controls
194 lines (151 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Tolgee 2022-2025. All Rights Reserved.
#include "TolgeeProviderLocalizationServiceOperations.h"
#include <LocalizationServiceOperations.h>
#include <Interfaces/IHttpResponse.h>
#include <HttpModule.h>
#include "TolgeeProviderLocalizationServiceCommand.h"
#include "TolgeeEditorSettings.h"
#include "TolgeeLog.h"
#include "TolgeeProviderUtils.h"
#include "TolgeeUtils.h"
FName FTolgeeProviderUploadFileWorker::GetName() const
{
return "UploadFileWorker";
}
bool FTolgeeProviderUploadFileWorker::Execute(FTolgeeProviderLocalizationServiceCommand& InCommand)
{
TSharedPtr<FUploadLocalizationTargetFile> UploadFileOp = StaticCastSharedRef<FUploadLocalizationTargetFile>(InCommand.Operation);
if (!UploadFileOp || !UploadFileOp.IsValid())
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Invalid operation"));
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
const FString FilePathAndName = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir() / UploadFileOp->GetInRelativeInputFilePathAndName());
FString FileContents;
if (!FFileHelper::LoadFileToString(FileContents, *FilePathAndName))
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Cannot load file %s"), *FilePathAndName);
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
const FGuid TargetGuid = UploadFileOp->GetInTargetGuid();
const FString Locale = UploadFileOp->GetInLocale();
//NOTE: This is currently not set by the Localization dashbard, when it becomes available send it as "removeOtherKeys"
const bool bRemoveMissingKeys = !UploadFileOp->GetPreserveAllText();
TSharedRef<FJsonObject> FileMapping = MakeShared<FJsonObject>();
const FString FileName = FPaths::GetCleanFilename(FilePathAndName);
FileMapping->SetStringField(TEXT("fileName"), FileName);
TSharedRef<FJsonObject> Params = MakeShared<FJsonObject>();
Params->SetBoolField(TEXT("convertPlaceholdersToIcu"), false);
Params->SetBoolField(TEXT("createNewKeys"), true);
Params->SetArrayField(TEXT("fileMappings"), {MakeShared<FJsonValueObject>(FileMapping)});
Params->SetStringField(TEXT("forceMode"), TEXT("KEEP"));
Params->SetBoolField(TEXT("overrideKeyDescriptions"), true);
Params->SetBoolField(TEXT("removeOtherKeys"), true);
Params->SetArrayField(TEXT("tagNewKeys"), {MakeShared<FJsonValueString>(TEXT("UnrealSDK"))});
FString ParamsContents;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&ParamsContents);
FJsonSerializer::Serialize(Params, Writer);
const UTolgeeEditorSettings* ProviderSettings = GetDefault<UTolgeeEditorSettings>();
const FTolgeePerTargetSettings* ProjectSettings = ProviderSettings->PerTargetSettings.Find(TargetGuid);
const FString Url = FString::Printf(TEXT("%s/v2/projects/%s/single-step-import"), *ProviderSettings->GetBaseUrl(), *ProjectSettings->ProjectId);
FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL(Url);
HttpRequest->SetVerb(TEXT("POST"));
HttpRequest->SetHeader(TEXT("X-API-Key"), ProviderSettings->ApiKey);
TolgeeUtils::AddSdkHeaders(HttpRequest);
const FString Boundary = "---------------------------" + FString::FromInt(FDateTime::Now().GetTicks());
TolgeeProviderUtils::AddMultiRequestHeader(HttpRequest, Boundary);
TolgeeProviderUtils::AddMultiRequestPart(HttpRequest, Boundary, TEXT("name=\"params\""), ParamsContents);
TolgeeProviderUtils::AddMultiRequestPart(HttpRequest, Boundary, TEXT("name=\"files\"; filename=\"test.po\""), FileContents);
TolgeeProviderUtils::FinishMultiRequest(HttpRequest, Boundary);
HttpRequest->SetDelegateThreadPolicy(EHttpRequestDelegateThreadPolicy::CompleteOnHttpThread);
HttpRequest->ProcessRequestUntilComplete();
FHttpResponsePtr Response = HttpRequest->GetResponse();
if (!Response)
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Failed to upload file %s"), *FilePathAndName);
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
if (!EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Failed to upload file %s. Response code: %d"), *FilePathAndName, Response->GetResponseCode());
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Response: %s"), *Response->GetContentAsString());
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
UE_LOG(LogTolgee, Display, TEXT("FTolgeeProviderUploadFileWorker: Successfully uploaded file %s. Content: %s"), *FilePathAndName, *Response->GetContentAsString());
InCommand.bCommandSuccessful = true;
return InCommand.bCommandSuccessful;
}
bool FTolgeeProviderUploadFileWorker::UpdateStates() const
{
return true;
}
FName FTolgeeProviderDownloadFileWorker::GetName() const
{
return "DownloadFileWorker";
}
bool FTolgeeProviderDownloadFileWorker::Execute(FTolgeeProviderLocalizationServiceCommand& InCommand)
{
TSharedPtr<FDownloadLocalizationTargetFile> DownloadFileOp = StaticCastSharedRef<FDownloadLocalizationTargetFile>(InCommand.Operation);
if (!DownloadFileOp || !DownloadFileOp.IsValid())
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderDownloadFileWorker: Invalid operation"));
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
const FString FilePathAndName = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir() / DownloadFileOp->GetInRelativeOutputFilePathAndName());
const FGuid TargetGuid = DownloadFileOp->GetInTargetGuid();
const FString Locale = DownloadFileOp->GetInLocale();
const UTolgeeEditorSettings* ProviderSettings = GetDefault<UTolgeeEditorSettings>();
const FTolgeePerTargetSettings* ProjectSettings = ProviderSettings->PerTargetSettings.Find(TargetGuid);
const FString Url = FString::Printf(TEXT("%s/v2/projects/%s/export"), *ProviderSettings->GetBaseUrl(), *ProjectSettings->ProjectId);
FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetURL(Url);
HttpRequest->SetVerb(TEXT("POST"));
HttpRequest->SetHeader(TEXT("X-API-Key"), ProviderSettings->ApiKey);
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
TolgeeUtils::AddSdkHeaders(HttpRequest);
TSharedRef<FJsonObject> Body = MakeShared<FJsonObject>();
Body->SetBoolField(TEXT("escapeHtml"), false);
Body->SetStringField(TEXT("format"), "PO");
Body->SetBoolField(TEXT("supportArrays"), false);
Body->SetBoolField(TEXT("zip"), false);
Body->SetArrayField(TEXT("languages"), {MakeShared<FJsonValueString>(Locale)});
FString RequestBody;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&RequestBody);
FJsonSerializer::Serialize(Body, Writer);
HttpRequest->SetContentAsString(RequestBody);
HttpRequest->SetDelegateThreadPolicy(EHttpRequestDelegateThreadPolicy::CompleteOnHttpThread);
HttpRequest->ProcessRequestUntilComplete();
FHttpResponsePtr Response = HttpRequest->GetResponse();
if (!Response)
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Failed to download file %s"), *FilePathAndName);
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
if (!EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Failed to download file %s. Response code: %d"), *FilePathAndName, Response->GetResponseCode());
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderUploadFileWorker: Response: %s"), *Response->GetContentAsString());
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
if (!FFileHelper::SaveStringToFile(Response->GetContentAsString(), *FilePathAndName, FFileHelper::EEncodingOptions::ForceUnicode))
{
UE_LOG(LogTolgee, Error, TEXT("FTolgeeProviderDownloadFileWorker: Failed to write file %s"), *FilePathAndName);
InCommand.bCommandSuccessful = false;
return InCommand.bCommandSuccessful;
}
UE_LOG(LogTolgee, Display, TEXT("FTolgeeProviderUploadFileWorker: Successfully downloaded file %s. Content: %s"), *FilePathAndName, *Response->GetContentAsString());
InCommand.bCommandSuccessful = true;
return InCommand.bCommandSuccessful;
}
bool FTolgeeProviderDownloadFileWorker::UpdateStates() const
{
return true;
}