forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#2 from Microsoft/xplat
sync to latest
- Loading branch information
Showing
312 changed files
with
20,050 additions
and
2,744 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.26-prerelease-00826-05 | ||
1.0.27-prerelease-00927-05 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.Build.LocalizationTasks | ||
{ | ||
public class ConvertToNeutralXlf : Task | ||
{ | ||
/// <summary> | ||
/// Path to the neutral resx files to generate a corresponding neutral xlf from. | ||
/// </summary> | ||
[Required] | ||
public ITaskItem[] NeutralResources { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
if (NeutralResources.Length == 0) | ||
{ | ||
Log.LogError($"Task was called with empty {nameof(NeutralResources)}"); | ||
} | ||
|
||
foreach (var neutralResource in NeutralResources) | ||
{ | ||
var localizedXlf = LocalizationUtils.LocalizedXlfFiles(neutralResource).FirstOrDefault(); | ||
|
||
if (localizedXlf == null) | ||
{ | ||
Log.LogError($"{neutralResource} has no corresponding xlf files"); | ||
} | ||
|
||
var outputFilename = ComputeNeutralXlfName(neutralResource); | ||
|
||
MakeNeutral(localizedXlf, outputFilename); | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
private string ComputeNeutralXlfName(ITaskItem neutralResouce) | ||
{ | ||
var filename = neutralResouce.GetMetadata("Filename"); | ||
var xlfRootPath = LocalizationUtils.ComputeXlfRootPath(neutralResouce); | ||
|
||
return Path.Combine(xlfRootPath, filename + ".xlf"); | ||
} | ||
|
||
private static void MakeNeutral(string inputfilename, string outputfilename) | ||
{ | ||
//need to load xml file | ||
var doc = XDocument.Load(inputfilename); | ||
|
||
//step 1: remove target-language attribute | ||
//< file datatype = "xml" source - language = "en" target - language = "cs" original = "../Strings.shared.resx" > | ||
var fileNodes = from node in doc.Descendants() | ||
where node.Name.LocalName != null && node.Name.LocalName == "file" | ||
select node; | ||
fileNodes.ToList().ForEach(x => | ||
{ | ||
if (x.HasAttributes) | ||
{ | ||
foreach (var attrib in x.Attributes()) | ||
{ | ||
if (attrib.Name == "target-language") | ||
attrib.Remove(); | ||
} | ||
} | ||
}); | ||
|
||
//step 2: remove all tags with "target" | ||
// < target state = "new" > MSBuild is expecting a valid "{0}" object.</ target > | ||
var targetNodes = from node in doc.Descendants() | ||
where node.Name.LocalName != null && node.Name.LocalName == "target" | ||
select node; | ||
targetNodes.ToList().ForEach(x => x.Remove()); | ||
|
||
//save | ||
var fi = new FileInfo(outputfilename); | ||
|
||
if (fi.Exists) | ||
{ | ||
fi.Delete(); | ||
} | ||
if (fi.Directory.Exists == false) | ||
{ | ||
fi.Directory.Create(); | ||
} | ||
|
||
doc.Save(fi.FullName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.Build.LocalizationTasks | ||
{ | ||
public static class LocalizationUtils | ||
{ | ||
/// <summary> | ||
/// Path to the root directory that contains the xlf files for a neutral resource | ||
/// Assumes all neutral resource have the same relative path to their xlf | ||
/// </summary> | ||
public static string RelativePathToXlfRoot = "xlf"; | ||
|
||
public static string ComputeXlfRootPath(ITaskItem neutralResource) | ||
{ | ||
var neutralResxRootDirectory = Path.GetDirectoryName(neutralResource.GetMetadata("FullPath")); | ||
return Path.Combine(neutralResxRootDirectory, RelativePathToXlfRoot); | ||
} | ||
|
||
public static bool IsValidLocalizedXlfName(string xlfPath, string neutralResx) | ||
{ | ||
var resxFileName = Path.GetFileNameWithoutExtension(neutralResx); | ||
var xlfFileName = Path.GetFileName(xlfPath); | ||
|
||
return Regex.IsMatch(xlfFileName, $"^{resxFileName}\\.[a-zA-Z\\-]+\\.xlf$"); | ||
} | ||
|
||
public static IEnumerable<string> LocalizedXlfFiles(string xlfRootPath, string neutralResx) | ||
{ | ||
return Directory.EnumerateFiles(xlfRootPath) | ||
.Where(f => IsValidLocalizedXlfName(f, neutralResx)); | ||
} | ||
|
||
public static IEnumerable<string> LocalizedXlfFiles(ITaskItem neutralResouce) | ||
{ | ||
return LocalizedXlfFiles(ComputeXlfRootPath(neutralResouce), neutralResouce.ItemSpec); | ||
} | ||
} | ||
} |
18 changes: 8 additions & 10 deletions
18
build/LocalizationTasks/Microsoft.Build.LocalizationTasks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
|
||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>Microsoft.Build.LocalizationTasks</RootNamespace> | ||
<AssemblyName>Microsoft.Build.LocalizationTasks</AssemblyName> | ||
<ProjectGuid>{06347A6C-32A4-4218-A930-0A22699C726C}</ProjectGuid> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- Source Files --> | ||
<Compile Include="*.cs"/> | ||
<Compile Include="*.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="System"/> | ||
<Reference Include="System.Text.RegularExpressions"/> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Text.RegularExpressions" /> | ||
<Reference Include="Microsoft.Build.Framework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> | ||
<Reference Include="Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\dir.targets" /> | ||
|
||
</Project> | ||
</Project> |
Oops, something went wrong.