Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Cultures Within Names For EmbeddedResources #5824

Merged
23 changes: 23 additions & 0 deletions src/Tasks.UnitTests/CreateCSharpManifestResourceName_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ public void RootnamespaceWithCulture()
Assert.Equal("RootNamespace.SubFolder.MyForm.en-GB", result);
}

/// <summary>
/// Explicitly retain culture
/// </summary>
[Fact]
public void RootnamespaceWithCulture_RetainCultureInFileName()
{
string result =
CreateCSharpManifestResourceName.CreateManifestNameImpl
(
@"Subfolder\File.cs.cshtml",
benvillalobos marked this conversation as resolved.
Show resolved Hide resolved
null,
true,
"RootNamespace", // Root namespace
null,
null,
null,
null,
true // retain culture in name
);

result.ShouldBe("RootNamespace.Subfolder.File.cs.cshtml");
}

/// <summary>
/// If there is a link file name then it is preferred over the main file name.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/Tasks/AssignCulture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public override bool Execute()
Culture.ItemCultureInfo info = Culture.GetItemCultureInfo
(
AssignedFiles[i].ItemSpec,
dependentUpon
dependentUpon,
// If 'WithCulture' is explicitly set to false, treat as 'culture-neutral' and keep the original name of the resource.
// https://github.com/dotnet/msbuild/issues/3064
AssignedFiles[i].GetMetadata("WithCulture").Equals("false", StringComparison.OrdinalIgnoreCase)
);

if (!string.IsNullOrEmpty(info.culture))
Expand Down
13 changes: 10 additions & 3 deletions src/Tasks/CreateCSharpManifestResourceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ Stream binaryStream
)
{
string culture = null;
bool treatAsCultureNeutral = false;
if (fileName != null && itemSpecToTaskitem.TryGetValue(fileName, out ITaskItem item))
{
culture = item.GetMetadata("Culture");
// If 'WithCulture' is explicitly set to false, treat as 'culture-neutral' and keep the original name of the resource.
// https://github.com/dotnet/msbuild/issues/3064
treatAsCultureNeutral = item.GetMetadata("WithCulture").Equals("false", StringComparison.OrdinalIgnoreCase);
}

/*
Expand All @@ -59,7 +63,8 @@ Stream binaryStream
dependentUponFileName,
culture,
binaryStream,
Log
Log,
treatAsCultureNeutral
);
}

Expand All @@ -78,6 +83,7 @@ Stream binaryStream
/// <param name="culture">The override culture of this resource, if any</param>
/// <param name="binaryStream">File contents binary stream, may be null</param>
/// <param name="log">Task's TaskLoggingHelper, for logging warnings or errors</param>
/// <param name="treatAsCultureNeutral">Whether to treat the current file as 'culture-neutral' and retain the culture in the name.</param>
/// <returns>Returns the manifest name</returns>
internal static string CreateManifestNameImpl
(
Expand All @@ -88,7 +94,8 @@ internal static string CreateManifestNameImpl
string dependentUponFileName, // May be null
string culture, // may be null
Stream binaryStream, // File contents binary stream, may be null
TaskLoggingHelper log
TaskLoggingHelper log,
bool treatAsCultureNeutral = false
)
{
// Use the link file name if there is one, otherwise, fall back to file name.
Expand All @@ -99,7 +106,7 @@ TaskLoggingHelper log
}

dependentUponFileName = FileUtilities.FixFilePath(dependentUponFileName);
Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName);
Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName, treatAsCultureNeutral);

// If the item has a culture override, respect that.
if (!string.IsNullOrEmpty(culture))
Expand Down
13 changes: 10 additions & 3 deletions src/Tasks/CreateVisualBasicManifestResourceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ protected override string CreateManifestName
)
{
string culture = null;
bool treatAsCultureNeutral = false;
if (fileName != null && itemSpecToTaskitem.TryGetValue(fileName, out ITaskItem item))
{
culture = item.GetMetadata("Culture");
// If 'WithCulture' is explicitly set to false, treat as 'culture-neutral' and keep the original name of the resource.
// https://github.com/dotnet/msbuild/issues/3064
treatAsCultureNeutral = item.GetMetadata("WithCulture").Equals("false", StringComparison.OrdinalIgnoreCase);
}

/*
Expand All @@ -57,7 +61,8 @@ protected override string CreateManifestName
dependentUponFileName,
culture,
binaryStream,
Log
Log,
treatAsCultureNeutral
);
}

Expand All @@ -76,6 +81,7 @@ protected override string CreateManifestName
/// <param name="culture">The override culture of this resource, if any</param>
/// <param name="binaryStream">File contents binary stream, may be null</param>
/// <param name="log">Task's TaskLoggingHelper, for logging warnings or errors</param>
/// <param name="treatAsCultureNeutral">Whether to treat the current file as 'culture-neutral' and retain the culture in the name.</param>
/// <returns>Returns the manifest name</returns>
internal static string CreateManifestNameImpl
(
Expand All @@ -86,7 +92,8 @@ internal static string CreateManifestNameImpl
string dependentUponFileName, // May be null
string culture,
Stream binaryStream, // File contents binary stream, may be null
TaskLoggingHelper log
TaskLoggingHelper log,
bool treatAsCultureNeutral = false
)
{
// Use the link file name if there is one, otherwise, fall back to file name.
Expand All @@ -96,7 +103,7 @@ TaskLoggingHelper log
embeddedFileName = fileName;
}

Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName);
Culture.ItemCultureInfo info = Culture.GetItemCultureInfo(embeddedFileName, dependentUponFileName, treatAsCultureNeutral);

// If the item has a culture override, respect that.
if (!string.IsNullOrEmpty(culture))
Expand Down
9 changes: 6 additions & 3 deletions src/Tasks/Culture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ internal struct ItemCultureInfo
internal static ItemCultureInfo GetItemCultureInfo
(
string name,
string dependentUponFilename
string dependentUponFilename,
bool treatAsCultureNeutral = false
)
{
ItemCultureInfo info;
Expand All @@ -38,9 +39,11 @@ string dependentUponFilename

if (String.Equals(Path.GetFileNameWithoutExtension(parentName),
Path.GetFileNameWithoutExtension(name),
StringComparison.OrdinalIgnoreCase))
StringComparison.OrdinalIgnoreCase) ||
treatAsCultureNeutral)
benvillalobos marked this conversation as resolved.
Show resolved Hide resolved
{
// Dependent, but we treat it is as not localized because they have same base filename
// Dependent but we treat it is as not localized because they have same base filename
// Or we want to treat this as a 'culture-neutral' file and retain the culture in the name. https://github.com/dotnet/msbuild/pull/5824
info.cultureNeutralFilename = name;
}
else
Expand Down