Skip to content

Commit

Permalink
Adds localization (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael de Bruin <[email protected]>
  • Loading branch information
MdeBruin93 and Michael de Bruin authored Nov 5, 2024
1 parent 64f7cc6 commit 9c961c2
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 35 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="9.0.0-rc.1.24511.1" />
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.2.1" />
<PackageVersion Include="markdig" Version="0.37.0" />
<PackageVersion Include="Microsoft.Extensions.Localization" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Components.QuickGrid" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0-rc.2.24473.5" />
Expand Down
41 changes: 25 additions & 16 deletions SharpSite.Web/Components/Admin/EditPost.razor
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
@page "/admin/post/{urldate:int?}/{slug?}"

@inject IOutputCacheStore OutputCacheStore
@inject IPostRepository PostService
@inject NavigationManager NavManager
@inject IStringLocalizer<SharedResource> Localizer
@rendermode InteractiveServer

<PageTitle>@if (string.IsNullOrEmpty(Slug)) {
<text>New Post</text>
} else {
<text>Edit Post</text>
}
<PageTitle>
@if (string.IsNullOrEmpty(Slug))
{
<text>@Localizer["sharpsite_newpost"]</text>
}
else
{
<text>@Localizer["sharpsite_editpost"]</text>
}
</PageTitle>


<h1>@if (string.IsNullOrEmpty(Slug)) {
<text>New Post</text>
} else {
<text>Edit Post</text>
}
<h1>
@if (string.IsNullOrEmpty(Slug))
{
<text>@Localizer["sharpsite_newpost"]</text>
}
else
{
<text>@Localizer["sharpsite_editpost"]</text>
}
</h1>

@if (Post is not null)
{
<div class="form-group">
<label for="title">Title</label>
<label for="title">@Localizer["sharpsite_editpost_title"]</label>
<input type="text" class="form-control" id="title" placeholder="Enter title" @bind="Post.Title" />
</div>

@** add a publish date field *@
<div class="form-group">
<label for="publishDate">Publish Date</label>
<label for="publishDate">@Localizer["sharpsite_editpost_publishdate"]</label>
<input type="date" class="form-control" id="publishDate" @bind="Post.PublishedDate" />
</div>

@** add a textarea for the Post description *@
<div class="form-group">
<label for="description">Description</label>
<label for="description">@Localizer["sharpsite_editpost_description"]</label>
<textarea class="form-control" id="description" rows="3" @bind="Post.Description"></textarea>
</div>

<div class="form-group">
<label for="content">Content</label>
<label for="content">@Localizer["sharpsite_editpost_content"]</label>
<TextEditor @bind-Content="@Post.Content" />
</div>

}

<div class="form-group">
<button type="submit" class="btn btn-primary" @onclick="SavePost">Save</button>
<button type="submit" class="btn btn-primary" @onclick="SavePost">@Localizer["sharpsite_editpost_save"]</button>
</div>
@code {
[Parameter] public string? Slug { get; set; }
Expand Down
9 changes: 5 additions & 4 deletions SharpSite.Web/Components/Admin/PostList.razor
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
@attribute [Route(RouteValues.AdminPostList)]
@using Microsoft.AspNetCore.Components.QuickGrid
@inject IPostRepository PostService
@inject IStringLocalizer<SharedResource> Localizer

<PageTitle>Posts</PageTitle>

<h1>Posts</h1>

@if (Posts is null)
{
<p><em>Loading...</em></p>
<p><em>@Localizer["sharpsite_loading"]</em></p>
}
else
{

@** add a link to create a new post *@
<a href="/admin/post">New Post</a>
<a href="/admin/post">@Localizer["sharpsite_newpost"]</a>


@** use a quickgrid to format the list of posts with each posts title being a link to the editpost page for that post *@
<QuickGrid Items="@GridPosts" style="width: 100%">
<TemplateColumn Title="Title">
<TemplateColumn Title="@Localizer["sharpsite_postlist_title"]">
<a href="@($"/admin/post{context.ToUrl()}")">@context.Title</a>
</TemplateColumn>
<PropertyColumn Property="@(p => p.PublishedDate.ToLocalTime())"
Format="g"
Title="Published Date" />
Title="@Localizer["sharpsite_postlist_publisheddate"]" />
</QuickGrid>
}

Expand Down
18 changes: 17 additions & 1 deletion SharpSite.Web/Components/App.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!DOCTYPE html>
@using Microsoft.AspNetCore.Localization
<!DOCTYPE html>
<html lang="en">

<head>
Expand All @@ -25,3 +26,18 @@
</body>

</html>

@code {
[CascadingParameter]
public HttpContext? HttpContext { get; set; }

protected override void OnInitialized()
{
HttpContext?.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(
new RequestCulture(
CultureInfo.CurrentCulture,
CultureInfo.CurrentUICulture)));
}
}
27 changes: 27 additions & 0 deletions SharpSite.Web/Components/LanguagePicker.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@using Microsoft.Extensions.Options

@inject IOptions<RequestLocalizationOptions> LocalizationOptions
@inject NavigationManager Navigation

<div class="mt-3 mb-3 mx-5">
@if (LocalizationOptions.Value.SupportedCultures != null)
{
@foreach (var culture in LocalizationOptions.Value.SupportedCultures)
{
<a onclick="location.href = '@SetCulture(culture.Name)'" class="text-decoration-none">
<span class="badge rounded-pill mx-1 @(culture.ToString() == CultureInfo.CurrentCulture.ToString()?"bg-primary":"bg-primary-dark")">@culture</span>
</a>
}
}
</div>

@code
{
private string SetCulture(string culture)
{
const string cultureParamName = "culture";

var url = Navigation.GetUriWithQueryParameter(cultureParamName, culture);
return url;
}
}
5 changes: 3 additions & 2 deletions SharpSite.Web/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inherits LayoutComponentBase
@inherits LayoutComponentBase
@inject IStringLocalizer<SharedResource> Localizer

<div class="page">
<div class="sidebar">
Expand All @@ -14,7 +15,7 @@
</div>

<div id="blazor-error-ui">
An unhandled error has occurred.
@Localizer["sharpsite_unhandled_exception"]
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
11 changes: 6 additions & 5 deletions SharpSite.Web/Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<span class="bi bi-house-door-fill" aria-hidden="true"></span>Posts admin
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="/aboutSharpSite" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill" aria-hidden="true"></span>About SharpSite
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="/aboutSharpSite" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill" aria-hidden="true"></span>About SharpSite
</NavLink>
</div>
<LanguagePicker />
</nav>
</div>
7 changes: 4 additions & 3 deletions SharpSite.Web/Components/Pages/About.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@page "/aboutSharpSite"
@inject IStringLocalizer<SharedResource> Localizer

<h1>About SharpSite</h1>
<h1>@Localizer["sharpsite_about"]</h1>

<p>SharpSite is a simple blog engine written in C# using Blazor.</p>
<p>@Localizer["sharpsite_about_description"]</p>

<h2>Packages Used</h2>
<h2>@Localizer["sharpsite_about_packages"]</h2>

<ul>
<li><a href="https://www.nuget.org/packages/Markdig/">Markdig</a></li>
Expand Down
5 changes: 3 additions & 2 deletions SharpSite.Web/Components/Pages/PostPage.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/{urldate:int}/{slug}"
@using Markdig
@inject IPostRepository PostService
@inject IStringLocalizer<SharedResource> Localizer

@if (Post is not null)
{
Expand All @@ -10,11 +11,11 @@
}
else
{
<p>Post not found</p>
<p>@Localizer["sharpsite_pagenotfound"]</p>
}

<hr />
<a href="/">Back to home</a>
<a href="/">@Localizer["sharpsite_backtohome"]</a>

@code {
[Parameter] public int UrlDate { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion SharpSite.Web/Components/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
@using Microsoft.JSInterop
@using SharpSite.Web
@using SharpSite.Web.Components
@using SharpSite.Abstractions
@using SharpSite.Abstractions
@using System.Globalization
@using Microsoft.Extensions.Localization
Loading

0 comments on commit 9c961c2

Please sign in to comment.