Skip to content

Commit

Permalink
Merge pull request #8 from SirJohnK/Support_Name_With_Dots
Browse files Browse the repository at this point in the history
Support name with dots
  • Loading branch information
SirJohnK committed May 8, 2023
2 parents bc5f967 + 2a864a8 commit 6b89f04
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 10 deletions.
44 changes: 44 additions & 0 deletions LocalizationResourceManager.Maui/ILocalizationResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,64 @@

namespace LocalizationResourceManager.Maui
{
/// <summary>
/// Interface for Localization Resource Manager
/// </summary>
public interface ILocalizationResourceManager
{
/// <summary>
/// Get Default / System culture.
/// </summary>
CultureInfo DefaultCulture { get; }

/// <summary>
/// Get/Set Current culture for resource manager.
/// </summary>
CultureInfo CurrentCulture { get; set; }

/// <summary>
/// Get resource text value for <see cref="CurrentCulture"/>.
/// </summary>
/// <param name="text">Resource name.</param>
/// <remarks>Will search all registered resources, in the order they were added, until first match is found!</remarks>
/// <returns>Found resource text value.</returns>
string GetValue(string text);

/// <summary>
/// Get formatted resource text value for <see cref="CurrentCulture"/> with specified parameters.
/// </summary>
/// <param name="text">Resource name.</param>
/// <param name="arguments">Parameters used when formatting resource text value.</param>
/// <remarks>
/// Uses <see cref="string.Format(string, object?[])"/> syntax.
/// Will search all registered resources, in the order they were added, until first match is found!
/// </remarks>
/// <returns>Formatted resource text value.</returns>
string GetValue(string text, params object[] arguments);

/// <summary>
/// Indexer property to Get resource text value for <see cref="CurrentCulture"/>.
/// </summary>
/// <param name="text">Resource name.</param>
/// <remarks>Will search all registered resources, in the order they were added, until first match is found!</remarks>
/// <returns>Found resource text value.</returns>
string this[string text] { get; }

/// <summary>
/// Indexer property to Get formatted resource text value for <see cref="CurrentCulture"/> with specified parameters.
/// </summary>
/// <param name="text">Resource name.</param>
/// <param name="arguments">Parameters used when formatting resource text value.</param>
/// <remarks>
/// Uses <see cref="string.Format(string, object?[])"/> syntax.
/// Will search all registered resources, in the order they were added, until first match is found!
/// </remarks>
/// <returns>Formatted resource text value.</returns>
string this[string text, params object[] arguments] { get; }

/// <summary>
/// Release All Resources for All registered resources.
/// </summary>
void ReleaseAllResources();
}
}
37 changes: 37 additions & 0 deletions LocalizationResourceManager.Maui/ILocalizationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,51 @@

namespace LocalizationResourceManager.Maui
{
/// <summary>
/// Interface for Localization Resource Manager Settings
/// </summary>
public interface ILocalizationSettings
{
/// <summary>
/// Register ResourceManager and verify access.
/// </summary>
/// <param name="resource">The ResourceManager to add</param>
/// <returns>Flag indication if ResourceManager was added successfully</returns>
bool AddResource(ResourceManager resource);

/// <summary>
/// Register file based ResourceManager and create default .resources file, if missing.
/// </summary>
/// <param name="baseName">The root name of the resource.</param>
/// <param name="resourceDir">Path to resource directory.</param>
/// <param name="usingResourceSet">Optional, Type of the ResourceSet the ResourceManager uses to construct ResourceSets.</param>
/// <returns>Flag indication if ResourceManager was added successfully</returns>
/// <exception cref="ArgumentNullException">If baseName or resourceDir is null, empty or whitespace.</exception>
bool AddFileResource(string baseName, string resourceDir, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type? usingResourceSet = null);

/// <summary>
/// Set initial/startup culture.
/// </summary>
/// <param name="culture">Culture to set</param>
/// <remarks>
/// If, <see cref="RestoreLatestCulture(bool)"/> is set to <see langword="true"/>, this will be ignored!
/// </remarks>
void InitialCulture(CultureInfo culture);

/// <summary>
/// Set if latest set culture should be restored. Default: <see langword="false"/>
/// </summary>
/// <param name="restore">Flag indicating if latest set culture should be restored</param>
/// <remarks>
/// If set to <see langword="true"/>, this will override <see cref="InitialCulture(CultureInfo)"/>!
/// </remarks>
void RestoreLatestCulture(bool restore);

/// <summary>
/// Activate support for Resource Names with Dots.
/// </summary>
/// <remarks>Dots in names will be temporarily replaced by the substitution text when handled by the <see cref="TranslateExtension"/>.</remarks>
/// <param name="substitution">Replacement text for dots in resource names.</param>
void SupportNameWithDots(string substitution = "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<RepositoryUrl>https://github.com/SirJohnK/LocalizationResourceManager.Maui</RepositoryUrl>
<PackageTags>dotnet,dotnetmaui,localization</PackageTags>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<PackageReleaseNotes>- Add AddFileResource setting for registration of file based ResourceManager and create default .resources file, if missing.
- ReleaseAllResources manager method to Release All Resources for All registered resources.</PackageReleaseNotes>
<Version>1.0.3</Version>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<PackageReleaseNotes>- Add SupportNameWithDots setting to activate support for Resource Names with Dots.
- Add and Update interface comments.</PackageReleaseNotes>
<Version>1.0.4</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'">
Expand Down
61 changes: 57 additions & 4 deletions LocalizationResourceManager.Maui/LocalizationResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class LocalizationResourceManager : ObservableObject, ILocalizationResour

private List<ResourceManager> resources = new List<ResourceManager>();

internal bool IsNameWithDotsSupported { get; private set; } = false;

internal string DotSubstitution { get; private set; } = "_";

private LocalizationResourceManager()
{
//Init
Expand Down Expand Up @@ -94,18 +98,18 @@ public bool AddFileResource(string baseName, string resourceDir, [DynamicallyAcc
/// </summary>
/// <param name="culture">Culture to set</param>
/// <remarks>
/// If, RestoreLatestCulture is set to true, this will be ignored!
/// If, <see cref="RestoreLatestCulture(bool)"/> is set to <see langword="true"/>, this will be ignored!
/// </remarks>
public void InitialCulture(CultureInfo culture) => CurrentCulture = (restoreLatestCulture ? CurrentCulture : culture);

private bool restoreLatestCulture = false;

/// <summary>
/// Set if latest set culture should be restored. Default: false
/// Set if latest set culture should be restored. Default: <see langword="false"/>
/// </summary>
/// <param name="restore">Flag indicating if latest set culture should be restored</param>
/// <remarks>
/// If set to true, this will override InitialCulture!
/// If set to <see langword="true"/>, this will override <see cref="InitialCulture(CultureInfo)"/>!
/// </remarks>
public void RestoreLatestCulture(bool restore)
{
Expand All @@ -115,27 +119,76 @@ public void RestoreLatestCulture(bool restore)
CurrentCulture = LatestCulture ?? DefaultCulture;
}

/// <summary>
/// Activate support for Resource Names with Dots.
/// </summary>
/// <remarks>Dots in names will be temporarily replaced by the substitution text when handled by the <see cref="TranslateExtension"/>.</remarks>
/// <param name="substitution">Replacement text for dots in resource names.</param>
public void SupportNameWithDots(string substitution = "_")
{
//Activate Support!
IsNameWithDotsSupported = true;
DotSubstitution = substitution;
}

#endregion ILocalizationSettings

#region ILocalizationResourceManager

/// <summary>
/// Get resource text value for <see cref="CurrentCulture"/>.
/// </summary>
/// <param name="text">Resource name.</param>
/// <remarks>Will search all registered resources, in the order they were added, until first match is found!</remarks>
/// <returns>Found resource text value.</returns>
/// <exception cref="InvalidOperationException">Will be thrown if no resources are added.</exception>
/// <exception cref="NullReferenceException">Will be thrown if no resource was found.</exception>
public string GetValue(string text)
{
//Verify Resources
if ((resources?.Count ?? 0) == 0)
throw new InvalidOperationException($"At least one resource must be added with Settings.{nameof(AddResource)}!");

//If supported, handle names with dots!
text = IsNameWithDotsSupported ? text.Replace(DotSubstitution, ".") : text;

//Attemp to get localized string with Current Culture
var value = resources?.Select(resource => resource.GetString(text, CurrentCulture)).FirstOrDefault(output => output is not null);

//Return Result
return value ?? throw new NullReferenceException($"{nameof(text)}: {text} not found!");
}

/// <summary>
/// Get formatted resource text value for <see cref="CurrentCulture"/> with specified parameters.
/// </summary>
/// <param name="text">Resource name.</param>
/// <param name="arguments">Parameters used when formatting resource text value.</param>
/// <remarks>
/// Uses <see cref="string.Format(string, object?[])"/> syntax.
/// Will search all registered resources, in the order they were added, until first match is found!
/// </remarks>
/// <returns>Formatted resource text value.</returns>
public string GetValue(string text, params object[] arguments) => string.Format(GetValue(text), arguments);

/// <summary>
/// Indexer property to Get resource text value for <see cref="CurrentCulture"/>.
/// </summary>
/// <param name="text">Resource name.</param>
/// <remarks>Will search all registered resources, in the order they were added, until first match is found!</remarks>
/// <returns>Found resource text value.</returns>
public string this[string text] => GetValue(text);

/// <summary>
/// Indexer property to Get formatted resource text value for <see cref="CurrentCulture"/> with specified parameters.
/// </summary>
/// <param name="text">Resource name.</param>
/// <param name="arguments">Parameters used when formatting resource text value.</param>
/// <remarks>
/// Uses <see cref="string.Format(string, object?[])"/> syntax.
/// Will search all registered resources, in the order they were added, until first match is found!
/// </remarks>
/// <returns>Formatted resource text value.</returns>
public string this[string text, params object[] arguments] => GetValue(text, arguments);

private CultureInfo currentCulture = CultureInfo.CurrentCulture;
Expand Down Expand Up @@ -165,7 +218,7 @@ public CultureInfo CurrentCulture
public CultureInfo DefaultCulture
{
get => CultureInfo.GetCultureInfo(DefaultCultureName);
set => Preferences.Set(nameof(DefaultCulture), value.Name);
private set => Preferences.Set(nameof(DefaultCulture), value.Name);
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion LocalizationResourceManager.Maui/TranslateExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
[ContentProperty(nameof(Text))]
public class TranslateExtension : IMarkupExtension<BindingBase>
{
public string Text { get; set; } = string.Empty;
private string text = string.Empty;

public string Text
{
get => text;
set => text = LocalizationResourceManager.Current.IsNameWithDotsSupported
? value.Replace(".", LocalizationResourceManager.Current.DotSubstitution)
: value;
}

public string? StringFormat { get; set; }

Expand Down

0 comments on commit 6b89f04

Please sign in to comment.