Files
UnrealEngineUWP/Engine/Source/Editor/UnrealEd/Private/Commandlets/GenerateTextLocalizationResourceCommandlet.cpp
Max Preussner 6510058ee7 Core: Moved Json out of Core and into its own module
The main changes are as follows:

1. moved Json out of Core into own module 'Json'
2. moved 3 i10n classes (Json serializers) from Core into a new module 'Internationalization' *
3. fixed up 2 i10n classes in Core to not instantiate the 3 Json-based classes. instead they are now passed in as a dependency

*) (2) and (3) were required to decouple the I10n code in Core from Json. Much of the i10n code probably doesn't belong into Core in the first place, but there is no time to fix this right now.

The following cosmetic changes were also made:

- NULL to nullptr
- namespaced enums to enum classes
- renamed the three i10n Json serializer classes to comply with naming conventions
- removed file header comments (not used)
- documentation, spelling, spacing etc,

#UpgradeNotes: If your module is including Json.h then you have to add 'Json' to your Build.cs module dependencies.

#ReviewedBy: justin.sargent, saul.abreu

[CL 2310420 by Max Preussner in Main branch]
2014-09-25 18:03:04 -04:00

176 lines
5.6 KiB
C++

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "UnrealEd.h"
#include "TextLocalizationResourceGenerator.h"
#include "Json.h"
#include "InternationalizationManifest.h"
#include "InternationalizationArchive.h"
#include "JsonInternationalizationManifestSerializer.h"
#include "JsonInternationalizationArchiveSerializer.h"
DEFINE_LOG_CATEGORY_STATIC(LogGenerateTextLocalizationResourceCommandlet, Log, All);
UGenerateTextLocalizationResourceCommandlet::UGenerateTextLocalizationResourceCommandlet(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
}
int32 UGenerateTextLocalizationResourceCommandlet::Main(const FString& Params)
{
// Parse command line - we're interested in the param vals
TArray<FString> Tokens;
TArray<FString> Switches;
TMap<FString, FString> ParamVals;
UCommandlet::ParseCommandLine(*Params, Tokens, Switches, ParamVals);
// Set config file.
const FString* ParamVal = ParamVals.Find(FString(TEXT("Config")));
FString GatherTextConfigPath;
if ( ParamVal )
{
GatherTextConfigPath = *ParamVal;
}
else
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No config specified."));
return -1;
}
// Set config section.
ParamVal = ParamVals.Find(FString(TEXT("Section")));
FString SectionName;
if ( ParamVal )
{
SectionName = *ParamVal;
}
else
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No config section specified."));
return -1;
}
// Get source path.
FString SourcePath;
if( !( GetConfigString( *SectionName, TEXT("SourcePath"), SourcePath, GatherTextConfigPath ) ) )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No source path specified."));
return -1;
}
if (FPaths::IsRelative(SourcePath))
{
if (!FPaths::GameDir().IsEmpty())
{
SourcePath = FPaths::Combine( *( FPaths::GameDir() ), *SourcePath );
}
else
{
SourcePath = FPaths::Combine( *( FPaths::EngineDir() ), *SourcePath );
}
}
// Get manifest name.
FString ManifestName;
if( !( GetConfigString( *SectionName, TEXT("ManifestName"), ManifestName, GatherTextConfigPath ) ) )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No manifest name specified."));
return -1;
}
// Get cultures to generate.
TArray<FString> CulturesToGenerate;
GetConfigArray( *SectionName, TEXT("CulturesToGenerate"), CulturesToGenerate, GatherTextConfigPath );
if( CulturesToGenerate.Num() == 0 )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No cultures specified for generation."));
return -1;
}
for(int32 i = 0; i < CulturesToGenerate.Num(); ++i)
{
if( FInternationalization::Get().GetCulture( CulturesToGenerate[i] ).IsValid() )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Verbose, TEXT("Specified culture is not a valid runtime culture, but may be a valid base language: %s"), *(CulturesToGenerate[i]));
}
}
// Get destination path.
FString DestinationPath;
if( !( GetConfigString( *SectionName, TEXT("DestinationPath"), DestinationPath, GatherTextConfigPath ) ) )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No destination path specified."));
return -1;
}
if (FPaths::IsRelative(DestinationPath))
{
if (!FPaths::GameDir().IsEmpty())
{
DestinationPath = FPaths::Combine( *( FPaths::GameDir() ), *DestinationPath );
}
else
{
DestinationPath = FPaths::Combine( *( FPaths::EngineDir() ), *DestinationPath );
}
}
// Get resource name.
FString ResourceName;
if( !( GetConfigString( *SectionName, TEXT("ResourceName"), ResourceName, GatherTextConfigPath ) ) )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No resource name specified."));
return -1;
}
// Read the manifest file from the source path.
FString ManifestFilePath = (SourcePath / ManifestName);
ManifestFilePath = FPaths::ConvertRelativePathToFull(ManifestFilePath);
TSharedPtr<FJsonObject> ManifestJSONObject = ReadJSONTextFile(ManifestFilePath);
if( !(ManifestJSONObject.IsValid()) )
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("No manifest found at %s."), *ManifestFilePath);
return -1;
}
TSharedRef<FInternationalizationManifest> InternationalizationManifest = MakeShareable( new FInternationalizationManifest );
{
FJsonInternationalizationManifestSerializer ManifestSerializer;
ManifestSerializer.DeserializeManifest(ManifestJSONObject.ToSharedRef(), InternationalizationManifest);
}
// For each culture:
for(int32 Culture = 0; Culture < CulturesToGenerate.Num(); Culture++)
{
const FString CultureName = *(CulturesToGenerate[Culture]);
const FString CulturePath = SourcePath / CultureName;
// Write resource.
const FString TextLocalizationResourcePath = DestinationPath / CultureName / ResourceName;
if( SourceControlInfo.IsValid() )
{
FText SCCErrorText;
if (!SourceControlInfo->CheckOutFile(TextLocalizationResourcePath, SCCErrorText))
{
UE_LOG(LogGenerateTextLocalizationResourceCommandlet, Error, TEXT("Check out of file %s failed: %s"), *TextLocalizationResourcePath, *SCCErrorText.ToString());
return -1;
}
}
TAutoPtr<FArchive> TextLocalizationResourceArchive(IFileManager::Get().CreateFileWriter(*TextLocalizationResourcePath));
if (TextLocalizationResourceArchive)
{
FJsonInternationalizationArchiveSerializer ArchiveSerializer;
if( !(FTextLocalizationResourceGenerator::Generate(SourcePath, InternationalizationManifest, CultureName, TextLocalizationResourceArchive, ArchiveSerializer)) )
{
IFileManager::Get().Delete( *TextLocalizationResourcePath );
}
TextLocalizationResourceArchive->Close();
}
}
return 0;
}