RegionOrebroLan / .NET-Localization-Extensions

Localization additions and extensions for .NET.
MIT License
0 stars 0 forks source link

.NET-Localization-Extensions

Localization additions and extensions for .NET.

NuGet

1 Path-based localization

Path-based localization implemented with JSON- and XML-localization.

Built on ASP.NET Core localization:

The functional idea behind it:

1.1 Features

1.2 Configuration & settings

1.2.1 Localization-options

1.2.1.1 Remarks

To get the FileResourcesDirectoryPath to be null it have to be absent from the configuration-file". This want work:

{
    "Localization": {
        "FileResourcesDirectoryPath": null
    }
}

1.2.2 Configuration

AppSettings.json example:

{
    "Localization": {
        "AlphabeticalSorting": true,
        "EmbeddedResourceAssemblies": [
            "Animals",
            "Colors",
            "Numbers",
            "Prioritized-words",
            "Root-namespaced-resources",
            "Words"
        ],
        "FileResourcesDirectoryPath": "Resources",
        "IncludeParentCultures": false,
        "ThrowErrors": false
    }
}

1.3 Setup

Example: Web-application startup

1.3.1 With AppSettings.json

...

builder.Services.AddPathBasedLocalization(builder.Configuration);

...
1.3.1.1 If we want to use a static cache - AppSettings.json
{
    "LocalizationDependencyInjection": {
        "StaticCache": true
    }
}

1.3.2 Action

...

builder.Services.AddPathBasedLocalization(localizationOptions =>
{
    localizationOptions.AlphabeticalSorting = true;
    localizationOptions.EmbeddedResourceAssemblies.Add("Assembly-name"); // Can be an assembly-name, assembly-name-pattern (MyAssembly*) or an assembly-fullname. 
    localizationOptions.FileResourcesDirectoryPath = "Resources";
});

...
1.3.2.1 If we want to use a static cache
...

builder.Services.AddPathBasedLocalization(localizationOptions =>
{
    localizationOptions.AlphabeticalSorting = true;
    localizationOptions.EmbeddedResourceAssemblies.Add("Assembly-name"); // Can be an assembly-name, assembly-name-pattern (MyAssembly*) or an assembly-fullname. 
    localizationOptions.FileResourcesDirectoryPath = "Resources";
}, true);

...

1.3.3 Default

...

builder.Services.AddPathBasedLocalization();

...
1.3.3.1 If we want to use a static cache
...

builder.Services.AddPathBasedLocalization(true);

...

1.4 Examples

1.4.1 Code

IStringLocalizer:

IStringLocalizer<T>, example where T is Company.WebApplication.Controllers.HomeController:

1.4.2 Lookup

A localization-entry can be a lookup to another entry:

1.4.2.1 JSON
{
    "Cultures": [
        {
            "Culture": {
                "Name": "en",
                "Nodes": [
                    {
                        "Node": {
                            "Name": "Common",
                            "Entries": {
                                "Yes": {
                                    "Value": "Yes"
                                }
                            }
                        }
                    },
                    {
                        "Node": {
                            "Name": "Company.WebApplication.Controllers.HomeController",
                            "Entries": {
                                "Yes": {
                                    "Lookup": "Common.Yes"
                                }
                            }
                        }

                    }
                ]
            }
        }
    ]
}
1.4.2.2 XML
<cultures>
    <culture name="en">
        <common>
            <yes>Yes</yes>
        </common>
        <company>
            <webApplication>
                <controllers>
                    <homeController>
                        <yes lookup="common.yes" />
                    </homeController>
                </controllers>
            </webApplication>
        </company>
    </culture>
</cultures>

1.4.3 Embedded resources

1.4.4 Sample-applications

2 Development

2.1 Signing

Drop the "StrongName.snk" file in the repository-root. The file should not be included in source control.