2020-12-22 03:13:18 +00:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Linq ;
2022-06-18 18:04:24 +00:00
namespace PKHeX.Core ;
public static class LocalizationUtil
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
private const string TranslationSplitter = " = " ;
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
/// <summary>
/// Gets the names of the properties defined in the given input
/// </summary>
/// <param name="input">Enumerable of translation definitions in the form "Property = Value".</param>
private static string [ ] GetProperties ( IEnumerable < string > input )
{
static string AfterSplit ( string l )
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
var split = l . IndexOf ( TranslationSplitter , StringComparison . Ordinal ) ;
return l [ . . split ] ;
2020-12-22 03:13:18 +00:00
}
2022-06-18 18:04:24 +00:00
return input . Select ( AfterSplit ) . ToArray ( ) ;
}
private static IEnumerable < string > DumpStrings ( Type t )
{
var props = ReflectUtil . GetPropertiesStartWithPrefix ( t , string . Empty ) ;
return props . Select ( p = > $"{p}{TranslationSplitter}{ReflectUtil.GetValue(t, p)}" ) ;
}
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
/// <summary>
/// Gets the current localization in a static class containing language-specific strings
/// </summary>
/// <param name="t"></param>
public static string [ ] GetLocalization ( Type t ) = > DumpStrings ( t ) . ToArray ( ) ;
/// <summary>
/// Gets the current localization in a static class containing language-specific strings
/// </summary>
/// <param name="t"></param>
/// <param name="existingLines">Existing localization lines (if provided)</param>
public static string [ ] GetLocalization ( Type t , string [ ] existingLines )
{
var currentLines = GetLocalization ( t ) ;
var existing = GetProperties ( existingLines ) ;
var current = GetProperties ( currentLines ) ;
var result = new string [ currentLines . Length ] ;
for ( int i = 0 ; i < current . Length ; i + + )
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
int index = Array . IndexOf ( existing , current [ i ] ) ;
result [ i ] = index < 0 ? currentLines [ i ] : existingLines [ index ] ;
2020-12-22 03:13:18 +00:00
}
2022-06-18 18:04:24 +00:00
return result ;
}
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
/// <summary>
/// Applies localization to a static class containing language-specific strings.
/// </summary>
/// <param name="t">Type of the static class containing the desired strings.</param>
/// <param name="lines">Lines containing the localized strings</param>
private static void SetLocalization ( Type t , IReadOnlyCollection < string > lines )
{
if ( lines . Count = = 0 )
return ;
foreach ( var line in lines )
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
var index = line . IndexOf ( TranslationSplitter , StringComparison . Ordinal ) ;
if ( index < 0 )
continue ;
var prop = line [ . . index ] ;
var value = line [ ( index + TranslationSplitter . Length ) . . ] ;
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
try
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
ReflectUtil . SetValue ( t , prop , value ) ;
2020-12-22 03:13:18 +00:00
}
2022-06-18 18:04:24 +00:00
// Malformed translation files, log
catch ( Exception e )
2020-12-22 03:13:18 +00:00
{
2022-06-18 18:04:24 +00:00
Debug . WriteLine ( $"Property not present: {prop} || Value written: {value}" ) ;
Debug . WriteLine ( e . Message ) ;
2020-12-22 03:13:18 +00:00
}
}
2022-06-18 18:04:24 +00:00
}
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
/// <summary>
/// Applies localization to a static class containing language-specific strings.
/// </summary>
/// <param name="t">Type of the static class containing the desired strings.</param>
/// <param name="languageFilePrefix">Prefix of the language file to use. Example: if the target is legality_en.txt, <paramref name="languageFilePrefix"/> should be "legality".</param>
/// <param name="currentCultureCode">Culture information</param>
private static void SetLocalization ( Type t , string languageFilePrefix , string currentCultureCode )
{
var lines = Util . GetStringList ( $"{languageFilePrefix}_{currentCultureCode}" ) ;
SetLocalization ( t , lines ) ;
}
2020-12-22 03:13:18 +00:00
2022-06-18 18:04:24 +00:00
/// <summary>
/// Applies localization to a static class containing language-specific strings.
/// </summary>
/// <param name="t">Type of the static class containing the desired strings.</param>
/// <remarks>The values used to translate the given static class are retrieved from [TypeName]_[CurrentLangCode2].txt in the resource manager of PKHeX.Core.</remarks>
/// <param name="currentCultureCode">Culture information</param>
public static void SetLocalization ( Type t , string currentCultureCode )
{
SetLocalization ( t , t . Name , currentCultureCode ) ;
2020-12-22 03:13:18 +00:00
}
}