Skip to content

Commit

Permalink
Fixed: Localization for two part language dialects
Browse files Browse the repository at this point in the history
Fixes #999
  • Loading branch information
Qstick committed May 13, 2022
1 parent 5207527 commit 166038a
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/NzbDrone.Core/Localization/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public class LocalizationService : ILocalizationService, IHandleAsync<ConfigSave

public Dictionary<string, string> GetLocalizationDictionary()
{
var language = IsoLanguages.Get((Language)_configService.UILanguage).TwoLetterCode;
var language = GetSetLanguageFileName();

return GetLocalizationDictionary(language);
}

public string GetLocalizedString(string phrase)
{
var language = IsoLanguages.Get((Language)_configService.UILanguage).TwoLetterCode;
var language = GetSetLanguageFileName();

return GetLocalizedString(phrase, language);
}
Expand All @@ -66,7 +66,7 @@ public string GetLocalizedString(string phrase, string language)

if (language.IsNullOrWhiteSpace())
{
language = IsoLanguages.Get((Language)_configService.UILanguage).TwoLetterCode;
language = GetSetLanguageFileName();
}

if (language == null)
Expand All @@ -84,6 +84,19 @@ public string GetLocalizedString(string phrase, string language)
return phrase;
}

private string GetSetLanguageFileName()
{
var isoLanguage = IsoLanguages.Get((Language)_configService.UILanguage);
var language = isoLanguage.TwoLetterCode;

if (isoLanguage.CountryCode.IsNotNullOrWhiteSpace())
{
language = string.Format("{0}_{1}", language, isoLanguage.CountryCode);
}

return language;
}

private Dictionary<string, string> GetLocalizationDictionary(string language)
{
if (string.IsNullOrEmpty(language))
Expand All @@ -109,9 +122,17 @@ public string GetLocalizedString(string phrase, string language)
var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var baseFilenamePath = Path.Combine(prefix, baseFilename);

var alternativeFilenamePath = Path.Combine(prefix, GetResourceFilename(culture));

await CopyInto(dictionary, baseFilenamePath).ConfigureAwait(false);

if (culture.Contains("_"))
{
var languageBaseFilenamePath = Path.Combine(prefix, GetResourceFilename(culture.Split('_')[0]));
await CopyInto(dictionary, languageBaseFilenamePath).ConfigureAwait(false);
}

await CopyInto(dictionary, alternativeFilenamePath).ConfigureAwait(false);

return dictionary;
Expand Down Expand Up @@ -145,11 +166,11 @@ private async Task CopyInto(IDictionary<string, string> dictionary, string resou

private static string GetResourceFilename(string culture)
{
var parts = culture.Split('-');
var parts = culture.Split('_');

if (parts.Length == 2)
{
culture = parts[0].ToLowerInvariant() + "-" + parts[1].ToUpperInvariant();
culture = parts[0].ToLowerInvariant() + "_" + parts[1].ToUpperInvariant();
}
else
{
Expand Down

0 comments on commit 166038a

Please sign in to comment.