- Internationalization involves Globalization and Localization.
- Globalization is the process of designing apps that support different cultures.
- Globalization adds support for input, display, and output of a defined set of language scripts that relate to specific geographic areas.
- Localization is the process of adapting a globalized app, which you have already processed for localizability, to a particular culture/locale.
- App localization involves the following:
- Make the app's content localizable
- Provide localized resources for the languages and cultures you support
- Implement a strategy to select the language/culture for each request
IStringLocalizer
and IStringLocalizer<T>
were architected to improve productivity when developing localized apps.
IStringLocalizer
uses the ResourceManager and ResourceReader to provide culture-specific resources at run time.
- The simple interface has an indexer and an
IEnumerable
for returning localized strings.
IStringLocalizer
doesn't require you to store the default language strings in a resource file.
- You can develop an app targeted for localization and not need to create resource files early in development.
- In the following code, the
IStringLocalizer<T>
implementation comes from Dependency Injection.
- If the localized value of "About Title" isn't found, then the indexer key is returned, that is, the string "About Title".
- You can leave the default language literal strings in the app and wrap them in the localizer, so that you can focus on developing the app.
- You develop your app with your default language and prepare it for the localization step without first creating a default resource file.
- Alternatively, you can use the traditional approach and provide a key to retrieve the default language string.
[Route("api/[controller]")]
public class AboutController : Controller
{
private readonly IStringLocalizer<AboutController> _localizer;
public AboutController(IStringLocalizer<AboutController> localizer)
{
_localizer = localizer;
}
[HttpGet]
public string Get()
{
return _localizer["About Title"];
}
}
- Use the
IHtmlLocalizer<T>
implementation for resources that contain HTML.
IHtmlLocalizer
HTML encodes arguments that are formatted in the resource string, but doesn't HTML encode the resource string itself.
public IActionResult Hello(string name)
{
ViewData["Message"] = _localizer["<b>Hello</b><i> {0}</i>", name];
return View();
}
private readonly IStringLocalizer _localizer;
private readonly IStringLocalizer _localizer2;
public TestController(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create(type);
_localizer2 = factory.Create("SharedResource", assemblyName.Name);
}
public IActionResult About()
{
ViewData["Message"] = _localizer["Your application description page."]
+ " loc 2: " + _localizer2["Your application description page."];
// ...
}
- The code above demonstrates each of the two factory create methods.
- You can partition your localized strings by controller, area, or have just one container.
- In the sample app, a dummy class named
SharedResource
is used for shared resources.
// Dummy class to group shared resources
namespace Localization.StarterWeb
{
public class SharedResource
{
}
}
- Some developers use the
Startup
class to contain global or shared strings.
- In the sample below, the
InfoController
and the SharedResource
localizers are used: