{"id":17621498,"url":"https://github.com/sql-mistermagoo/blazorcultures","last_synced_at":"2025-04-23T16:25:10.127Z","repository":{"id":91797695,"uuid":"277023176","full_name":"SQL-MisterMagoo/BlazorCultures","owner":"SQL-MisterMagoo","description":"Sample Globalisation/Localisation in Blazor Server","archived":false,"fork":false,"pushed_at":"2020-07-04T02:35:56.000Z","size":225,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T01:27:35.897Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SQL-MisterMagoo.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-04T02:22:13.000Z","updated_at":"2020-07-29T01:06:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"407251ae-0fdc-4d72-942a-d40c33352c65","html_url":"https://github.com/SQL-MisterMagoo/BlazorCultures","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SQL-MisterMagoo%2FBlazorCultures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SQL-MisterMagoo%2FBlazorCultures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SQL-MisterMagoo%2FBlazorCultures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SQL-MisterMagoo%2FBlazorCultures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SQL-MisterMagoo","download_url":"https://codeload.github.com/SQL-MisterMagoo/BlazorCultures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250468929,"owners_count":21435562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-22T20:43:38.569Z","updated_at":"2025-04-23T16:25:10.072Z","avatar_url":"https://github.com/SQL-MisterMagoo.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Globalisation and Localisation in Blazor Server\n\nCurrent documentation :\n[ASP.NET Core Docs](https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-3.1#blazor-server)\n\nThis repo shows an exmaple of a working Glob/Loc Blazor project.\n\nTry it out: https://blazorcultures.azurewebsites.net/\n( Azure free tier, so don't expect speed! )\n\n### Things to look at:\n\n#### Enabling Localisation in startup.cs\n##### Line 32 - add localization and tell it where to find the resx files\n   \n``` csharp\nservices.AddLocalization(options =\u003e options.ResourcesPath = \"Resources\");\n```\n\n##### Lines 48-51 - add supported cultures\n\n``` csharp\nvar supportedCultures = new[] { \"en-GB\", \"en-US\", \"fr\" };\nvar localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])\n\t.AddSupportedCultures(supportedCultures)\n\t.AddSupportedUICultures(supportedCultures);\n```\n\n#### Resource files (resx)\n\nWe told the system to look in `Resources` so that's where to create the files.\n\nYou don't need a resx for your default culture - in this case `en-GB`, so there are just files for `fr` and `en-US`.\n\n```\nPages.Index.en-US.resx\nPages.Index.fr.resx\n```\n\n*Note: The resource file names match the namespace of the class they are for - in this case the resources are for the Index page, whose namespace is Pages by default*\n\n#### Host page\n\nBecause Blazor runs over SignalR it has no HTTP Requests to work with, so we add code to the Host page to set a cookie with the client culture.\nSignalR will use the client side cookie to set the correct Culture.\n\n##### Lines 3-4 - bring in the required namespaces\n\n``` csharp\n@using System.Globalization\n@using Microsoft.AspNetCore.Localization\n```\n\n##### Lines 22-29 - create the cookie for SignalR\n\n``` csharp\n    @{\n        this.HttpContext.Response.Cookies.Append(\n            CookieRequestCultureProvider.DefaultCookieName,\n            CookieRequestCultureProvider.MakeCookieValue(\n                new RequestCulture(\n                    CultureInfo.CurrentCulture,\n                    CultureInfo.CurrentUICulture)));\n    }\n```\n\n#### Using localised strings in Index.razor\n\n##### Line 2 - Inject a String Localizer from DI for the Index\n\n``` csharp\n@inject Microsoft.Extensions.Localization.IStringLocalizer\u003cIndex\u003e text\n```\n\n##### Line 5 - use a localized string\n\n```\n\u003ch1\u003e@text[\"Hello, world!\"]\u003c/h1\u003e\n```\n\n*Note that the string \"Hello, world!\" is both the default value and the lookup key in the resource files.*\n\n#### Bonus Feature - Main Layout - Choose a Culture\n\nI've added some code to allow the user to manually choose a supported culture.\nIf they change culture, we set the cookie and reload the page.\n\n#### Bonus Feature - Query String Culture\n\nBy default ASP.NET Core configures a Querystring culture provider, so you can set the culture in the browser address bar.\n\nTry adding `/?culture=fr` to the URL to set French culture.\n\n#### Bonus Feature - Timezone Offset in App.razor\n\nTimezones are hard. I like to get the UTC offset from the client and share it as a CascadingValue.\n\n##### Lines 4-6 - Cascading Value\n\nI have wrapped the RouteView in a cascading value that share the time offset to any component that wants it.\n\n##### Lines 14-26 Get the offset from the client\n\nThese lines simply use JS Interop to get the client browser UTC offset.\nIt's crude, but works a lot of the time as users tend to have their system clocks set appropriately for them, regardless of what timezone might be reported by the system.\n\n\n\n \n##### \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsql-mistermagoo%2Fblazorcultures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsql-mistermagoo%2Fblazorcultures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsql-mistermagoo%2Fblazorcultures/lists"}