Skip to content

Commit 0285364

Browse files
tesar-techstsrki
andauthored
Localization: Add culture-specific overloads for text localization APIs (#6087)
* Add culture-specific overloads for text localization APIs. GetString, GetStrings * Formating * Add SelectedCulture property to ITextLocalizer interface and implementation. * Formating * release notes --------- Co-authored-by: Mladen Macanovic <mladen.macanovic@blazorise.com> Co-authored-by: Mladen Macanovic <mladen.macanovic@gmail.com>
1 parent 3a959c6 commit 0285364

3 files changed

Lines changed: 69 additions & 9 deletions

File tree

Documentation/Blazorise.Docs/Pages/News/2025-03-10-release-notes-180.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,12 @@
178178
The <Code>MessageService</Code> now supports the <Code>Choices</Code> method, allowing you to display a list of options in a message dialog. This feature is useful for scenarios where you want to present users with multiple choices and handle their selection accordingly.
179179
</Paragraph>
180180

181+
<Heading Size="HeadingSize.Is3">
182+
Culture-specific overloads for text localization
183+
</Heading>
184+
185+
<Paragraph>
186+
The <Code>ITextLocalizer</Code> interface now includes overloads for the <Code>GetString</Code> method that accept a <Code>CultureInfo</Code> parameter. The <Code>CultureInfo</Code> parameter can be used to specify the desired culture for localization, enabling you to retrieve strings that are tailored to the user's language and region. This is particularly useful in scenarios where you need to support multiple languages or regions within your application.
187+
</Paragraph>
188+
181189
<NewsPagePostInfo UserName="Mladen Macanović" ImageName="mladen" PostedOn="March 10th, 2025" Read="7 min" />

Source/Blazorise/Localization/ITextLocalizer.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#region Using directives
22
using System.Collections.Generic;
3+
using System.Globalization;
34
#endregion
45

56
namespace Blazorise.Localization;
@@ -24,6 +25,15 @@ public interface ITextLocalizer
2425
/// <returns>The formatted string resource <paramref name="name"/> if not found.</returns>
2526
string this[string name, params object[] arguments] { get; }
2627

28+
/// <summary>
29+
/// Gets the string resource with the given name and formatted with the supplied arguments using specified culture.
30+
/// </summary>
31+
/// <param name="culture">The culture to use for localization.</param>
32+
/// <param name="name">The name of the string resource.</param>
33+
/// <param name="arguments">The values to format the string with.</param>
34+
/// <returns>The formatted string resource <paramref name="name"/> if not found.</returns>
35+
string this[CultureInfo culture, string name, params object[] arguments] { get; }
36+
2737
/// <summary>
2838
/// Adds a custom language resource to the list of supported cultures.
2939
/// </summary>
@@ -38,12 +48,34 @@ public interface ITextLocalizer
3848
/// <returns>Localized string.</returns>
3949
string GetString( string name, params object[] arguments );
4050

51+
/// <summary>
52+
/// Gets the localized string by the name with the optional list object for formatting.
53+
/// </summary>
54+
/// <param name="culture">The culture to use for localization.</param>
55+
/// <param name="name">A name to localize.</param>
56+
/// <param name="arguments">An object array that contains zero or more objects to format.</param>
57+
/// <returns>Localized string.</returns>
58+
string GetString( CultureInfo culture, string name, params object[] arguments );
59+
4160
/// <summary>
4261
/// Gets the localized string for each key in the localization object.
4362
/// </summary>
4463
/// <param name="arguments">An object array that contains zero or more objects to format.</param>
4564
/// <returns>Localized key/value pairs.</returns>
4665
IReadOnlyDictionary<string, string> GetStrings( params object[] arguments );
66+
67+
/// <summary>
68+
/// Gets the localized string for each key in the localization object.
69+
/// </summary>
70+
/// <param name="culture">The culture to use for localization.</param>
71+
/// <param name="arguments">An object array that contains zero or more objects to format.</param>
72+
/// <returns>Localized key/value pairs.</returns>
73+
IReadOnlyDictionary<string, string> GetStrings( CultureInfo culture, params object[] arguments );
74+
75+
/// <summary>
76+
/// Gets the current culture info.
77+
/// </summary>
78+
CultureInfo SelectedCulture { get; }
4779
}
4880

4981
/// <summary>

Source/Blazorise/Localization/TextLocalizer.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ protected virtual string[] GetLocalizationResourceNames( Assembly assembly, stri
134134
/// <summary>
135135
/// Gets all of the translations.
136136
/// </summary>
137+
/// <param name="culture"></param>
137138
/// <returns>Key/value pairs of translations.</returns>
138-
protected virtual IReadOnlyDictionary<string, string> GetTranslations()
139+
protected virtual IReadOnlyDictionary<string, string> GetTranslations( CultureInfo culture )
139140
{
140141
// The selected culture can either be a neutral culture (2-digit:"cn") or a specific culture
141142
// (5-digit:"en-UK").
@@ -149,12 +150,12 @@ protected virtual IReadOnlyDictionary<string, string> GetTranslations()
149150
// 5. Invariant culture (defaults to "en")
150151
IReadOnlyDictionary<string, string> result;
151152

152-
if ( localizerService.SelectedCulture is not null
153-
&& translationsByCulture.TryGetValue( localizerService.SelectedCulture.Name, out result ) )
153+
if ( culture is not null
154+
&& translationsByCulture.TryGetValue( culture.Name, out result ) )
154155
return result;
155156

156-
if ( localizerService.SelectedCulture?.Parent is not null && !localizerService.SelectedCulture.IsNeutralCulture
157-
&& translationsByCulture.TryGetValue( localizerService.SelectedCulture.Parent.Name, out result ) )
157+
if ( culture?.Parent is not null && !culture.IsNeutralCulture
158+
&& translationsByCulture.TryGetValue( culture.Parent.Name, out result ) )
158159
return result;
159160

160161
if ( CultureInfo.CurrentUICulture is not null
@@ -174,28 +175,41 @@ protected virtual IReadOnlyDictionary<string, string> GetTranslations()
174175
/// <inheritdoc/>
175176
public virtual string GetString( string name, params object[] arguments )
176177
{
177-
var translations = GetTranslations();
178+
return GetString( localizerService.SelectedCulture, name, arguments );
179+
}
180+
181+
182+
/// <inheritdoc/>
183+
public virtual string GetString( CultureInfo culture, string name, params object[] arguments )
184+
{
185+
var translations = GetTranslations( culture );
178186

179187
if ( translations is null || !translations.TryGetValue( name, out var value ) )
180188
value = name;
181189

182190
if ( arguments.Length > 0 )
183-
value = string.Format( localizerService.SelectedCulture, value, arguments );
191+
value = string.Format( culture, value, arguments );
184192

185193
return value;
186194
}
187195

188196
/// <inheritdoc/>
189197
public virtual IReadOnlyDictionary<string, string> GetStrings( params object[] arguments )
190198
{
191-
var translations = GetTranslations();
199+
return GetStrings( localizerService.SelectedCulture, arguments );
200+
}
201+
202+
/// <inheritdoc/>
203+
public virtual IReadOnlyDictionary<string, string> GetStrings( CultureInfo culture, params object[] arguments )
204+
{
205+
var translations = GetTranslations( culture );
192206

193207
return ( from t in translations
194208
select new
195209
{
196210
t.Key,
197211
Value = arguments.Length > 0
198-
? string.Format( localizerService.SelectedCulture, t.Value, arguments )
212+
? string.Format( culture, t.Value, arguments )
199213
: t.Value
200214
} ).ToDictionary( x => x.Key, x => x.Value );
201215
}
@@ -210,5 +224,11 @@ public virtual IReadOnlyDictionary<string, string> GetStrings( params object[] a
210224
/// <inheritdoc/>
211225
public string this[string name, params object[] arguments] => GetString( name, arguments );
212226

227+
/// <inheritdoc/>
228+
public string this[CultureInfo culture, string name, params object[] arguments] => GetString( culture, name, arguments );
229+
230+
/// <inheritdoc />
231+
public CultureInfo SelectedCulture => localizerService.SelectedCulture;
232+
213233
#endregion
214234
}

0 commit comments

Comments
 (0)