Skip to content

Commit caed58a

Browse files
committed
Quality of life updates
1 parent 9984bbf commit caed58a

11 files changed

+96
-39
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Will generate:
1616
- Folder1.FolderViewModelLocalized.g.cs (RootNamespace.Folder1.FolderViewModelLocalized)
1717
- ServiceCollections.g.cs
1818

19+
1920
## To use this:
2021
1. Install Microsoft.Extensions.Localization
2122
2. Install Shiny.Extensions.Localization.Generator
@@ -24,22 +25,24 @@ Will generate:
2425
builder.Services.AddLocalization();
2526
builder.Services.AddStronglyTypedLocalizations();
2627
```
27-
4. Now inject the strongly typed classes
28+
4. Now add an `.resx` file to your project that matches the name of class (ie. ViewModel, Controller, Service, etc).
29+
5. Now inject the strongly typed classes
2830
```csharp
2931
public class MyViewModel
3032
{
31-
public MyViewModel(MyViewModelLocalized localizer)
33+
public MyViewModel(MyViewModelLocalized localizer) // same namespace and class name with "Localized" suffix
3234
=> this.Localizer = localizer;
3335

3436
public MyViewModelLocalized Localizer { get; }
3537
}
3638
```
37-
5. Now bind (xaml intellisense will pick it up)
39+
6.Now bind (xaml intellisense will pick it up)
3840
```xml
3941
<Label Text="{Binding Localizer.MyKey}" />
4042
```
4143

42-
WARNING: If the "class" beside the resource does not exist, a compile error will occur with the generated code
44+
> [!WARNING]
45+
> If the "class" beside the resource does not exist, a compile error will occur with the generated code
4346
4447
## Generate Classes with Internal Accessor
4548

Sample/Program.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
23
using Sample;
34

4-
//var services = new ServiceCollection();
5-
//services.AddLocalization();
6-
//services.AddStronglyTypedLocalizations();
7-
//services.AddSingleton<SampleClass>();
5+
var builder = Host.CreateApplicationBuilder();
6+
builder.Services.AddStronglyTypedLocalizations();
7+
var app = builder.Build();
88

9-
Console.WriteLine("Hello, World!");
9+
var localizer = app.Services.GetRequiredService<SampleClassLocalized>();
10+
Console.WriteLine(localizer.First);

Sample/Sample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
1415
<PackageReference Include="Microsoft.Extensions.Localization" Version="9.0.6" />
1516
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
1617
</ItemGroup>

Shiny.Extensions.Localization.Generator.Tests/LocalizationSourceGeneratorTests.EndToEndTest_rootNamespace=MyTest_projectName=MyTest.Core_generateInternal=False#ServiceCollectionExtensions.g.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace MyTest;
88

99
public static class ServiceCollectionExtensions_Generated
1010
{
11-
public static void AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
11+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
1212
{
13+
services.AddLocalization();
1314
services.AddSingleton<global::MyTest.StringsLocalized>();
15+
return services;
1416
}
1517
}

Shiny.Extensions.Localization.Generator.Tests/LocalizationSourceGeneratorTests.EndToEndTest_rootNamespace=null_projectName=MyTest.Library_generateInternal=False#ServiceCollectionExtensions.g.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace MyTest.Library;
88

99
public static class ServiceCollectionExtensions_Generated
1010
{
11-
public static void AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
11+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
1212
{
13+
services.AddLocalization();
1314
services.AddSingleton<global::MyTest.Library.StringsLocalized>();
15+
return services;
1416
}
1517
}

Shiny.Extensions.Localization.Generator.Tests/LocalizationSourceGeneratorTests.EndToEndTest_rootNamespace=null_projectName=MyTest.Library_generateInternal=True#ServiceCollectionExtensions.g.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace MyTest.Library;
88

99
internal static class ServiceCollectionExtensions_Generated
1010
{
11-
public static void AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
11+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
1212
{
13+
services.AddLocalization();
1314
services.AddSingleton<global::MyTest.Library.StringsLocalized>();
15+
return services;
1416
}
1517
}

Shiny.Extensions.Localization.Generator.Tests/LocalizationSourceGeneratorTests.EndToEndTest_rootNamespace=null_projectName=MyTest_generateInternal=False#ServiceCollectionExtensions.g.verified.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace MyTest;
88

99
public static class ServiceCollectionExtensions_Generated
1010
{
11-
public static void AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
11+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
1212
{
13+
services.AddLocalization();
1314
services.AddSingleton<global::MyTest.StringsLocalized>();
15+
return services;
1416
}
1517
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//HintName: ServiceCollectionExtensions.g.cs
2+
// <auto-generated />
3+
// This file is auto-generated by Shiny.Extensions.Localization.Generator
4+
// Do not edit this file directly, instead edit the .resx files in your project.
5+
using global::Microsoft.Extensions.DependencyInjection;
6+
7+
public static class ServiceCollectionExtensions_Generated
8+
{
9+
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddStronglyTypedLocalizations(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
10+
{
11+
services.AddLocalization();
12+
return services;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Message: No .resx files were found to process.,
5+
Severity: Warning,
6+
WarningLevel: 1,
7+
Descriptor: {
8+
Id: SHINY0000,
9+
Title: No Files found,
10+
MessageFormat: No .resx files were found to process.,
11+
Category: Shiny.Localization,
12+
DefaultSeverity: Warning,
13+
IsEnabledByDefault: true
14+
}
15+
}
16+
]
17+
}

Shiny.Extensions.Localization.Generator.Tests/LocalizationSourceGeneratorTests.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,27 @@ await Verify(results)
4949
generateInternal
5050
);
5151
}
52+
53+
[Fact]
54+
public Task NoResourcesAddEmptyExtensionMethod()
55+
{
56+
var compilation = CSharpCompilation.Create(
57+
assemblyName: "Tests"
58+
);
59+
var generator = new LocalizationSourceGenerator().AsSourceGenerator();
60+
var options = new TestAnalyzerConfigOptionsProvider();
61+
options.Options.Add("build_property.MSBuildProjectFullPath", "Shiny.Extensions.Localization.Generator");
62+
options.Options.Add("build_property.MSBuildProjectName", "MyTest.Core");
5263

53-
//[Fact]
54-
//public void EndToEndTest()
55-
//{
56-
// var services = new ServiceCollection();
57-
// services.AddLocalization();
58-
// services.AddStrongTypedLocalizations();
59-
// var sp = services.BuildServiceProvider();
64+
GeneratorDriver driver = CSharpGeneratorDriver.Create(
65+
[generator],
66+
optionsProvider: options
67+
);
6068

61-
// sp.GetRequiredService<MyClassLocalized>().Should().NotBeNull("MyClass localization missing in registration");
62-
// sp.GetRequiredService<FolderTest1Localized>().Should().NotBeNull("FolderTest1 localization missing in registration");
63-
// sp.GetRequiredService<FolderTest2Localized>().Should().NotBeNull("FolderTest2 localization missing in registration");
69+
driver = driver.RunGenerators(compilation);
70+
var results = driver.GetRunResult();
6471

65-
// // TODO: ensure keys are set
66-
// // TODO: check keys with spaces
67-
//}
72+
return Verify(results);
73+
}
6874
}
6975

0 commit comments

Comments
 (0)