Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back support for i18n in Demo app #136

Merged
merged 1 commit into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions demo/Demo.csproj
Expand Up @@ -2,18 +2,19 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\lib\CronExpressionDescriptor.csproj" />
</ItemGroup>

</Project>
60 changes: 37 additions & 23 deletions demo/Pages/Index.razor
@@ -1,12 +1,15 @@
@page "/"
@using System.Globalization;
@using Microsoft.AspNetCore.WebUtilities;
@inject NavigationManager UriHelper
@inject NavigationManager NavigationManager

<form>
<fieldset>
<label for="expression">Cron Expression</label>
<input id="expression" name="expression" @bind="expression" placeholder="" type="text" />
<label for="locale">Language</label>
<select id="locale" name="locale" @bind="locale">
<select id="locale" name="locale" @onchange="LocaleChanged" value="@locale">
<option value="en-US">English</option>
@* <option value="zh-CN">Chinese (Simplified)</option>
<option value="zh-CN">Chinese (Simplified)</option>
<option value="zh-TW">Chinese (Traditional)</option>
<option value="da">Danish</option>
<option value="nl">Dutch</option>
Expand All @@ -24,8 +27,10 @@
<option value="es">Spanish</option>
<option value="sv">Swedish</option>
<option value="tr">Turkish</option>
<option value="uk">Ukrainian</option> *@
<option value="uk">Ukrainian</option>
</select>
<label for="expression">Cron Expression</label>
<input id="expression" name="expression" @bind="expression" placeholder="" type="text" />
<button type="button" class="button button-black" id="describe" @onclick="UpdateDescription">Describe</button>
</fieldset>
</form>
Expand All @@ -37,24 +42,33 @@
</blockquote>

@code {
private string expression = "* * * * *";
private string locale = "en-US";
private string description = "";
private void UpdateDescription()
{
var options = new CronExpressionDescriptor.Options()
{
ThrowExceptionOnParseError = false,
Verbose = false,
DayOfWeekStartIndexZero = true,
Locale = (this.locale)
};
private string locale = "en-US";
private string expression = "0 23 ? * MON-FRI";
private string description = "";
private void UpdateDescription()
{
var options = new CronExpressionDescriptor.Options()
{
ThrowExceptionOnParseError = false,
Verbose = false,
DayOfWeekStartIndexZero = true,
};

this.description = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(this.expression, options);
}
this.description = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(this.expression, options);
}

protected override void OnInitialized()
{
this.UpdateDescription();
}
private void LocaleChanged(ChangeEventArgs e)
{
NavigationManager.NavigateTo($"/?locale={e.Value.ToString()}&expression={this.expression}", true);
}

protected override void OnInitialized()
{
var uri = UriHelper.ToAbsoluteUri(UriHelper.Uri);
this.expression = QueryHelpers.ParseQuery(uri.Query).TryGetValue("expression", out var type) ? type.First() : this.expression;

this.locale = CultureInfo.DefaultThreadCurrentCulture?.ToString() ?? this.locale;

this.UpdateDescription();
}
}
45 changes: 31 additions & 14 deletions demo/Program.cs
@@ -1,25 +1,42 @@
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using System.Globalization;

namespace demo
namespace Demo
{
public class Program
public class Program
{
public static async Task Main(string[] args)
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services.AddSingleton(new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddLocalization();

var host = builder.Build();

// Set the culture by using the "locale" querystring
var jsInterop = host.Services.GetRequiredService<IJSRuntime>();
var result = await jsInterop.InvokeAsync<string>("blazorCulture.get");
if (result == "")
{
result = null;
}

if (result != null)
{
var culture = new CultureInfo(result);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}

await builder.Build().RunAsync();
}
await host.RunAsync();
}
}
}
2 changes: 1 addition & 1 deletion demo/_Imports.razor
Expand Up @@ -5,4 +5,4 @@
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using demo
@using Demo