Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Dec 14, 2024
1 parent 7a3b976 commit 89330e1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Globalization;
using System.Threading.Tasks;

namespace OrchardCore.Localization;

Expand All @@ -9,14 +7,6 @@ namespace OrchardCore.Localization;
/// </summary>
public interface ILocalizationManager
{
/// <summary>
/// Retrieves a dictionary for a specified culture.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/>.</param>
/// <returns>A <see cref="CultureDictionary"/> for the specified culture.</returns>
[Obsolete("This method has been deprecated, please use GetDictionaryAsync instead.")]
CultureDictionary GetDictionary(CultureInfo culture) => GetDictionaryAsync(culture).GetAwaiter().GetResult();

/// <summary>
/// Retrieves a dictionary for a specified culture.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Localization;
Expand All @@ -15,15 +13,5 @@ public interface IPluralStringLocalizer : IStringLocalizer
/// <param name="name">The resource name.</param>
/// <param name="arguments">Optional parameters that can be used inside the resource key.</param>
/// <returns>A list of localized strings including the plural forms.</returns>
[Obsolete("This method is deprecated, please use GetTranslationAsync instead.")]
(LocalizedString, object[]) GetTranslation(string name, params object[] arguments)
=> GetTranslationAsync(name, arguments).GetAwaiter().GetResult();

/// <summary>
/// Gets the localized strings.
/// </summary>
/// <param name="name">The resource name.</param>
/// <param name="arguments">Optional parameters that can be used inside the resource key.</param>
/// <returns>A list of localized strings including the plural forms.</returns>
Task<(LocalizedString, object[])> GetTranslationAsync(string name, params object[] arguments);
(LocalizedString, object[]) GetTranslation(string name, params object[] arguments);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
using System;
using System.Threading.Tasks;

namespace OrchardCore.Localization;

/// <summary>
/// Contract to provide a translations.
/// </summary>
public interface ITranslationProvider
{
/// <summary>
/// Loads translations from a certain source for a specific culture.
/// </summary>
/// <param name="cultureName">The culture name.</param>
/// <param name="dictionary">The <see cref="CultureDictionary"/> that will contains all loaded translations.</param>
[Obsolete("This method has been deprecated, please use LoadTranslationsAsync instead.")]
void LoadTranslations(string cultureName, CultureDictionary dictionary)
=> LoadTranslationsAsync(cultureName, dictionary).GetAwaiter().GetResult();

/// <summary>
/// Loads translations from a certain source for a specific culture.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

namespace OrchardCore.Localization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;

namespace OrchardCore.Localization.PortableObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Text;

namespace OrchardCore.Localization.PortableObject;
Expand All @@ -25,14 +20,6 @@ static PoParser()
}.ToFrozenDictionary();
}

/// <summary>
/// Parses a .po file.
/// </summary>
/// <param name="reader">The <see cref="TextReader"/>.</param>
/// <returns>A list of culture records.</returns>
[Obsolete("This methos has been deprecated, please use ParseAsync instead.")]
public IEnumerable<CultureDictionaryRecord> Parse(TextReader reader) => ParseAsync(reader).ToEnumerable();

/// <summary>
/// Parses a .po file.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,8 @@ public override LocalizedHtmlString this[string name]
// Get an unformatted string and all non plural arguments (1st one is the plural count).
var (translation, argumentsWithCount) = pluralLocalizer.GetTranslation(name, arguments);

// See https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNetCore.Mvc.Localization/LocalizedHtmlString.cs#L97

// But with a plural localizer, arguments may be provided for plural localization. So, we
// still use them to get a non formatted translation and extract all non plural arguments.

// Otherwise an already formatted string containing curly braces will be wrongly reformatted.

if (_localizer is IPluralStringLocalizer pluralLocalizer && arguments.Length == 1 && arguments[0] is PluralizationArgument)
{
// Get an unformatted string and all non plural arguments (1st one is the plural count).
var (translation, argumentsWithCount) = pluralLocalizer.GetTranslationAsync(name, arguments).GetAwaiter().GetResult();

// Formatting will use non plural arguments if any.
return ToHtmlString(translation, argumentsWithCount);
}

return ToHtmlString(_localizer[name], arguments);
// Formatting will use non plural arguments if any.
return ToHtmlString(translation, argumentsWithCount);
}

return ToHtmlString(_localizer[name], arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.Localization.DataAnnotations;
Expand Down Expand Up @@ -46,7 +44,7 @@ public virtual LocalizedString this[string name]
{
ArgumentNullException.ThrowIfNull(name);

var translation = GetTranslationAsync(name, _context, CultureInfo.CurrentUICulture, null).GetAwaiter().GetResult();
var translation = GetTranslation(name, _context, CultureInfo.CurrentUICulture, null);

return new LocalizedString(name, translation ?? name, translation == null);
}
Expand All @@ -57,7 +55,7 @@ public virtual LocalizedString this[string name]
{
get
{
var (translation, argumentsWithCount) = GetTranslationAsync(name, arguments).GetAwaiter().GetResult();
var (translation, argumentsWithCount) = GetTranslation(name, arguments);
var formatted = string.Format(translation.Value, argumentsWithCount);

return new LocalizedString(name, formatted, translation.ResourceNotFound);
Expand All @@ -76,14 +74,14 @@ public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCult
}

/// <inheritdocs />
public virtual async Task<(LocalizedString, object[])> GetTranslationAsync(string name, params object[] arguments)
public virtual (LocalizedString, object[]) GetTranslation(string name, params object[] arguments)
{
ArgumentNullException.ThrowIfNull(name);

// Check if a plural form is called, which is when the only argument is of type PluralizationArgument.
if (arguments.Length == 1 && arguments[0] is PluralizationArgument pluralArgument)
{
var translation = await GetTranslationAsync(name, _context, CultureInfo.CurrentUICulture, pluralArgument.Count);
var translation = GetTranslation(name, _context, CultureInfo.CurrentUICulture, pluralArgument.Count);

object[] argumentsWithCount;

Expand All @@ -98,7 +96,7 @@ public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCult
argumentsWithCount = [pluralArgument.Count];
}

translation ??= await GetTranslationAsync(pluralArgument.Forms, CultureInfo.CurrentUICulture, pluralArgument.Count);
translation ??= GetTranslation(pluralArgument.Forms, CultureInfo.CurrentUICulture, pluralArgument.Count);

return (new LocalizedString(name, translation, translation == null), argumentsWithCount);
}
Expand Down Expand Up @@ -145,13 +143,9 @@ private async IAsyncEnumerable<LocalizedString> GetAllStringsFromCultureHierarch
} while (currentCulture != currentCulture.Parent);
}

[Obsolete("This method is deprecated, please use GetTranslationAsync instead.")]
protected string GetTranslation(string[] pluralForms, CultureInfo culture, int? count)
=> GetTranslationAsync(pluralForms, culture, count).GetAwaiter().GetResult();

protected async Task<string> GetTranslationAsync(string[] pluralForms, CultureInfo culture, int? count)
{
var dictionary = await _localizationManager.GetDictionaryAsync(culture);
var dictionary = _localizationManager.GetDictionaryAsync(culture).GetAwaiter().GetResult();

var pluralForm = count.HasValue ? dictionary.PluralRule(count.Value) : 0;

Expand All @@ -169,11 +163,7 @@ protected async Task<string> GetTranslationAsync(string[] pluralForms, CultureIn
return pluralForms[pluralForm];
}

[Obsolete("This method has been deprecated please use instead.")]
protected string GetTranslation(string name, string context, CultureInfo culture, int? count)
=> GetTranslationAsync(name, context, culture, count).GetAwaiter().GetResult();

protected async Task<string> GetTranslationAsync(string name, string context, CultureInfo culture, int? count)
{
string translation = null;
try
Expand All @@ -182,7 +172,7 @@ protected async Task<string> GetTranslationAsync(string name, string context, Cu
{
do
{
if (await ExtractTranslationAsync() != null)
if (ExtractTranslation() != null)
{
break;
}
Expand All @@ -193,12 +183,12 @@ protected async Task<string> GetTranslationAsync(string name, string context, Cu
}
else
{
await ExtractTranslationAsync();
ExtractTranslation();
}

async Task< string> ExtractTranslationAsync()
string ExtractTranslation()
{
var dictionary = await _localizationManager.GetDictionaryAsync(culture);
var dictionary = _localizationManager.GetDictionaryAsync(culture).GetAwaiter().GetResult();

if (dictionary != null)
{
Expand Down
Loading

0 comments on commit 89330e1

Please sign in to comment.