add automated language string extraction to utility
This commit is contained in:
@@ -20,6 +20,11 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
public static class Exts
|
public static class Exts
|
||||||
{
|
{
|
||||||
|
public static bool IsUppercase(this string str)
|
||||||
|
{
|
||||||
|
return string.Compare(str.ToUpperInvariant(), str, false) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
public static string F(this string fmt, params object[] args)
|
public static string F(this string fmt, params object[] args)
|
||||||
{
|
{
|
||||||
return string.Format(fmt, args);
|
return string.Format(fmt, args);
|
||||||
|
|||||||
@@ -111,9 +111,13 @@ namespace OpenRA
|
|||||||
public IEnumerable<Type> GetTypesImplementing<T>()
|
public IEnumerable<Type> GetTypesImplementing<T>()
|
||||||
{
|
{
|
||||||
var it = typeof(T);
|
var it = typeof(T);
|
||||||
|
return GetTypes().Where(t => t != it && it.IsAssignableFrom(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Type> GetTypes()
|
||||||
|
{
|
||||||
return assemblies.Select(ma => ma.First).Distinct()
|
return assemblies.Select(ma => ma.First).Distinct()
|
||||||
.SelectMany(ma => ma.GetTypes()
|
.SelectMany(ma => ma.GetTypes());
|
||||||
.Where(t => t != it && it.IsAssignableFrom(t)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Constructor)]
|
[AttributeUsage(AttributeTargets.Constructor)]
|
||||||
|
|||||||
68
OpenRA.Utility/ExtractLanguageStrings.cs
Normal file
68
OpenRA.Utility/ExtractLanguageStrings.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
|
||||||
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
|
* available to you under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation. For more information,
|
||||||
|
* see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace OpenRA.Utility
|
||||||
|
{
|
||||||
|
public class ExtractLanguageStrings
|
||||||
|
{
|
||||||
|
[Desc("MOD", "Extract translatable strings that are not yet localized and update chrome layout.")]
|
||||||
|
public static void FromMod(string[] args)
|
||||||
|
{
|
||||||
|
var mod = args[1];
|
||||||
|
Game.modData = new ModData(mod);
|
||||||
|
Game.modData.RulesetCache.LoadDefaultRules();
|
||||||
|
|
||||||
|
var types = Game.modData.ObjectCreator.GetTypes();
|
||||||
|
var translatableFields = types.SelectMany(t => t.GetFields())
|
||||||
|
.Where(f => f.HasAttribute<TranslateAttribute>()).Distinct();
|
||||||
|
|
||||||
|
foreach (var filename in Game.modData.Manifest.ChromeLayout)
|
||||||
|
{
|
||||||
|
Console.WriteLine("# {0}:", filename);
|
||||||
|
var yaml = MiniYaml.FromFile(filename);
|
||||||
|
ExtractLanguageStrings.FromChromeLayout(ref yaml, null,
|
||||||
|
translatableFields.Select(t => t.Name).Distinct(), null);
|
||||||
|
using (var file = new StreamWriter(filename))
|
||||||
|
file.WriteLine(yaml.WriteToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Properties can also be translated.
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void FromChromeLayout(ref List<MiniYamlNode> nodes, MiniYamlNode parent, IEnumerable<string> translatables, string container)
|
||||||
|
{
|
||||||
|
var parentNode = parent != null ? parent.Key.Split('@') : null;
|
||||||
|
var parentType = parent != null ? parentNode.First() : null;
|
||||||
|
var parentLabel = parent != null ? parentNode.Last() : null;
|
||||||
|
|
||||||
|
if ((parentType == "Background" || parentType == "Container") && parentLabel.IsUppercase())
|
||||||
|
container = parentLabel;
|
||||||
|
|
||||||
|
foreach (var node in nodes)
|
||||||
|
{
|
||||||
|
var alreadyTranslated = node.Value.Value != null && node.Value.Value.Contains('@');
|
||||||
|
if (translatables.Contains(node.Key) && !alreadyTranslated)
|
||||||
|
{
|
||||||
|
var translationKey = "{0}-{1}-{2}".F(container.Replace('_', '-'), parentLabel.Replace('_', '-'), node.Key.ToUpper());
|
||||||
|
Console.WriteLine("\t{0}: {1}", translationKey , node.Value.Value);
|
||||||
|
node.Value.Value = "@{0}@".F(translationKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
FromChromeLayout(ref node.Value.Nodes, node, translatables, container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -80,6 +80,7 @@
|
|||||||
<Compile Include="UpgradeRules.cs" />
|
<Compile Include="UpgradeRules.cs" />
|
||||||
<Compile Include="LegacyMapImporter.cs" />
|
<Compile Include="LegacyMapImporter.cs" />
|
||||||
<Compile Include="Glob.cs" />
|
<Compile Include="Glob.cs" />
|
||||||
|
<Compile Include="ExtractLanguageStrings.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
|
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#region Copyright & License Information
|
#region Copyright & License Information
|
||||||
/*
|
/*
|
||||||
* Copyright 2007-2012 The OpenRA Developers (see AUTHORS)
|
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
|
||||||
* This file is part of OpenRA, which is free software. It is made
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
* available to you under the terms of the GNU General Public License
|
* available to you under the terms of the GNU General Public License
|
||||||
* as published by the Free Software Foundation. For more information,
|
* as published by the Free Software Foundation. For more information,
|
||||||
@@ -33,7 +33,8 @@ namespace OpenRA.Utility
|
|||||||
{ "--map-upgrade-v5", Command.UpgradeV5Map },
|
{ "--map-upgrade-v5", Command.UpgradeV5Map },
|
||||||
{ "--upgrade-map", UpgradeRules.UpgradeMap },
|
{ "--upgrade-map", UpgradeRules.UpgradeMap },
|
||||||
{ "--upgrade-mod", UpgradeRules.UpgradeMod },
|
{ "--upgrade-mod", UpgradeRules.UpgradeMod },
|
||||||
{ "--map-import", Command.ImportLegacyMap }
|
{ "--map-import", Command.ImportLegacyMap },
|
||||||
|
{ "--extract-language-strings", ExtractLanguageStrings.FromMod }
|
||||||
};
|
};
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
|
|||||||
Reference in New Issue
Block a user