To implement a Polish i18n (internationalization) in ErsatzTV, we need to follow these steps:
Step 1: Add Polish language code to the database
In the LanguageCodeConfiguration.cs file, we need to add the Polish language code to the database.
[Redacted
public class LanguageCodeConfiguration : IEntityTypeConfiguration<LanguageCode>
{
public void Configure(EntityTypeBuilder<LanguageCode> builder)
{
builder.ToTable("LanguageCode");
// Add Polish language code
builder.HasData(new LanguageCode { Code = "pl", Name = "Polish" });
}
}
Step 2: Create a Polish translation file
Create a new file called pl.json in the Resources folder. This file will contain the Polish translations for the application.
json
{
"Hello": "Cześć",
"Goodbye": "Żegnaj",
// Add more translations here
}
Step 3: Update the StringExtensions.cs file
In the StringExtensions.cs file, we need to add a method to get the translation for a given key.
[Redacted
public static class StringExtensions
{
// ...
public static string GetTranslation(this string key, string languageCode)
{
// Load the translation file for the given language code
string translationFile = $"Resources/{languageCode}.json";
string translation = File.ReadAllText(translationFile);
// Deserialize the translation file into a dictionary
Dictionary<string, string> translations = JsonConvert.DeserializeObject<Dictionary<string, string>>(translation);
// Return the translation for the given key
return translations.TryGetValue(key, out string value) ? value : key;
}
}
Step 4: Use the translation method in the application
Now we can use the GetTranslation method to get the Polish translation for a given key.
In the ImageCache.cs file, we need to update the GetPathForImage method to use the language code when generating the image path.
[Redacted
public class ImageCache : IImageCache
{
// ...
public virtual string GetPathForImage(string fileName, ArtworkKind artworkKind, Option<int> maybeMaxHeight)
{
// ...
// Use the language code when generating the image path
string languageCode = "pl"; // Get the language code from the database or configuration
string basePath = Path.Combine(FileSystemLayout.ImageCacheFolder, languageCode);
// ...
}
}
That's it! Now we have implemented Polish i18n in ErsatzTV. We can add more languages by following the same steps.