Giter Site home page Giter Site logo

welisonmenezes / wm-blazor-internationalization Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 27.38 MB

Localize text and html blocks in your Blazor Web App! (internationalization, localization, translation, i18n). Dotnet 5.

HTML 53.85% CSS 17.33% C# 27.13% JavaScript 1.68%
blazor internacionalization i18n i18n-blazor localize localization blazor-localization translation webassembly blazor-webassembly blazor-server

wm-blazor-internationalization's Introduction

WM Blazor Internationalization

Summary

This package is an another way to localize text and html blocks in your Blazor Web App! (internationalization, localization, translation, ...)

  • Dotnet Version: 5.0
  • It works on both Blazor Server and Blazor WebAssembly.
  • Your translations come from a json file.

NuGet Package

https://www.nuget.org/packages/WMBlazorInternationalization/

GitHub Repository

https://github.com/welisonmenezes/wm-blazor-internationalization

Configuration

First, import the namespaces in _Imports.razor

@using WMBlazorInternationalization
@using WMBlazorInternationalization.WMBI

Then, add the WMBInternationalization component to wrap all the components in your App.razor. You can do it by wrapping the Router component of the aplication.

Note that the ChildContent must wrap the app content, the ChildLoading must wrap the content shown when the localization is loading and the ChildError must wrap ther error content when the localization is fail.

<WMBInternationalization>
    <ChildContent>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ChildContent>
    <ChildLoading>
        <p>Loading your localization data.</p>
    </ChildLoading>
    <ChildError>
        <p>An error was occurred while loading your localization.</p>
    </ChildError>
</WMBInternationalization>

Parameters

You can also customize some things by passing parameters

Example:

<WMBInternationalization defaultLanguage="pt" defaultFileName="Locale" defaultFilePath="i18ntext/" storageType="sessionStorage">
    <ChildContent>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ChildContent>
    <ChildLoading>
        <p>Loading your localization data.</p>
    </ChildLoading>
    <ChildError>
        <p>An error was occurred while loading your localization.</p>
    </ChildError>
</WMBInternationalization>
  • defaultLanguage: The default is en, but you can choose anyone or null;
  • fileName: The default is Locale, but you can choose anyone or null;
  • filePath: The default is i18ntext/, but you can choose anyone or null;
  • storageType: The default is localStorage, but you can choose sessionStorage and null. When null the selected language will not be persisted;

When parameters are null, the default values will be assumed.

Setting up the translations

Inside the wwwroot directory, create a folder called i18ntext and, inside that, create a json file for each language you want. That is important to name this files as following:

Locale.[language-code].json

Examples:

Locale.en.json
Locale.pt.json
Locale.pt-br.json
Locale.es.json

Note that i18next and Locale values can be changed as shown above.

Create localized text source files as JSON

Example:

{
    "Key1": "Localized text 1",
    "Key2": "Localized text 2",
    ...
}

Using in pages and components

Inside your component, get the WMBInternationalization functionalities by the CascadingParameter named CascadeWMBI

@code {
    [CascadingParameter(Name = "CascadeWMBI")]
    protected WMBInternationalization WMBI { get; set; }
}

Now you has access to 3 methods: GetTranslation, SetLanguage and GetCurrentLang.

GetTranslation

By GetTranslation you can read any key property from the json file regarding current language.

Example:

<h1>@WMBI.GetTranslation("HelloWorld")</h1>

The param is the json key you want to access from the Locale json file.

SetLanguage

By SetLanguage you can choose another language.

Example:

WMBI.SetLanguage("pt");

The param is the language code you want to be used.

GetCurrentLang

By GetCurrentLang you can see the current language code.

Example:

@WMBI.GetCurrentLang()

By this method you can even choose show some html blocks depending on the selected language.

Example:

@if(@WMBI.GetCurrentLang() == "en")
{
    <img src="img/reino-unido.png">
}

@if(@WMBI.GetCurrentLang() == "pt")
{
    <img src="img/brasil.png">
}

For live demo, download it from github and run, either BlazorServerDemo or BlazorWasmDemo project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.