ArticyXImporter
ArticyXImporter for Unreal Engine
Loading...
Searching...
No Matches
ArticyLocalizerSystem.h
1//
2// Copyright (c) 2023 articy Software GmbH & Co. KG. All rights reserved.
3//
4
5#pragma once
6
7#include "CoreMinimal.h"
8#include "ArticyType.h"
9#include "ArticyTextExtension.h"
10#include "Internationalization/StringTableRegistry.h"
11#include "UObject/UObjectIterator.h"
12#include "UObject/Package.h"
13#include "Internationalization/StringTableCore.h"
14#include "ArticyLocalizerSystem.generated.h"
15
16UCLASS(BlueprintType)
17class ARTICYRUNTIME_API UArticyLocalizerSystem : public UObject
18{
19 GENERATED_BODY()
20
21public:
22 static UArticyLocalizerSystem* Get()
23 {
24 static TWeakObjectPtr<UArticyLocalizerSystem> ArticyLocalizerSystem;
25 if (ArticyLocalizerSystem.IsValid())
26 {
27 return ArticyLocalizerSystem.Get();
28 }
29
30 UClass* ParentClass = UArticyLocalizerSystem::StaticClass();
31
32 // Iterate over all classes
33 for (TObjectIterator<UClass> It; It; ++It)
34 {
35 UClass* Class = *It;
36 // Check if the class is derived from the parent class
37 if (Class->IsChildOf(ParentClass) && Class != ParentClass)
38 {
39 // Create an instance of the found subclass
40 UArticyLocalizerSystem* NewLocalizerSystem = NewObject<UArticyLocalizerSystem>(GetTransientPackage(), Class);
41
42 // Assign the instance to the TWeakObjectPtr
43 ArticyLocalizerSystem = NewLocalizerSystem;
44
45 return ArticyLocalizerSystem.Get();
46 }
47 }
48
49 // Handle case where no subclass is found
50 return nullptr;
51 }
52
53 virtual void Reload() {};
54
55 inline FText ResolveText(UObject* Outer, const FText* SourceText)
56 {
57 return UArticyTextExtension::Get()->Resolve(Outer, SourceText);
58 }
59
60 inline FText LocalizeString(UObject* Outer, const FText& Key, bool ResolveTextExtension = true, const FText* BackupText = nullptr)
61 {
62 if (!bDataLoaded)
63 {
64 Reload();
65 }
66
67 const FText MissingEntry = FText::FromString("<MISSING STRING TABLE ENTRY>");
68
69 // Default to key
70 FText SourceString = Key;
71
72 // Look up entry in specified string table
73 TOptional<FString> TableName = FTextInspector::GetNamespace(Key);
74 if (!TableName.IsSet())
75 {
76 TableName = TEXT("ARTICY");
77 }
78
79 // Find the table
80 FStringTableConstPtr TablePtr = FStringTableRegistry::Get().FindStringTable(FName(TableName.GetValue()));
81 if (TablePtr.IsValid())
82 {
83 // Find the entry
84 const FStringTable* Table = TablePtr.Get();
85 FStringTableEntryConstPtr EntryPtr = Table->FindEntry(FTextKey(Key.ToString()));
86 if (EntryPtr.IsValid())
87 {
88 const FStringTableEntry* TableEntry = EntryPtr.Get();
89 SourceString = FText::FromString(TableEntry->GetSourceString());
90 }
91 }
92
93 if (!SourceString.IsEmpty() && !SourceString.EqualTo(MissingEntry) && !SourceString.EqualTo(Key))
94 {
95 if (ResolveTextExtension)
96 {
97 return ResolveText(Outer, &SourceString);
98 }
99 return FText(SourceString);
100 }
101
102 // By default, return via the key
103 if (ResolveTextExtension && !Key.ToString().EndsWith(".PreviewText"))
104 {
105 return ResolveText(Outer, &Key);
106 }
107
108 // Return backup text, if relevant
109 if (BackupText)
110 {
111 return *BackupText;
112 }
113
114 return Key;
115 }
116
117protected:
118 bool bDataLoaded = false;
119 bool bListenerSet = false;
120};
Definition ArticyLocalizerSystem.h:18