Skip to content

Commit

Permalink
Merge pull request #2 from SirJohnK/development
Browse files Browse the repository at this point in the history
Add AddFileResource setting and ReleaseAllResources method
  • Loading branch information
SirJohnK committed Jan 12, 2023
2 parents a193235 + 4a25a74 commit 985224e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface ILocalizationResourceManager
string this[string text] { get; }

string this[string text, params object[] arguments] { get; }

void ReleaseAllResources();
}
}
7 changes: 5 additions & 2 deletions LocalizationResourceManager.Maui/ILocalizationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Resources;

namespace LocalizationResourceManager.Maui
{
public interface ILocalizationSettings
{
void AddResource(ResourceManager resource);
bool AddResource(ResourceManager resource);

bool AddFileResource(string baseName, string resourceDir, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type? usingResourceSet = null);

void InitialCulture(CultureInfo culture);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
<Title>LocalizationResourceManager.Maui</Title>
<Authors>Johan Svensson, Charlin Agramonte, Brandon Minnick, Maksym Koshovyi</Authors>
<Description>Enhanced .NET MAUI version of the Xamarin Community Toolkit LocalizationResourceManager.</Description>
<Copyright2022 Johan Svensson All Rights Reserved</Copyright>
<Copyright2023 Johan Svensson All Rights Reserved</Copyright>
<PackageProjectUrl>https://github.com/SirJohnK/LocalizationResourceManager.Maui</PackageProjectUrl>
<PackageIcon>sirjohnk.png</PackageIcon>
<RepositoryUrl>https://github.com/SirJohnK/LocalizationResourceManager.Maui</RepositoryUrl>
<PackageTags>dotnet,dotnetmaui,localization</PackageTags>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<PackageReleaseNotes>- Add format params parameter option</PackageReleaseNotes>
<Version>1.0.2</Version>
<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>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'">
Expand Down
94 changes: 90 additions & 4 deletions LocalizationResourceManager.Maui/LocalizationResourceManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LocalizationResourceManager.Maui.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Resources;

Expand All @@ -7,7 +8,7 @@ namespace LocalizationResourceManager.Maui;
/// <summary>
/// Manager to track current resource manager and current culture.
/// </summary>
public partial class LocalizationResourceManager : ObservableObject, ILocalizationResourceManager, ILocalizationSettings
public class LocalizationResourceManager : ObservableObject, ILocalizationResourceManager, ILocalizationSettings
{
private static readonly Lazy<LocalizationResourceManager> currentHolder = new(() => new LocalizationResourceManager());

Expand All @@ -23,12 +24,89 @@ private LocalizationResourceManager()

#region ILocalizationSettings

public void AddResource(ResourceManager resource) => resources.Add(resource);
/// <summary>
/// Register ResourceManager and verify access.
/// </summary>
/// <param name="resource">The ResourceManager to add</param>
/// <returns>Flag indication if ResourceManager was added successfully</returns>
public bool AddResource(ResourceManager resource)
{
try
{
//Verify access by attempting to get empty string
resource.GetString(string.Empty);

//Access attempt was successful!
resources.Add(resource);

//Return successful status
return true;
}
catch (Exception)
{
//Access attempt was not successful!
return false;
}
}

/// <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>
public bool AddFileResource(string baseName, string resourceDir, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type? usingResourceSet = null)
{
//Verify parameters
if (string.IsNullOrWhiteSpace(baseName)) throw new ArgumentNullException(nameof(baseName));
if (string.IsNullOrWhiteSpace(resourceDir)) throw new ArgumentNullException(nameof(resourceDir));

try
{
//Init
var resourceFileName = Path.Combine(resourceDir, $"{baseName}.resources");

//Check if default .resources file exits, otherwise create it!
if (!File.Exists(resourceFileName))
{
//Attempt to create default .resources file!
using (var writer = new ResourceWriter(resourceFileName))
{
//Add default empty string resource
writer.AddResource(string.Empty, string.Empty);
}
}

//Create and register file based ResourceManager
return AddResource(ResourceManager.CreateFileBasedResourceManager(baseName, resourceDir, usingResourceSet));
}
catch (Exception)
{
//Registration of file resource failed!
return false;
}
}

/// <summary>
/// Set initial/startup culture.
/// </summary>
/// <param name="culture">Culture to set</param>
/// <remarks>
/// If, RestoreLatestCulture is set to 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
/// </summary>
/// <param name="restore">Flag indicating if latest set culture should be restored</param>
/// <remarks>
/// If set to true, this will override InitialCulture!
/// </remarks>
public void RestoreLatestCulture(bool restore)
{
//Set state and Update Current Culture
Expand All @@ -39,6 +117,8 @@ public void RestoreLatestCulture(bool restore)

#endregion ILocalizationSettings

#region ILocalizationResourceManager

public string GetValue(string text)
{
//Verify Resources
Expand All @@ -52,8 +132,6 @@ public string GetValue(string text)
return value ?? throw new NullReferenceException($"{nameof(text)}: {text} not found!");
}

#region ILocalizationResourceManager

public string GetValue(string text, params object[] arguments) => string.Format(GetValue(text), arguments);

public string this[string text] => GetValue(text);
Expand Down Expand Up @@ -90,6 +168,14 @@ public CultureInfo DefaultCulture
set => Preferences.Set(nameof(DefaultCulture), value.Name);
}

/// <summary>
/// Release All Resources for All registered resources.
/// </summary>
public void ReleaseAllResources()
{
resources?.ForEach(resource => resource?.ReleaseAllResources());
}

#endregion ILocalizationResourceManager

private string LatestCultureName => Preferences.Get(nameof(LatestCulture), DefaultCulture.Name);
Expand Down

0 comments on commit 985224e

Please sign in to comment.