markjprice / cs10dotnet6

Repository for the Packt Publishing book titled "C# 10 and .NET 6 - Modern Cross-Platform Development" by Mark J. Price
854 stars 374 forks source link

Text Translation #71

Open HoshyarKarimi opened 2 years ago

HoshyarKarimi commented 2 years ago

Can you please add text translation from one language to another for Chapter 8? Also do you know any terminal for Windows which supports Persian/Arabic characters? I tested fa-IR ISO code. It worked. It exactly converted Jalali date to minutes for in page 364, but instead of Persian chars it shows ???? chars. I know this is not your problem nor C# or .NET's problem. Just terminal can't show Persian chars.

markjprice commented 2 years ago

Hi,

Thank you for the feedback. In the next edition, I have expanded the internationalization topic into its own chapter with the date handling section and to include localization. Based on your feedback I have also added fa-IR as an example culture for the reader to try.

The console app now uses the following code, and note the state to set OutputEncoding to support Farsi:

using Microsoft.Extensions.Hosting; // IHost, Host
// AddLocalization, AddTransient<T>
using Microsoft.Extensions.DependencyInjection;

// to enable special characters like €
OutputEncoding = System.Text.Encoding.Unicode;

using IHost host = Host.CreateDefaultBuilder(args)
  .ConfigureServices(services =>
  {
    services.AddLocalization(options =>
    {
      options.ResourcesPath = "Resources";
    });

    services.AddTransient<PacktResources>();
  })
  .Build();

OutputCultures("Default cultures");

WriteLine("en-US: English (United States) - default");
WriteLine("da-DK: Danish (Denmark)");
WriteLine("fr-CA: French (Canada)");
WriteLine("fa-IR: Persian (Iran)");
WriteLine();

Write("Enter an ISO culture code: ");
string? cultureCode = ReadLine();

if (string.IsNullOrWhiteSpace(cultureCode))
{
  cultureCode = "en-US";
}

CultureInfo ci;

try
{
  ci = CultureInfo.GetCultureInfo(cultureCode);
}
catch (CultureNotFoundException)
{
  WriteLine($"Culture code not found: {cultureCode}");
  return;
}

// change the current cultures on the thread
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;

OutputCultures("After changing cultures");

PacktResources resources =
    host.Services.GetRequiredService<PacktResources>();

Write(resources.GetEnterYourNamePrompt());
string? name = ReadLine();
if (string.IsNullOrWhiteSpace(name))
{
  name = "Bob";
}

Write(resources.GetEnterYourDobPrompt());
string? dobText = ReadLine();

if (string.IsNullOrWhiteSpace(dobText))
{
  dobText = "1/2/1990";
}

Write(resources.GetEnterYourSalaryPrompt());
string? salaryText = ReadLine();

if (string.IsNullOrWhiteSpace(salaryText))
{
  salaryText = "34500";
}

DateTime dob = DateTime.Parse(dobText);
int minutes = (int)DateTime.Today.Subtract(dob).TotalMinutes;
decimal salary = decimal.Parse(salaryText);

WriteLine(resources.GetPersonDetails(name, dob, minutes, salary));

Here's the output:

*
* Default cultures
*
The current globalization culture is en-GB: English (United Kingdom)
The current localization culture is en-GB: English (United Kingdom)
Days of the week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
Number group separator: ,
Number decimal separator: .

en-US: English (United States) - default
da-DK: Danish (Denmark)
fr-CA: French (Canada)
fa-IR: Persian (Iran)

Enter an ISO culture code: fa-IR
*
* After changing cultures
*
The current globalization culture is fa-IR: فارسی (ایران)
The current localization culture is fa-IR: فارسی (ایران)
Days of the week: یکشنبه, دوشنبه, سه‌شنبه, چهارشنبه, پنجشنبه, جمعه, شنبه
Number group separator: ٬
Number decimal separator: ٫

اسمت را وارد کن / Enter your name: Bob
تاریخ تولد خود را وارد کنید / Enter your date of birth: 1972/1/26
حقوق خود را وارد کنید / Enter your salary: 45000
Bob در 1972/1/26 0:00:00 متولد شد، ‎−30025

0080 دقیقه سن دارد و 45000 درآمد دارد.

fa-IR-example

markjprice commented 2 years ago

The full project including resource files is here: https://github.com/markjprice/apps-services-net7/tree/main/vs4win/Chapter02