Coder Social home page Coder Social logo

jsakamoto / toolbelt.blazor.server.scopedculture Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 437 KB

This is a library for Blazor Server apps adding an ability that changes the current culture of each connection without reloading.

License: MIT License

HTML 22.57% C# 69.46% CSS 7.98%

toolbelt.blazor.server.scopedculture's Introduction

Blazor Server Scoped Culture NuGet Package

"It's a very dirty hack, but it works for now."

Summary

This is a library for Blazor Server apps adding an ability that changes the current culture of each connection without reloading.

movie.1

Quick Start

Installation

  1. Add the "Toolbelt.Blazor.Server.ScopedCulture" NuGet package to your Blazor server application project.
dotnet add package Toolbelt.Blazor.Server.ScopedCulture --prerelease
  1. Register the "Scoped Culture" service into DI container in your app.
// Program.cs
using Toolbelt.Blazor.Extensions.DependencyInjection; // ๐Ÿ‘ˆ Open this namespace, and...
...
builder.Services.AddScopedCulture(); // ๐Ÿ‘ˆ Add this line.
...
  1. To be convinience, open the "Toolbelt.Blazor.Server.ScopedCulture" name space globally.
@* _Import.razor *@
...
@* ๐Ÿ‘‡ Open this name space. *@
@using Toolbelt.Blazor.Server.ScopedCulture
  1. Surround the entire contents in App.razor with the <ScopedCultureZone> component tag.
@* App.razor *@
<ScopedCultureZone>
  <Router AppAssembly="@typeof(Program).Assembly">
    ...
  </Router>
</ScopedCultureZone>

Usage

When you want to change the current culture & current UI culture, don't set the culture values you want to change to CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture static properties directly because it doesn't cause any effect.

Instead, now you can call the SetCurrentCulture() method of the IScopedCulture service anytime with the culture name you want to change.

The SignalR connection and application states will be kept.

@* *.razor *@
@* ๐Ÿ‘‡ Inject the IScopedCulture service into your Razor components. *@
@inject IScopedCulture ScopedCulture
...
@code {
  ...
  // ๐Ÿ‘‡ Call "SetCurrentCulture()" method with the culture name such as "en", "sv", "ja", etc.
  this.ScopedCulture.SetCurrentCulture(cultureName);
  ...

Track to changes in current culture

Suppose you need to track changing current culture in the current connection on Blazor server apps, particularly re-rendering components after changed culture.

In that case, you can do that by one of the following three methods.

Method A. Surround contents by the ScopedCultureZone component

When you change the current culture in the current connection on Blazor Server apps, a ScopedCultureZone component's StateHasChanged() method will be invoked.

Then, that will cause re-rendering of the child content inside a ScopedCultureZone component.

@* *.razor *@
<ScopedCultureZone>
  This area will be re-rendered every time you change the current 
  culture by using the "ScopedCulture.SetCurrentCulture()".
</ScopedCultureZone>

Method B. Register componets to re-render by the RefreshWhenCultureChanged() method

Once you invoke the IScopedCulture.RefreshWhenCultureChanged() method with your component as an argument, that component's StateHasChanged() method will be invoked every time you change the current culture in the current connection on Blazor Server apps.

@* *.razor *@
@inject IScopedCulture ScopedCulture
...
@code 
{
  public override void OnInitialized() 
  {
    // ๐Ÿ‘‡ After doing this, the "StateHasChanged()" method of this component
    //    will be invoked every time you change the current culture
    //    by using the "IScopedCulture.SetCurrentCulture()".
    this.ScopedCulture.RefreshWhenCultureChanged(this);
  }
}

Method C. Handle the IScopedCulture.CurrentCultureChanged event

@* *.razor *@
@* ๐Ÿ‘‡ Please remember to implement IDisposable interface. *@
@implements IDisposable
@inject IScopedCulture ScopedCulture
...
@code 
{
  public override void OnInitialized() 
  {
    // ๐Ÿ‘‡ Handle the `IScopedCulture.CurrentCultureChanged` event.
    this.ScopedCulture.CurrentCultureChanged += this.ScopedCulture_CurrentCultureChanged;
  }

  private void ScopedCulture_CurrentCultureChanged(object sender, EventArgs e) {
    // ๐Ÿ‘‰ This method will be invoked every time you change 
    // the current culture by using the "IScopedCulture.SetCurrentCulture()".
  }

  public void Dispose() {
    // ๐Ÿ‘‡ Please remember to detach the event handler.
    this.ScopedCulture.CurrentCultureChanged -= this.ScopedCulture_CurrentCultureChanged;
  }
}

Supported versions

Blazor Server apps on .NET Core 3.1 or later (including .NET 5.0, 6.0, 7.0) are supported.

Disclaimer

Please remember that this library access and overwrite non-public API of the Blazor Server's infrastructure.

That means there is a risk that this library might cause your apps to be crashed unexpectedly in the current and future versions of .NET.

Release notes

The release notes is here.

License

MIT License

toolbelt.blazor.server.scopedculture's People

Contributors

jsakamoto avatar

Stargazers

Wicked Wizzel avatar NAKIGOE.ORG avatar  avatar Miguel Pimenta avatar

Watchers

 avatar James Cloos avatar  avatar

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.