i18nStronglyTypedCore 1.0.0.6

Strongly typed i18n (internationalization) for dot net core. This is a .net core port of afana.me's respected i18n for MVC5 (xml resources) ++. Tutorials provided.

Install-Package i18nStronglyTypedCore -Version 1.0.0.6
dotnet add package i18nStronglyTypedCore --version 1.0.0.6
<PackageReference Include="i18nStronglyTypedCore" Version="1.0.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add i18nStronglyTypedCore --version 1.0.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

i18nStronglyTypedCore

This is the source code for the i18nStronglyTypedCore nuget package.

Github source code

About

This is a .net core port of afana's excellent i18n (internationalization) provider for MVC 5, using xml for the resource strings.

There's also some extras, such as per request language responses and multiple resource file handling (see tutorial below).

Of course, dotnet core has its' own i18n provider
but like many people, I'd rather keep things strongly typed as much as possible. It's simple to get up and running.

The upsides to this package are:

  1. Strongly typed
  2. Seems pretty solid
  3. Only takes a couple of minutes to get up and running.
  4. Can handle multiple resource files.
  5. Can handle "per request" language responses (see tutorial)

The downsides to this package are:

  1. *Doesn't work with Attributes (the standard .net core one does)
  2. If you create the c# part, but forget to put the corresponding row in the xml, the error is a bit tricky to work out.

Prerequisites

  • Dotnet core 2.1+

To get up and running

(see deeper tutorial with screenshots at Github if needed)

It's really simple to get going and only takes a few minutes. Assuming an MVC Core project:

  1. Via the CLR or Package Manager (View > Other Windows > Package Manager Console) enter the following, then hit enter:

Install-Package i18nStronglyTypedCore

  1. In wwwroot, add a folder called 'Resources' and in that add an xml file called 'Resources.xml'.

  2. In resources.xml, insert the following placeholder content:

WARNING: The following example has the default culture to be British English (en-GB). You may need en-US or another culture.

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <!-- ENGLISH -->
    <resource culture="en-GB" type="string" name="Site_Name" value="Site name (english)"></resource>
    
    <!-- FRENCH -->
    <resource culture="fr-FR" type="string" name="Site_Name" value="Site name (Francais)"></resource>

</resources>
  1. Create a class and have that class inherit from i18nStronglyTypedCore.i18n (I made a class called 'i18n.cs' in a 'Resources' folder). Include the following property in the class.

public static string Site_Name { get { return GetStringValue(); } }

  1. In startup.cs 'Configure' method, add the following 2 lines to initialise the resources.
var pathToResources = System.IO.Path.Combine(env.WebRootPath, @"Resources\Resources.xml");
Resources.i18n.InitResources(pathToResources);

NOTE: If you have more than one resource file, you can add multiple files as shown below (but don't copy for this tutorial, as we only have 1 resource file)

  1. In views/home/index, remove the standard content and add the following (remember to replace 'Testweb.Resources' with you own website's namespace).
@{
    ViewData["Title"] = "Home Page";
}
<h4>English Site Name</h4>
<p>@Testweb.Resources.i18n.Site_Name</p>

<h4>French Site Name</h4>
<p>@Testweb.Resources.i18n.GetLocalisedStringValue(() => Testweb.Resources.i18n.Site_Name, "fr-FR")</p>
  1. Run it to see the results.

More details

If you are unsure of how to set the culture, how to add more resource files or add more properties, see the detailed tutorial at github source readme.

i18nStronglyTypedCore

This is the source code for the i18nStronglyTypedCore nuget package.

Github source code

About

This is a .net core port of afana's excellent i18n (internationalization) provider for MVC 5, using xml for the resource strings.

There's also some extras, such as per request language responses and multiple resource file handling (see tutorial below).

Of course, dotnet core has its' own i18n provider
but like many people, I'd rather keep things strongly typed as much as possible. It's simple to get up and running.

The upsides to this package are:

  1. Strongly typed
  2. Seems pretty solid
  3. Only takes a couple of minutes to get up and running.
  4. Can handle multiple resource files.
  5. Can handle "per request" language responses (see tutorial)

The downsides to this package are:

  1. *Doesn't work with Attributes (the standard .net core one does)
  2. If you create the c# part, but forget to put the corresponding row in the xml, the error is a bit tricky to work out.

Prerequisites

  • Dotnet core 2.1+

To get up and running

(see deeper tutorial with screenshots at Github if needed)

It's really simple to get going and only takes a few minutes. Assuming an MVC Core project:

  1. Via the CLR or Package Manager (View > Other Windows > Package Manager Console) enter the following, then hit enter:

Install-Package i18nStronglyTypedCore

  1. In wwwroot, add a folder called 'Resources' and in that add an xml file called 'Resources.xml'.

  2. In resources.xml, insert the following placeholder content:

WARNING: The following example has the default culture to be British English (en-GB). You may need en-US or another culture.

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <!-- ENGLISH -->
    <resource culture="en-GB" type="string" name="Site_Name" value="Site name (english)"></resource>
    
    <!-- FRENCH -->
    <resource culture="fr-FR" type="string" name="Site_Name" value="Site name (Francais)"></resource>

</resources>
  1. Create a class and have that class inherit from i18nStronglyTypedCore.i18n (I made a class called 'i18n.cs' in a 'Resources' folder). Include the following property in the class.

public static string Site_Name { get { return GetStringValue(); } }

  1. In startup.cs 'Configure' method, add the following 2 lines to initialise the resources.
var pathToResources = System.IO.Path.Combine(env.WebRootPath, @"Resources\Resources.xml");
Resources.i18n.InitResources(pathToResources);

NOTE: If you have more than one resource file, you can add multiple files as shown below (but don't copy for this tutorial, as we only have 1 resource file)

  1. In views/home/index, remove the standard content and add the following (remember to replace 'Testweb.Resources' with you own website's namespace).
@{
    ViewData["Title"] = "Home Page";
}
<h4>English Site Name</h4>
<p>@Testweb.Resources.i18n.Site_Name</p>

<h4>French Site Name</h4>
<p>@Testweb.Resources.i18n.GetLocalisedStringValue(() => Testweb.Resources.i18n.Site_Name, "fr-FR")</p>
  1. Run it to see the results.

More details

If you are unsure of how to set the culture, how to add more resource files or add more properties, see the detailed tutorial at github source readme.

Release Notes

1.0.0.6 - New public method 'SetCustomResourcesCulture' to enable getting of all resources in a different locale to CurrentUICulture.
1.0.0.5 - Additional public method to get a list of locales available (from the resources).
1.0.0.4 - Change to metadata only
1.0.0.3 - Can now have multiple resource files

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0.6 304 10/1/2018
1.0.0.5 242 9/28/2018
1.0.0.4 234 9/26/2018
1.0.0.3 257 9/20/2018
1.0.0.2 241 9/19/2018
1.0.0.1 256 9/19/2018