Skip to content

Commit 697c636

Browse files
committed
Fix (kinda) yaml
1 parent 2df2cfe commit 697c636

31 files changed

+789
-69
lines changed

Lepo.i18n.sln

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lepo.i18n.DependencyInjecti
2828
EndProject
2929
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lepo.i18n.DependencyInjection.UnitTests", "tests\Lepo.i18n.DependencyInjection.UnitTests\Lepo.i18n.DependencyInjection.UnitTests.csproj", "{274021AA-F5A4-4A02-B8B7-224B254031A1}"
3030
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lepo.i18n.Wpf.UnitTests", "tests\Lepo.i18n.Wpf.UnitTests\Lepo.i18n.Wpf.UnitTests.csproj", "{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}"
32+
EndProject
3133
Global
3234
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3335
Debug|Any CPU = Debug|Any CPU
@@ -58,13 +60,18 @@ Global
5860
{274021AA-F5A4-4A02-B8B7-224B254031A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{274021AA-F5A4-4A02-B8B7-224B254031A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{274021AA-F5A4-4A02-B8B7-224B254031A1}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(SolutionProperties) = preSolution
6369
HideSolutionNode = FALSE
6470
EndGlobalSection
6571
GlobalSection(NestedProjects) = preSolution
6672
{98789E33-F370-46FF-B4B6-CEAA81E9C2C3} = {AA93DD64-B88F-46ED-9981-EABA0F3C3E95}
6773
{274021AA-F5A4-4A02-B8B7-224B254031A1} = {AA93DD64-B88F-46ED-9981-EABA0F3C3E95}
74+
{ED677E81-8D0F-42B2-A001-D37FEB3AAEA2} = {AA93DD64-B88F-46ED-9981-EABA0F3C3E95}
6875
EndGlobalSection
6976
GlobalSection(ExtensibilityGlobals) = postSolution
7077
SolutionGuid = {BF9C3C0B-D0EC-4FA3-A0D5-A8F70572737A}

src/Lepo.i18n.DependencyInjection/GlobalUsings.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
44
// All Rights Reserved.
55

6-
global using Microsoft.Extensions.DependencyInjection;
7-
global using Microsoft.Extensions.Localization;
86
global using System;
97
global using System.Collections.Generic;
108
global using System.Globalization;
119
global using System.Linq;
10+
global using Microsoft.Extensions.DependencyInjection;
11+
global using Microsoft.Extensions.Localization;

src/Lepo.i18n.DependencyInjection/ServiceCollectionExtensions.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace Lepo.i18n;
1010
/// </summary>
1111
public static class ServiceCollectionExtensions
1212
{
13+
/// <summary>
14+
/// Adds the string localizer and related services to the specified <see cref="IServiceCollection"/>.
15+
/// </summary>
16+
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
17+
/// <param name="configure">A delegate to configure the localization builder.</param>
18+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
1319
public static IServiceCollection AddStringLocalizer(
1420
this IServiceCollection services,
1521
Action<LocalizationBuilder> configure
@@ -19,7 +25,8 @@ Action<LocalizationBuilder> configure
1925

2026
configure(builder);
2127

22-
LocalizationProvider localizer = new(CultureInfo.CurrentCulture, builder.GetLocalizations());
28+
LocalizationProvider localizer =
29+
new(CultureInfo.CurrentCulture, builder.GetLocalizations());
2330
LocalizationProvider.SetInstance(localizer);
2431

2532
_ = services.AddSingleton<ILocalizationProvider>(

src/Lepo.i18n.DependencyInjection/StringLocalizer.cs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Lepo.i18n;
77

8+
/// <summary>
9+
/// Provides functionality to localize strings.
10+
/// </summary>
811
public class StringLocalizer(
912
ILocalizationProvider localizations,
1013
ILocalizationCultureManager cultureManager

src/Lepo.i18n.Wpf/ApplicationExtensions.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55

66
namespace Lepo.i18n.Wpf;
77

8+
/// <summary>
9+
/// Provides extension methods for the <see cref="Application"/> class.
10+
/// </summary>
811
public static class ApplicationExtensions
912
{
13+
/// <summary>
14+
/// Configures the application to use a string localizer.
15+
/// </summary>
16+
/// <param name="app">The application to configure.</param>
17+
/// <param name="configure">A delegate to configure the localization builder.</param>
18+
/// <returns>The configured application.</returns>
1019
public static Application UseStringLocalizer(
1120
this Application app,
1221
Action<LocalizationBuilder> configure
@@ -16,9 +25,24 @@ Action<LocalizationBuilder> configure
1625

1726
configure(builder);
1827

19-
LocalizationProvider localizer = new(CultureInfo.CurrentUICulture, builder.GetLocalizations());
28+
LocalizationProvider localizer =
29+
new(CultureInfo.CurrentUICulture, builder.GetLocalizations());
30+
2031
LocalizationProvider.SetInstance(localizer);
2132

2233
return app;
2334
}
35+
36+
/// <summary>
37+
/// Sets the culture for localization in the application.
38+
/// </summary>
39+
/// <param name="app">The application to set the culture for.</param>
40+
/// <param name="culture">The culture to set.</param>
41+
/// <returns>The application with the set culture.</returns>
42+
public static Application SetLocalizationCulture(this Application app, CultureInfo culture)
43+
{
44+
LocalizationProvider.GetInstance()?.SetCulture(culture);
45+
46+
return app;
47+
}
2448
}

src/Lepo.i18n.Wpf/Lepo.i18n.Wpf.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<IsTrimmable>true</IsTrimmable>
88
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
9-
<UseWpf>true</UseWpf>
9+
<UseWPF>true</UseWPF>
1010
</PropertyGroup>
1111

1212
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">

src/Lepo.i18n.Wpf/StringLocalizerExtension.cs

+45-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
44
// All Rights Reserved.
55

6+
using System.Linq;
7+
68
namespace Lepo.i18n.Wpf;
79

810
/// <summary>
@@ -20,6 +22,11 @@ public class StringLocalizerExtension : MarkupExtension
2022
/// </summary>
2123
public string? Text { get; set; }
2224

25+
/// <summary>
26+
/// Gets or sets the namespace of the text to be localized.
27+
/// </summary>
28+
public string? Namespace { get; set; }
29+
2330
/// <summary>
2431
/// Initializes a new instance of the <see cref="StringLocalizerExtension"/> class.
2532
/// </summary>
@@ -31,7 +38,18 @@ public StringLocalizerExtension() { }
3138
/// <param name="text">The text to be localized.</param>
3239
public StringLocalizerExtension(string text)
3340
{
34-
Text = text;
41+
Text = EscapeText(text);
42+
}
43+
44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="StringLocalizerExtension"/> class with the specified text and namespace.
46+
/// </summary>
47+
/// <param name="text">The text to be localized.</param>
48+
/// <param name="textNamespace">The namespace of the text to be localized.</param>
49+
public StringLocalizerExtension(string text, string textNamespace)
50+
{
51+
Text = EscapeText(text);
52+
Namespace = textNamespace;
3553
}
3654

3755
/// <summary>
@@ -46,8 +64,32 @@ public StringLocalizerExtension(string text)
4664
return string.Empty;
4765
}
4866

49-
//return LocalizationProvider.GetInstance()?[EscapeText(Text)] ?? EscapeText(Text);
50-
return string.Empty;
67+
LocalizationSet? localizationSet;
68+
69+
if (Namespace is null)
70+
{
71+
localizationSet = LocalizationProvider
72+
.GetInstance()
73+
?.Get(
74+
LocalizationProvider.GetInstance()?.GetCulture() ?? CultureInfo.CurrentUICulture
75+
);
76+
}
77+
else
78+
{
79+
localizationSet = LocalizationProvider
80+
.GetInstance()
81+
?.Get(
82+
Namespace,
83+
LocalizationProvider.GetInstance()?.GetCulture() ?? CultureInfo.CurrentUICulture
84+
);
85+
}
86+
87+
if (localizationSet is null)
88+
{
89+
return Text;
90+
}
91+
92+
return localizationSet.Strings.FirstOrDefault(s => s.Key == Text).Value ?? Text;
5193
}
5294

5395
/// <summary>

src/Lepo.i18n/GlobalUsings.cs

-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010
global using System.Linq;
1111
global using System.Reflection;
1212
global using System.Resources;
13-
global using System.Security.Cryptography;
14-
global using System.Text;

src/Lepo.i18n/ILocalizationCultureManager.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
namespace Lepo.i18n;
77

88
/// <summary>
9-
/// Manages localization settings.
9+
/// Defines methods for managing localization settings.
1010
/// </summary>
1111
public interface ILocalizationCultureManager
1212
{
13+
/// <summary>
14+
/// Sets the current culture for localization.
15+
/// </summary>
16+
/// <param name="culture">The culture to set.</param>
1317
void SetCulture(CultureInfo culture);
1418

19+
/// <summary>
20+
/// Gets the current culture used for localization.
21+
/// </summary>
22+
/// <returns>The current culture.</returns>
1523
CultureInfo GetCulture();
1624
}

src/Lepo.i18n/LocalizationBuilder.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@
66
namespace Lepo.i18n;
77

88
/// <summary>
9-
/// Builds a dictionary of localized strings for different cultures.
9+
/// Provides functionality to build a collection of localized strings for different cultures.
1010
/// </summary>
1111
public class LocalizationBuilder
1212
{
1313
private readonly HashSet<LocalizationSet> localizations = new HashSet<LocalizationSet>();
1414

1515
/// <summary>
16-
/// Gets the dictionary of localized strings for different cultures.
16+
/// Gets the collection of localized strings for different cultures.
1717
/// </summary>
18-
/// <returns>The dictionary of localized strings.</returns>
18+
/// <returns>The collection of localized strings.</returns>
1919
public IEnumerable<LocalizationSet> GetLocalizations()
2020
{
2121
return localizations;
2222
}
2323

24+
/// <summary>
25+
/// Adds a localization set to the collection.
26+
/// </summary>
27+
/// <param name="localization">The localization set to add.</param>
28+
/// <exception cref="InvalidOperationException">Thrown when a localization set for the same culture already exists in the collection.</exception>
2429
public void AddLocalization(LocalizationSet localization)
2530
{
2631
if (localizations.Any(x => x.Culture == localization.Culture))

src/Lepo.i18n/LocalizationBuilderException.cs

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
namespace Lepo.i18n;
77

8+
/// <summary>
9+
/// Represents errors that occur during the execution of the localization builder.
10+
/// </summary>
811
public class LocalizationBuilderException(string message, Exception innerException)
912
: Exception(message, innerException);

src/Lepo.i18n/LocalizationBuilderExtensions.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,41 @@ public static class LocalizationBuilderExtensions
2020
public static LocalizationBuilder AddLocalization(
2121
this LocalizationBuilder builder,
2222
CultureInfo culture,
23-
IDictionary<string, string?> localizations
23+
IEnumerable<KeyValuePair<string, string?>> localizations
2424
)
2525
{
2626
builder.AddLocalization(new LocalizationSet(default, culture, localizations));
2727

2828
return builder;
2929
}
3030

31+
/// <summary>
32+
/// Adds localized strings from a resource in the calling assembly to the <see cref="LocalizationBuilder"/>.
33+
/// </summary>
34+
/// <typeparam name="TResource">The type of the resource.</typeparam>
35+
/// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localized strings to.</param>
36+
/// <param name="culture">The culture for which the localized strings are provided.</param>
37+
/// <returns>The <see cref="LocalizationBuilder"/> with the added localized strings.</returns>
3138
public static LocalizationBuilder FromResource<TResource>(
3239
this LocalizationBuilder builder,
3340
CultureInfo culture
3441
)
3542
{
36-
return builder.FromResource<TResource>(culture, Assembly.GetCallingAssembly());
43+
return builder.FromResource<TResource>(Assembly.GetCallingAssembly(), culture);
3744
}
3845

46+
/// <summary>
47+
/// Adds localized strings from a resource in the specified assembly to the <see cref="LocalizationBuilder"/>.
48+
/// </summary>
49+
/// <typeparam name="TResource">The type of the resource.</typeparam>
50+
/// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localized strings to.</param>
51+
/// <param name="assembly">The assembly that contains the resource.</param>
52+
/// <param name="culture">The culture for which the localized strings are provided.</param>
53+
/// <returns>The <see cref="LocalizationBuilder"/> with the added localized strings.</returns>
3954
public static LocalizationBuilder FromResource<TResource>(
4055
this LocalizationBuilder builder,
41-
CultureInfo culture,
42-
Assembly assembly
56+
Assembly assembly,
57+
CultureInfo culture
4358
)
4459
{
4560
string? resourceName = typeof(TResource).FullName;
@@ -52,6 +67,15 @@ Assembly assembly
5267
return builder.FromResource(assembly, resourceName, culture);
5368
}
5469

70+
/// <summary>
71+
/// Adds localized strings from a resource with the specified base name in the specified assembly to the <see cref="LocalizationBuilder"/>.
72+
/// </summary>
73+
/// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localized strings to.</param>
74+
/// <param name="assembly">The assembly that contains the resource.</param>
75+
/// <param name="baseName">The base name of the resource.</param>
76+
/// <param name="culture">The culture for which the localized strings are provided.</param>
77+
/// <returns>The <see cref="LocalizationBuilder"/> with the added localized strings.</returns>
78+
/// <exception cref="LocalizationBuilderException">Thrown when the resource cannot be found.</exception>
5579
public static LocalizationBuilder FromResource(
5680
this LocalizationBuilder builder,
5781
Assembly assembly,

src/Lepo.i18n/LocalizationCultureManager.cs

+4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
namespace Lepo.i18n;
77

8+
/// <summary>
9+
/// Provides functionality to manage the current culture for localization.
10+
/// </summary>
811
public class LocalizationCultureManager : ILocalizationCultureManager
912
{
13+
/// <inheritdoc />
1014
public CultureInfo GetCulture()
1115
{
1216
return LocalizationProvider.GetInstance()?.GetCulture() ?? CultureInfo.CurrentCulture;

0 commit comments

Comments
 (0)