Skip to content

Commit

Permalink
Impl change culture for StoryBook
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Oct 24, 2024
1 parent 48a3093 commit 31b86b0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
47 changes: 44 additions & 3 deletions src/Inc.TeamAssistant.Stories/App.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
<BlazingStoryApp
@implements IAsyncDisposable

@inject IJSRuntime JsRuntime

<BlazingStoryApp
EnableHotReloading="true"
Title="TeamAssistUI docs"
Assemblies="[typeof(App).Assembly]"
DefaultLayout="typeof(DefaultLayout)"
AvailableColorSchemes="AvailableColorSchemes.Dark"/>
AvailableColorSchemes="AvailableColorSchemes.Dark">
<BrandLogoArea>
UI docs <a href="#" @onclick="ChangeCulture" @onclick:preventDefault="true" class="link_change-culture">
@_targetLanguage
</a>
</BrandLogoArea>
</BlazingStoryApp>

@code {
private string _targetLanguage = LanguageSettings.DefaultLanguageId.Value;

private IJSObjectReference? _appModule;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_appModule = await JsRuntime.InvokeAsync<IJSObjectReference>(
"import",
"./App.razor.js");

var currentCulture = await _appModule.InvokeAsync<string?>("currentCulture");

_targetLanguage = currentCulture == "en" ? "ru" : "en";
}
}

private async Task ChangeCulture()
{
if (_appModule is not null)
await _appModule.InvokeVoidAsync("changeCulture", _targetLanguage);
}

public async ValueTask DisposeAsync()
{
if (_appModule is not null)
await _appModule.DisposeAsync();
}
}
12 changes: 12 additions & 0 deletions src/Inc.TeamAssistant.Stories/App.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const cultureKey = "current_culture";

export function changeCulture(culture) {
window.localStorage.setItem(cultureKey, culture);
window.location.reload();
}

export function currentCulture() {
let culture = window.localStorage.getItem(cultureKey);

return culture ?? "en";
}
12 changes: 1 addition & 11 deletions src/Inc.TeamAssistant.Stories/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"profiles": {
"Inc.TeamAssistant.Stories.EN": {
"Inc.TeamAssistant.Stories": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
Expand All @@ -9,16 +9,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Inc.TeamAssistant.Stories.RU": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5102",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
10 changes: 6 additions & 4 deletions src/Inc.TeamAssistant.Stories/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
@using Microsoft.JSInterop
@using BlazingStory.Components
@using BlazingStory.Types
@using Inc.TeamAssistant.Stories
@using Inc.TeamAssistant.Stories.Layouts
@using Inc.TeamAssistant.Stories.Components

@using Inc.TeamAssistant.Primitives.Languages
@using Inc.TeamAssistant.WebUI.Components
@using Inc.TeamAssistant.WebUI.Services.ClientCore
@using Inc.TeamAssistant.WebUI.Icons
@using Inc.TeamAssistant.WebUI.Icons

@using Inc.TeamAssistant.Stories
@using Inc.TeamAssistant.Stories.Layouts
@using Inc.TeamAssistant.Stories.Components
10 changes: 10 additions & 0 deletions src/Inc.TeamAssistant.Stories/wwwroot/css/blazor-ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
content: var(--blazor-load-percentage-text, "Loading");
}

/* CUSTOM */
.link_change-culture {
float: right;
padding-right: 5px;
}
.link_change-culture:hover {
text-decoration: underline !important;
}
/* END CUSTOM */

@media (prefers-color-scheme: dark) {
.loading-progress {
background-color: #222425;
Expand Down
4 changes: 1 addition & 3 deletions src/Inc.TeamAssistant.Stories/wwwroot/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
<script src="leaflet/leaflet.js"></script>
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
let env = window.location.port === '5102' || window.location.hostname.includes("ru") ? "ru" : "en";

Blazor.start({
environment: env
environment: window.localStorage.getItem("current_culture")
});
</script>
</body>
Expand Down

0 comments on commit 31b86b0

Please sign in to comment.