-
Notifications
You must be signed in to change notification settings - Fork 6
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 #61 from smoogipoo/file-namespace
Add support for adjusting the localisation namespace
- Loading branch information
Showing
22 changed files
with
258 additions
and
89 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
...Resources/CodeFixes/CustomLocalisationNamespace/Fixed/Localisation/Sub/ProgramStrings.txt
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,16 @@ | ||
using osu.Framework.Localisation; | ||
|
||
namespace TestProject.Localisation.Sub | ||
{ | ||
public static class ProgramStrings | ||
{ | ||
private const string prefix = @"TestProject.Localisation.Program"; | ||
|
||
/// <summary> | ||
/// "abc" | ||
/// </summary> | ||
public static LocalisableString Abc => new TranslatableString(getKey(@"abc"), @"abc"); | ||
|
||
private static string getKey(string key) => $@"{prefix}:{key}"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
LocalisationAnalyser.Tests/Resources/CodeFixes/CustomLocalisationNamespace/Fixed/Program.txt
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,12 @@ | ||
using TestProject.Localisation.Sub; | ||
|
||
namespace Test | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string x = ProgramStrings.Abc; | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...ationAnalyser.Tests/Resources/CodeFixes/CustomLocalisationNamespace/Sources/.editorconfig
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,4 @@ | ||
root = true | ||
|
||
[*.cs] | ||
dotnet_diagnostic.OLOC001.localisation_namespace = Localisation.Sub |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
LocalisationAnalyser.Tests/Resources/CodeFixes/CustomPrefixNamespace/Sources/Program.txt
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,10 @@ | ||
namespace Test | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string x = [|"abc"|]; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...r.Tests/Resources/CodeFixes/CustomResourceNamespace/Fixed/Localisation/ProgramStrings.txt
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,16 @@ | ||
using osu.Framework.Localisation; | ||
|
||
namespace TestProject.Localisation | ||
{ | ||
public static class ProgramStrings | ||
{ | ||
private const string prefix = @"CustomNamespace.Program"; | ||
|
||
/// <summary> | ||
/// "abc" | ||
/// </summary> | ||
public static LocalisableString Abc => new TranslatableString(getKey(@"abc"), @"abc"); | ||
|
||
private static string getKey(string key) => $@"{prefix}:{key}"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
LocalisationAnalyser.Tests/Resources/CodeFixes/CustomResourceNamespace/Fixed/Program.txt
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,12 @@ | ||
using TestProject.Localisation; | ||
|
||
namespace Test | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string x = ProgramStrings.Abc; | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
LocalisationAnalyser.Tests/Resources/CodeFixes/CustomResourceNamespace/Sources/.editorconfig
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,4 @@ | ||
root = true | ||
|
||
[*.cs] | ||
dotnet_diagnostic.OLOC001.resource_namespace = CustomNamespace |
10 changes: 10 additions & 0 deletions
10
LocalisationAnalyser.Tests/Resources/CodeFixes/CustomResourceNamespace/Sources/Program.txt
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,10 @@ | ||
namespace Test | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string x = [|"abc"|]; | ||
} | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
LocalisationAnalyser/Extensions/AnalyzerConfigOptionsExtensions.cs
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,82 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace LocalisationAnalyser.Extensions | ||
{ | ||
public static class AnalyzerConfigOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Retrieves the namespace under which the localisation strings are expected to be placed. | ||
/// </summary> | ||
/// <param name="config">The config that may provide a custom value via <c>localisation_namespace</c>.</param> | ||
/// <param name="fallback">The fallback value in case no custom value is provided.</param> | ||
/// <returns>The localisation strings namespace - either a custom value from <paramref name="config"/>, or the given <paramref name="fallback"/>.</returns> | ||
/// <exception cref="InvalidOperationException">If the config contained an empty string as a custom value.</exception> | ||
public static string GetLocalisationNamespace(this AnalyzerConfigOptions? config, string fallback) | ||
{ | ||
if (config == null) | ||
return fallback; | ||
|
||
if (config.TryGetValue($"dotnet_diagnostic.{DiagnosticRules.STRING_CAN_BE_LOCALISED.Id}.localisation_namespace", out string? customFileNamespace)) | ||
{ | ||
if (string.IsNullOrEmpty(customFileNamespace)) | ||
throw new InvalidOperationException("Custom localisation namespace cannot be empty."); | ||
|
||
return customFileNamespace; | ||
} | ||
|
||
return fallback; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the namespace under which the localisation resources are expected to be found. | ||
/// </summary> | ||
/// <param name="config">The config that may provide a custom value via either <c>resource_namespace</c> or <c>prefix_namespace</c>.</param> | ||
/// <param name="fallback">The fallback value in case no custom value is provided.</param> | ||
/// <returns>The localisation resource namespace - either a custom value from <paramref name="config"/>, or the given <paramref name="fallback"/>.</returns> | ||
/// <exception cref="InvalidOperationException">If the config contained an empty string as a custom value.</exception> | ||
public static string GetResourceNamespace(this AnalyzerConfigOptions? config, string fallback) | ||
{ | ||
if (config == null) | ||
return fallback; | ||
|
||
if (config.TryGetValue($"dotnet_diagnostic.{DiagnosticRules.STRING_CAN_BE_LOCALISED.Id}.resource_namespace", out string? customResourceNamespace)) | ||
{ | ||
if (string.IsNullOrEmpty(customResourceNamespace)) | ||
throw new InvalidOperationException("Custom resource namespace cannot be empty."); | ||
|
||
return customResourceNamespace; | ||
} | ||
|
||
if (config.TryGetValue($"dotnet_diagnostic.{DiagnosticRules.STRING_CAN_BE_LOCALISED.Id}.prefix_namespace", out string? customPrefixNamespace)) | ||
{ | ||
if (string.IsNullOrEmpty(customPrefixNamespace)) | ||
throw new InvalidOperationException("Custom prefix namespace cannot be empty."); | ||
|
||
return customPrefixNamespace; | ||
} | ||
|
||
return fallback; | ||
} | ||
|
||
public static string[]? GetLicenseHeader(this AnalyzerConfigOptions? config) | ||
{ | ||
if (config == null) | ||
return null; | ||
|
||
if (!config.TryGetValue($"dotnet_diagnostic.{DiagnosticRules.STRING_CAN_BE_LOCALISED.Id}.license_header", out string licenseHeader)) | ||
return null; | ||
|
||
if (string.IsNullOrEmpty(licenseHeader)) | ||
return null; | ||
|
||
if (!licenseHeader.StartsWith("//", StringComparison.Ordinal)) | ||
licenseHeader = $"// {licenseHeader}"; | ||
|
||
return licenseHeader.Split(new[] { "\\n" }, StringSplitOptions.None); | ||
} | ||
} | ||
} |
Oops, something went wrong.