Skip to content

DCCS-IT-Business-Solutions/DCCS.LocalizedString.NetStandard

Repository files navigation

DCCS.LocalizedString.NetStandard Build status NuGet Badge

DCCS.LocalizedString.NetStandard provide a multilanguage string implementation and multilangue exceptions.

The accessor keys for the strings have to be defined as static objects which can be found by reflection. There exist furthermore a tools which can create resource files in C# projects for the required keys.

Note: Take a look at DCCS.AspNetCore.LocalizedString for usage of this library in an ASP.NET Core project

Installation

Install DCCS.LocalizedString.NetStandard with NuGet:

Install-Package DCCS.LocalizedString.NetStandard

Or via the .NET Core command line interface:

dotnet add package DCCS.LocalizedString.NetStandard

Either commands, from Package Manager Console or .NET Core CLI, will download and install DCCS.LocalizedString.NetStandard and all required dependencies.

Resource Creator Tool

This tools create resource file by using reflection out of your compiled .NET assembly

Install the tool in the package managment console, command prompt or powershell

dotnet tool install --global DCCS.LocalizedString.ProjectResourceCreator

To run the tool go to your project or solution folder and start

createprojectresource

This will create resource files for invariant and current user interface culture. If you want other resources run the tool with the cultures option (e.g. for es and fr):

createprojectresource --cultures es,fr

Important: For classic .net framework project ensure that you have build your projects first!

Note: Resource entries which values starts with '!' are ignored

Uninstall the tool in the package managment console, command prompt or powershell

dotnet tool uninstall --global DCCS.LocalizedString.ProjectResourceCreator

Do upgrade the tool to the lasted version, deinstall the previous and install the new

dotnet tool uninstall --global DCCS.LocalizedString.ProjectResourceCreator
dotnet tool install --global DCCS.LocalizedString.ProjectResourceCreator

Examples

Simple console application sample:

using System;
using System.Diagnostics;
using System.Globalization;
using DCCS.LocalizedString.NetStandard;

class MyClass
{
    // Declaration of the key - Must be static in none generic classes
    static readonly LocalizedStringKey ThisIsASample = new LocalizedStringKey("This is a sample application");

    // If you want use runtime parameters, use the LocalizedFormatKey and specifiy the name of the provided parameters
    static readonly LocalizedFormatKey ThisIsASampleError = new LocalizedFormatKey("This is a sample error thrown at {0}", "Time Stamp");

    void Main()
    {
        ITranslationService translationService = new TranslationService(new ITranslationProviderService[] { new ResourceTranslationProvider() });
        ILocalizedString localizedString = translationService.Create(ThisIsASample);

        // Write out the message in the current UI culture:
        Console.WriteLine(localizedString.GetText(CultureInfo.CurrentUICulture));

        // Write out the message in the current UI culture:
        Console.WriteLine(localizedString.GetText(CultureInfo.CurrentUICulture));

        // Write out the message in english
        Trace.WriteLine(localizedString.GetText(CultureInfo.InvariantCulture));

        try
        {
            // To use runtimeparameters, specify a LocalizedFormatKey and a matching number of runtime parameters
            ILocalizedString localizedStringWithRuntimeParamters = translationService.Create(ThisIsASampleError, DateTime.Now);
            throw new LocalizedException(localizedStringWithRuntimeParamters);
        }
        catch (Exception e)
        {
            ILocalizedString localizedErrorMessage = LocalizedException.FindLocalizedMessage(e);
            if (localizedErrorMessage != null)
                Console.WriteLine(localizedErrorMessage.GetText(CultureInfo.CurrentUICulture));
            else
                Console.WriteLine($"Internal error {e.Message}");

            ILocalizedString createsAlwaysAUserFriendlyMessage = LocalizedException.CreateLocalizedMessage(translationService, e);
            // Will write out "Internal Error" if no LocalizedException was found in the exception hirachy
            Console.WriteLine(createsAlwaysAUserFriendlyMessage.GetText(CultureInfo.CurrentUICulture));
        }

        // Create a localized string out of an enum - Note: the enum must be marked with an [Translated] attribute
        ILocalizedString localizedEnumValue = translationService.Create(Colors.Red);
        Console.WriteLine(localizedEnumValue.GetText(CultureInfo.CurrentUICulture));
    }

    [Translated]
    enum Colors
    {
        Red,
        Blue,
        Green,
        [Translated("Dark Green")] // Provide default text, if the text of the enum value is not suitable
        GreenDark,
    }
}

Contributing

All contributors are welcome to improve this project. The best way would be to start a bug or feature request and discuss the way you want find a solution with us. After this process, please create a fork of the repository and open a pull request with your changes. Of course, if your changes are small, feel free to immediately start your pull request without previous discussion.

License

This library is MIT licensed

About

DCCS.LocalizedString.NetStandard provide a multilanguage string implementation and multilangue exceptions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages