2016-01-07 08:17:16 -05:00
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
2014-03-14 14:13:41 -04:00
# include "UnrealEd.h"
# include "TextLocalizationResourceGenerator.h"
2014-06-30 10:25:10 -04:00
# include "Json.h"
2014-03-14 14:13:41 -04:00
# include "InternationalizationManifest.h"
2014-06-30 10:25:10 -04:00
# include "InternationalizationArchive.h"
2014-09-25 18:03:04 -04:00
# include "JsonInternationalizationManifestSerializer.h"
# include "JsonInternationalizationArchiveSerializer.h"
2014-03-14 14:13:41 -04:00
DEFINE_LOG_CATEGORY_STATIC ( LogGenerateTextLocalizationResourceCommandlet , Log , All ) ;
2014-10-14 10:29:11 -04:00
UGenerateTextLocalizationResourceCommandlet : : UGenerateTextLocalizationResourceCommandlet ( const FObjectInitializer & ObjectInitializer )
: Super ( ObjectInitializer )
2014-03-14 14:13:41 -04:00
{
}
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 ;
2015-01-05 08:52:06 -05:00
if ( ! ( GetPathFromConfig ( * SectionName , TEXT ( " SourcePath " ) , SourcePath , GatherTextConfigPath ) ) )
2014-03-14 14:13:41 -04:00
{
UE_LOG ( LogGenerateTextLocalizationResourceCommandlet , Error , TEXT ( " No source path specified. " ) ) ;
return - 1 ;
}
// Get manifest name.
FString ManifestName ;
2015-01-05 08:52:06 -05:00
if ( ! ( GetStringFromConfig ( * SectionName , TEXT ( " ManifestName " ) , ManifestName , GatherTextConfigPath ) ) )
2014-03-14 14:13:41 -04:00
{
UE_LOG ( LogGenerateTextLocalizationResourceCommandlet , Error , TEXT ( " No manifest name specified. " ) ) ;
return - 1 ;
}
2015-06-25 19:32:27 -04:00
// Get cultures to generate.
FString NativeCultureName ;
if ( ! ( GetStringFromConfig ( * SectionName , TEXT ( " NativeCulture " ) , NativeCultureName , GatherTextConfigPath ) ) )
{
UE_LOG ( LogGenerateTextLocalizationResourceCommandlet , Error , TEXT ( " No native culture specified. " ) ) ;
return - 1 ;
}
2014-03-14 14:13:41 -04:00
// Get cultures to generate.
TArray < FString > CulturesToGenerate ;
2015-01-05 08:52:06 -05:00
GetStringArrayFromConfig ( * SectionName , TEXT ( " CulturesToGenerate " ) , CulturesToGenerate , GatherTextConfigPath ) ;
2014-03-14 14:13:41 -04:00
if ( CulturesToGenerate . Num ( ) = = 0 )
{
UE_LOG ( LogGenerateTextLocalizationResourceCommandlet , Error , TEXT ( " No cultures specified for generation. " ) ) ;
return - 1 ;
}
for ( int32 i = 0 ; i < CulturesToGenerate . Num ( ) ; + + i )
{
2014-04-23 18:30:01 -04:00
if ( FInternationalization : : Get ( ) . GetCulture ( CulturesToGenerate [ i ] ) . IsValid ( ) )
2014-03-14 14:13:41 -04:00
{
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 ;
2015-01-05 08:52:06 -05:00
if ( ! ( GetPathFromConfig ( * SectionName , TEXT ( " DestinationPath " ) , DestinationPath , GatherTextConfigPath ) ) )
2014-03-14 14:13:41 -04:00
{
UE_LOG ( LogGenerateTextLocalizationResourceCommandlet , Error , TEXT ( " No destination path specified. " ) ) ;
return - 1 ;
}
// Get resource name.
FString ResourceName ;
2015-01-05 08:52:06 -05:00
if ( ! ( GetStringFromConfig ( * SectionName , TEXT ( " ResourceName " ) , ResourceName , GatherTextConfigPath ) ) )
2014-03-14 14:13:41 -04:00
{
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 ) ;
{
2014-09-25 18:03:04 -04:00
FJsonInternationalizationManifestSerializer ManifestSerializer ;
2014-03-14 14:13:41 -04:00
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 ) ) ;
2014-08-28 17:35:37 -04:00
if ( TextLocalizationResourceArchive )
2014-03-14 14:13:41 -04:00
{
2014-09-25 18:03:04 -04:00
FJsonInternationalizationArchiveSerializer ArchiveSerializer ;
2015-06-25 19:32:27 -04:00
if ( ! ( FTextLocalizationResourceGenerator : : Generate ( SourcePath , InternationalizationManifest , NativeCultureName , CultureName , TextLocalizationResourceArchive , ArchiveSerializer ) ) )
2014-08-28 17:35:37 -04:00
{
IFileManager : : Get ( ) . Delete ( * TextLocalizationResourcePath ) ;
}
TextLocalizationResourceArchive - > Close ( ) ;
2014-03-14 14:13:41 -04:00
}
}
return 0 ;
}