I know how to get the current culture information for Blazor server app by using HTTPContext with IRequestCultureFeature. Is there any better way to get current browser culture in Blazor webassembly (wasm) / client app?
Better way to get browser culture in Blazor webassembly / client app
3.9k views Asked by Son04 At
2
There are 2 answers
0
agua from mars
On
You can request the browser language using JSInterop:
window.browserJsFunctions = {
getLanguage: () => {
return navigator.language || navigator.userLanguage;
},
getBrowserTimeZoneOffset: () => {
return new Date().getTimezoneOffset();
},
getBrowserTimeZoneIdentifier: () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
},
};
@inject IJSRuntime _jsRuntime
@code {
string _cultureName;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_cultureName = await _jsRuntime.InvokeAsync<string>("browserJsFunctions.getLanguage");
}
}
}
Related Questions in BLAZOR
- Failed to fetch dynamically imported module on Blazor JS Interop
- Implementing Azure AD B2C Authentication in .NET 8 Blazor Project (RenderMode: InteractiveAuto)
- New Blazor Web App, Password Reset "A valid antiforgery token was not provided"
- How to share authorization implemented in the server project with the client project in Blazor Web App Auto project?
- My Blazor UI is not responding on a domain, but working on another domain
- Telerik Blazor GridCommandButton not working
- How to set language in a server-side rendering blazor app
- Stopwatch loosing time in a browser using blazor and web assembly when browser put in background
- How to Nest a TelerikGrid inside TelerikForm with Blazor
- Blazor/Razor resolve components using dependency injection
- UI not updating after resetting object
- 3rd Party cookies error on deployment server
- Blazor DbContext in a service
- Blazor Web App (.Net 8) with oidc loses auth when switching to client
- Why Blazor 8 prerendering does not render all of the page?
Related Questions in CULTUREINFO
- Convert decimal to string with at least a zero for integer value in C#
- Culture info usage while converting string to float type
- RegionInfo by country code returns different EnglishName
- EF generates query with not supported minus sign in MS SQL (f.e. norvegian culture)
- Query string with dot as decimal separator in C#
- CultureNotFoundException when setting culture to "pl-PL" in .NET 8 application
- Which culture code to use for Chinese string resource (resx) files?
- MVC 5 Redirect the user to a page based on CultureInfo
- Unable to switch app language/locale at runtime in a .NET MAUI Blazor Hybrid Application
- C# Unable to load DLL - HRESULT: 0x8007007E occurs for application with Japanese culture
- C# CultureInfo in currently selected language
- Changing DATETIMEOFFSET Values globally across the application using culture in C# & ASP.NET MVC5
- Culture-specific sorting not applied when deployed
- Formatting and parsing decimals with the Arabic (Saudi Arabia) culture
- Localizing integer values
Related Questions in WEBASSEMBLY
- Run main several times of wasm in browser
- How to pass a byte array to a WASM module from wasmer in Rust?
- Does flutter wasm support native module import?
- How do I log WASM heap memory usage from Rust?
- WebAssembly C++ using JS value - Import #0 module="env": module is not an object or function
- Configuring WebAssembly/Emscripten locally build issues
- Is there a c# compiler/runtime for js in the browser
- How to set up Assimp for Qt6 MinGW 64-bit to run on WebAssembly
- Blazor WASM migration problem from NET 7 to NET 8 dotnet.js file is missing
- Kotlin WASM: How to add links?
- Can you specify webassembly extensions in deno deploy instances?
- How to bind event in MarkupString
- Uncaught SyntaxError: Unexpected token 'export' in wasm.js file when trying to build a chrome extension
- how can I load wasm function in a react app
- Why is `#[no_mangle]` required to see Rust functions in WASM library?
Related Questions in BLAZOR-CLIENT-SIDE
- Unable to hit breakpoints in Blazor WASM
- dotnet watch run blazor wasm loading pdb symbols
- Blazor Server: How to reliably handle the delay of user actions?
- Avoid 'multi steps refreshing' on page transition in Blazor solution for the entire layout
- Always on push notification on ASP.NET Core Blazor WebAssembly
- Communicating from child to parent when using Components in Blazor
- Blazor WASM Standalone: 404 error on Logout using AD B2C and MSAL
- Blazor InputFile Drag and Drop File Uploader with Combination of Files & Directories
- Blazor Wasm - cannot convert from 'method group' to 'EventCallback'
- Pass arguments to Blazor WebAssembly from pipeline build-publish-deployment
- Boostrap-select Blazor WASM
- Blazor WASM - Build Error - "Static web asset has a conflicting web root path"
- Blazor Page (WebAssembly/WASM): How can EditForm and EditContext are always be null in SetParametersAsync?
- Viewing an image captured in a Blazor Web Assembly PWA
- blazor.server.js breaks my script files in my Blazer Web App project
Related Questions in CURRENTCULTURE
- CurrentUICulture determining wrong culture
- Dealing with Decimal Separator and different Culture Settings
- CultureInfo change for all user in Blazor server side
- NET 6 decimal default interpolation conversion
- Does CultureInfo.CurrentCulture.Name always return the language code and country/region code separated by a hyphen?
- Can't keep CultureInfo across pages on .Net Maui Blazor
- What is the dotnet 6 equivalent of localizer.WithCulture?
- Set page culture through DropDown Menu and additionally by Query Parameter
- How to make culture setting match the OS setting in Blazor WebAssembly?
- Set default language culture for each client connection blazor-server
- Datetime.ParseExact inverse day and month value
- Set CultureInfo.CurrentCulture sets also CurrentUICulture and vice versa
- c# Language.resx doesn't work when I install my compiled application
- Format date as "MM/dd/yyyy" from a unstructured string in c# regex and
- CultureInfo.CurrentCulture always return en-US when publish Asp.net core angular 9 application
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Currently client size Blazor does not have a way to get culture data because mono on webassembly does not implement it (yet - tracked on github).
As far as I know, your best option is going to be able to find it with javascript, and then send it to Blazor with
JsInterop.Something like this to get you started, in the index page in a script tag:
Then in Blazor:
Then check the console window and you'll see the culture printed.
Edit: Actually someone made an open source package for this that may be easier to use: https://github.com/Blazored/Localisation
https://www.nuget.org/packages/Blazored.Localisation/
Edit 2: Blazored.Localisation is deprecated, you can use Blazored.LocalStorage instead: https://github.com/Blazored/LocalStorage
https://www.nuget.org/packages/Blazored.LocalStorage/