Skip to content

Commit

Permalink
GetLocalizedDescription and GetLocalizedName
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael de Oliveira committed Jun 7, 2024
1 parent e6ea8e8 commit e0e0d95
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gh-nuget-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Build, Test, and Publish NuGet
uses: wahinekai/actions-publish-nuget@v3.0.1
with:
version: 1.1.4
version: 1.1.5
project-path: FoodService.Models/FoodService.Models.csproj
solution-path: FoodService.Models.sln
nuget-feed-password: ${{ secrets.PACKAGES_TOKEN }}
44 changes: 43 additions & 1 deletion FoodService.Models/Entities/Product.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using FoodService.Models.Abstract;
using FoodService.Models.Enum;
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

namespace FoodService.Models.Entities
{
Expand All @@ -9,6 +12,8 @@ namespace FoodService.Models.Entities
/// </summary>
public class Product : Item
{
private static IStringLocalizer _localizer;

/// <summary>
/// Gets or sets the ID of the Product.
/// </summary>
Expand Down Expand Up @@ -38,5 +43,42 @@ public class Product : Item
/// Gets or sets the list of product ingredients associated with this product.
/// </summary>
public List<ProductIngredient>? ProductIngredients { get; set; }

/// <summary>
/// Sets the string localizer for the Product class.
/// </summary>
/// <param name="localizer">The string localizer.</param>
public static void SetLocalizer(IStringLocalizer localizer)
{
_localizer = localizer;
}

/// <summary>
/// Gets the localized name of the product based on the specified culture.
/// </summary>
/// <param name="culture">The culture to use for localization.</param>
/// <returns>The localized name of the product.</returns>
public string GetLocalizedName(CultureInfo culture)
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
var localizedName = _localizer?[$"{nameof(Product)}_{Id}_Name"] ?? base.Name;
Thread.CurrentThread.CurrentCulture = originalCulture;
return localizedName;
}

/// <summary>
/// Gets the localized description of the product based on the specified culture.
/// </summary>
/// <param name="culture">The culture to use for localization.</param>
/// <returns>The localized description of the product.</returns>
public string GetLocalizedDescription(CultureInfo culture)
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
var localizedDescription = _localizer?[$"{nameof(Product)}_{Id}_Description"] ?? base.Description;
Thread.CurrentThread.CurrentCulture = originalCulture;
return localizedDescription;
}
}
}
}
3 changes: 2 additions & 1 deletion FoodService.Models/FoodService.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PackageId>FoodService.Models</PackageId>
<Version>1.1.4</Version>
<Version>1.1.5</Version>
<Authors>FoodService</Authors>
<Company>FoodService</Company>
<PackageDescription>A library for models in FoodService project</PackageDescription>
Expand All @@ -26,6 +26,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.6" />
</ItemGroup>

</Project>

0 comments on commit e0e0d95

Please sign in to comment.