Skip to content

Commit 0c0600f

Browse files
authored
Add Localization Samples for .NET 8 (#34199)
* Add Localization & PO Localization samples for .NET 8 * Update markdown files * Remove unnecessary files
1 parent 393d676 commit 0c0600f

File tree

137 files changed

+61813
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+61813
-12
lines changed

aspnetcore/fundamentals/localization/make-content-localizable.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ One task for localizing an app is to wrap localizable content with code that fac
2323

2424
The following code example shows how to wrap the string "About Title" for localization.
2525

26-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/Controllers/AboutController.cs)]
26+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/Controllers/AboutController.cs)]
2727

2828
In the preceding code, the `IStringLocalizer<T>` implementation comes from [Dependency Injection](~/fundamentals/dependency-injection.md). If the localized value of "About Title" isn't found, then the indexer key is returned, that is, the string "About Title".
2929

@@ -35,33 +35,33 @@ Alternatively, you can use the traditional approach and provide a key to retriev
3535

3636
Use the <xref:Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer%601> implementation for resources that contain HTML. <xref:Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer> HTML-encodes arguments that are formatted in the resource string, but doesn't HTML-encode the resource string itself. In the following highlighted code, only the value of the `name` parameter is HTML-encoded.
3737

38-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/Controllers/BookController.cs?highlight=3,5,20&start=1&end=24)]
38+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/Controllers/BookController.cs?highlight=3,5,20&start=1&end=24)]
3939

4040
***NOTE:*** Generally, only localize text, not HTML.
4141

4242
## `IStringLocalizerFactory`
4343

4444
At the lowest level, <xref:Microsoft.Extensions.Localization.IStringLocalizerFactory> can be retrieved from of [Dependency Injection](~/fundamentals/dependency-injection.md):
4545

46-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/Controllers/TestController.cs?highlight=6-12&name=snippet_1)]
46+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/Controllers/TestController.cs?highlight=6-12&name=snippet_1)]
4747

4848
The preceding code demonstrates each of the two factory create methods.
4949

5050
## Shared resources
5151

5252
You can partition your localized strings by controller or area, or have just one container. In the sample app, a marker class named `SharedResource` is used for shared resources. The marker class is never called:
5353

54-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/SharedResource.cs)]
54+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/SharedResource.cs)]
5555

5656
In the following sample, the `InfoController` and the `SharedResource` localizers are used:
5757

58-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/Controllers/InfoController.cs?name=snippet_1)]
58+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/Controllers/InfoController.cs?name=snippet_1)]
5959

6060
## View localization
6161

6262
The <xref:Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer> service provides localized strings for a [view](xref:mvc/views/overview). The `ViewLocalizer` class implements this interface and finds the resource location from the view file path. The following code shows how to use the default implementation of `IViewLocalizer`:
6363

64-
[!code-cshtml[](~/fundamentals/localization/sample/6.x/Localization/Views/Home/About.cshtml)]
64+
[!code-cshtml[](~/fundamentals/localization/sample/8.x/Localization/Views/Home/About.cshtml)]
6565

6666
The default implementation of `IViewLocalizer` finds the resource file based on the view's file name. There's no option to use a global shared resource file. `ViewLocalizer` implements the localizer using `IHtmlLocalizer`, so Razor doesn't HTML-encode the localized string. You can parameterize resource strings, and `IViewLocalizer` HTML-encodes the parameters but not the resource string. Consider the following Razor markup:
6767

@@ -81,7 +81,7 @@ Generally, ***only localize text***, not HTML.
8181

8282
To use a shared resource file in a view, inject `IHtmlLocalizer<T>`:
8383

84-
[!code-cshtml[](~/fundamentals/localization/sample/6.x/Localization/Views/Test/About.cshtml?highlight=5,12)]
84+
[!code-cshtml[](~/fundamentals/localization/sample/8.x/Localization/Views/Test/About.cshtml?highlight=5,12)]
8585

8686
## DataAnnotations localization
8787

@@ -90,7 +90,7 @@ DataAnnotations error messages are localized with `IStringLocalizer<T>`. Using t
9090
* *Resources/ViewModels.Account.RegisterViewModel.fr.resx*
9191
* *Resources/ViewModels/Account/RegisterViewModel.fr.resx*
9292

93-
[!code-csharp[](~/fundamentals/localization/sample/6.x/Localization/ViewModels/Account/RegisterViewModel.cs)]
93+
[!code-csharp[](~/fundamentals/localization/sample/8.x/Localization/ViewModels/Account/RegisterViewModel.cs)]
9494

9595
Non-validation attributes are localized.
9696

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Localization;
3+
4+
namespace Localization.Controllers;
5+
6+
[Route("api/[controller]")]
7+
public class AboutController : Controller
8+
{
9+
private readonly IStringLocalizer<AboutController> _localizer;
10+
11+
public AboutController(IStringLocalizer<AboutController> localizer)
12+
{
13+
_localizer = localizer;
14+
}
15+
16+
[HttpGet]
17+
public string Get()
18+
{
19+
return _localizer["About Title"];
20+
}
21+
}

0 commit comments

Comments
 (0)