Skip to content

Commit ee4b797

Browse files
docs(common): improve localization article
1 parent 49a9ae5 commit ee4b797

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

globalization/localization.md

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ The necessary steps are to:
4040

4141
>note The code snippets below will showcase a sample implementation for a server-side app. For a client-side app, some framework configurations my differ and you cannot use `.resx` files because the framework does not support them.
4242
43-
>tip You can find an example implementation in our offline demos project that you can find your Telerik UI for Blazor installation.
43+
>tip You can find an example implementation in our offline demos project that are available your Telerik UI for Blazor installation (both [msi]({%slug installation/msi%}) and [zip]({%slug installation/zip%})).
44+
45+
>note When following this tutorial to add localization to an existing app, make sure to compare the configuration you are copying so that you do not remove configuration necessary for your app.
4446
4547
>caption Step 1 - Example for enabling localization in the app
4648
@@ -59,12 +61,11 @@ public class Startup
5961
{
6062
// define the list of cultures your app will support
6163
var supportedCultures = new List<CultureInfo>()
62-
{
63-
new CultureInfo("en-US"),
64-
new CultureInfo("de-DE"),
65-
new CultureInfo("es-ES"),
66-
new CultureInfo("bg-BG"),
67-
};
64+
{
65+
new CultureInfo("en-US"),
66+
new CultureInfo("fr-FR"),
67+
new CultureInfo("bg-BG")
68+
};
6869

6970
// set the default culture
7071
options.DefaultRequestCulture = new RequestCulture("en-US");
@@ -73,16 +74,20 @@ public class Startup
7374
options.SupportedUICultures = supportedCultures;
7475
});
7576

77+
// the custom localizer service is registered later, after the Telerik services
78+
7679
#endregion
7780

78-
// there may be other services registered here, this is just an example
7981
services.AddRazorPages();
8082
services.AddServerSideBlazor();
8183

82-
services.AddTelerikBlazor();
8384

84-
// register a custom localizer for the Telerik components
85+
services.AddTelerikBlazor();
86+
// register a custom localizer for the Telerik components, after registering the Telerik services
8587
services.AddSingleton(typeof(ITelerikStringLocalizer), typeof(SampleResxLocalizer));
88+
89+
90+
services.AddSingleton<WeatherForecastService>();
8691
}
8792

8893
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -117,7 +122,6 @@ public class Startup
117122
app.UseEndpoints(endpoints =>
118123
{
119124
// enable controllers for the culture controller
120-
endpoints.MapDefaultControllerRoute();
121125
endpoints.MapControllers();
122126

123127
endpoints.MapBlazorHub();
@@ -127,7 +131,7 @@ public class Startup
127131
}
128132
````
129133

130-
>caption Step 2 -Controller for changing the thread UI culture and redirecting the user (a redirect is required by the framework)
134+
>caption Step 2 - Sample controller for changing the thread UI culture and redirecting the user (a redirect is required by the framework)
131135
132136
````CS
133137
[Route("[controller]/[action]")]
@@ -140,6 +144,9 @@ public class CultureController : Controller
140144
HttpContext.Response.Cookies.Append(
141145
CookieRequestCultureProvider.DefaultCookieName,
142146
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture("en-US", culture)));
147+
// for the time being the thread culture is hardcoded to "en-US"
148+
// until culture-aware number and date formats are implemented
149+
// so here we only change the UICulture of the thread to the new culture
143150
}
144151

145152
return LocalRedirect(redirectUri);
@@ -154,7 +161,7 @@ public class CultureController : Controller
154161
}
155162
````
156163

157-
>caption Use a cookie to store the culture choice of the user - in this example - in `~/Pages/_Host.cshtml`
164+
>caption Step 2 (continued) - Use a cookie to store the culture choice of the user - in this example - in `~/Pages/_Host.cshtml`
158165
159166
````CSHTML
160167
@using Microsoft.AspNetCore.Localization
@@ -177,7 +184,7 @@ public class CultureController : Controller
177184
</body>
178185
````
179186

180-
>caption Sample UI component for changing cultures
187+
>caption Step 3 - Sample UI component for changing cultures
181188
182189
````
183190
@using System.Threading
@@ -193,6 +200,10 @@ public class CultureController : Controller
193200
TextField="@nameof(CultureData.Text)"
194201
ValueField="@nameof(CultureData.Value)">
195202
</TelerikDropDownList>
203+
<br />
204+
Current UI culture (used for localization): @Thread.CurrentThread.CurrentUICulture.Name
205+
<br />
206+
Current thread culture (used for date and number formatting): @Thread.CurrentThread.CurrentCulture.Name
196207
</div>
197208
198209
@code{
@@ -205,8 +216,7 @@ public class CultureController : Controller
205216
public List<CultureData> Cultures { get; set; } = new List<CultureData>()
206217
{
207218
new CultureData() { Text = "English", Value = "en-US" },
208-
new CultureData() { Text = "German", Value = "de-DE" },
209-
new CultureData() { Text = "Spanish", Value = "es-ES" },
219+
new CultureData() { Text = "French", Value = "fr-FR" },
210220
new CultureData() { Text = "Bulgarian", Value = "bg-BG" },
211221
};
212222
@@ -230,14 +240,14 @@ public class CultureController : Controller
230240
}
231241
````
232242

233-
>caption Sample Telerik localization service implementation - this example relies on a `~/Resources` folder with the necessary `.resx` files.
243+
>caption Step 4 - Sample Telerik localization service implementation - this example relies on a `~/Resources` folder with the necessary `.resx` files.
234244
235245
>important You must implement the indexer only. You can obtain the needed strings from any source you prefer and that matches you application, such as database, `resx` files (not supported in client-side projects at the time of writing), `json` files, hash tables, and so on.
236246
237247
````CS
238248
public class SampleResxLocalizer : ITelerikStringLocalizer
239249
{
240-
// this is the indexed you must implement
250+
// this is the indexer you must implement
241251
public string this[string name]
242252
{
243253
get
@@ -246,37 +256,42 @@ public class SampleResxLocalizer : ITelerikStringLocalizer
246256
}
247257
}
248258

249-
// sample implementation - uses .resx files in the ~/Resources folder names TelerikMessages.<culture-locale>.resx
259+
// sample implementation - uses .resx files in the ~/Resources folder named TelerikMessages.<culture-locale>.resx
250260
public string GetStringFromResource(string key)
251261
{
252262
return Resources.TelerikMessages.ResourceManager.GetString(key, Resources.TelerikMessages.Culture); ;
253263
}
254264
}
255265
````
256266

257-
>caption Add `.resx` files to the `~/Resources` folder
267+
>caption Step 4 (continued) - Add `.resx` files to the `~/Resources` folder
258268
259-
In this example the files must be named `~/Resources/TelerikMessages.<culture-locale>.resx`, for example `TelerikMessages.bg-BG.resx`. Make sure to
269+
In this example the files must be named `~/Resources/TelerikMessages.<culture-locale>.resx`, for example `TelerikMessages.bg-BG.resx`. You can use different names (for example, in our demos we use `Messages.resx`). The file names affect the static class that is generated and how you use it in your code (for example, to localize other elements you define yourself, such as grid command buttons or your own buttons).
260270

261-
* mark them as `Embedded Resource`
262-
* add this in your `ProjectName.csproj` file so they are built
271+
It is required that you add the resource file provided in your Telerik UI for Blazor installation that matches the version used in your project. This is the file that contains the current set of localizable strings and whose designer file must be generated by the build.
263272

264-
````XML
265-
<ItemGroup>
266-
<Compile Update="Resources\TelerikMessages.designer.cs">
267-
<DesignTime>True</DesignTime>
268-
<AutoGen>True</AutoGen>
269-
<DependentUpon>TelerikMessages.resx</DependentUpon>
270-
</Compile>
271-
</ItemGroup>
273+
Make sure to:
274+
275+
* Mark the `resx` files as `Embedded Resource` (right click > Properties > Build Action).
276+
* Have the following in your `ProjectName.csproj` file so the designer file is generated (it should be added when you add the main messages file, or when you open and save it. Copy the snippet in case it is not added).
277+
278+
**XML**
279+
280+
<ItemGroup>
281+
<Compile Update="Resources\TelerikMessages.designer.cs">
282+
<DesignTime>True</DesignTime>
283+
<AutoGen>True</AutoGen>
284+
<DependentUpon>TelerikMessages.resx</DependentUpon>
285+
</Compile>
286+
</ItemGroup>
287+
288+
<ItemGroup>
289+
<EmbeddedResource Update="Resources\TelerikMessages.resx">
290+
<Generator>PublicResXFileCodeGenerator</Generator>
291+
<LastGenOutput>TelerikMessages.Designer.cs</LastGenOutput>
292+
</EmbeddedResource>
293+
</ItemGroup>
272294

273-
<ItemGroup>
274-
<EmbeddedResource Update="Resources\TelerikMessages.resx">
275-
<Generator>PublicResXFileCodeGenerator</Generator>
276-
<LastGenOutput>TelerikMessages.Designer.cs</LastGenOutput>
277-
</EmbeddedResource>
278-
</ItemGroup>
279-
````
280295

281296
## See Also
282297

0 commit comments

Comments
 (0)