Skip to content

Commit

Permalink
Fixes in localization system and localized string
Browse files Browse the repository at this point in the history
  • Loading branch information
danae committed Sep 2, 2024
1 parent 154e730 commit 620743a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
6 changes: 5 additions & 1 deletion Runtime/ILocalizationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ public interface ILocalizationSystem : IMessageFunctionExecutor
// Format a localized string using the specified locale
public string Format(Locale locale, ILocalizedString reference)
{
if (locale == null)
throw new ArgumentNullException(nameof(locale));
if (reference == null)
throw new ArgumentNullException(nameof(reference));

if (!reference.TryResolve(locale.strings, out var message))
throw new LocalizationException($"String \"{reference}\" could not be found in locale {locale}");

return Format(message, reference.arguments);
return Format(locale, message, reference.arguments);
}

// Format the contents of a text asset with the specified arguments using the specified locale
public string FormatAsset(Locale locale, string path, IReadOnlyDictionary<string, object> arguments = null)
{
if (locale == null)
throw new ArgumentNullException(nameof(locale));
if (path == null)
throw new ArgumentNullException(nameof(path));

Expand Down
2 changes: 2 additions & 0 deletions Runtime/LocalizationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public bool TryExecuteFunction(string name, string argument, out string value)
// Format a message with the specified arguments using the specified locale
public string Format(Locale locale, string message, IReadOnlyDictionary<string, object> arguments = null)
{
if (locale == null)
throw new ArgumentNullException(nameof(locale));
if (message == null)
throw new ArgumentNullException(nameof(message));

Expand Down
7 changes: 7 additions & 0 deletions Runtime/References/FormattedLocalizedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public FormattedLocalizedString(ILocalizedString source, LocalizedStringFormatte
}


// Return the string representation of the localized string
public override string ToString()
{
return _source.ToString();
}


#region Localized string implementation
// Return the arguments of the localized string
public IReadOnlyDictionary<string, object> arguments => _source.arguments;
Expand Down
4 changes: 2 additions & 2 deletions Runtime/References/ILocalizedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static ILocalizedString Join(ILocalizedString separator, IEnumerable<ILoc
var actualStrings = strings.Interleave(separator);

var actualArguments = new Dictionary<string, object>();
foreach (var e in strings.SelectMany(s => s.arguments))
foreach (var e in strings.SelectMany(s => s?.arguments ?? Enumerable.Empty<KeyValuePair<string, object>>()))
{
if (!actualArguments.ContainsKey(e.Key))
actualArguments[e.Key] = e.Value;
Expand All @@ -135,7 +135,7 @@ public static ILocalizedString Concat(IEnumerable<ILocalizedString> strings)
// Return a new localized string that concatenates the specified localized strings
public static ILocalizedString Concat(params ILocalizedString[] strings)
{
return Concat(strings);
return Concat((IEnumerable<ILocalizedString>)strings);
}


Expand Down
22 changes: 21 additions & 1 deletion Runtime/References/JoinedLocalizedString.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Audune.Localization
{
Expand All @@ -19,6 +20,13 @@ public JoinedLocalizedString(IEnumerable<ILocalizedString> strings = null, IEnum
}


// Return the string representation of the localized string
public override string ToString()
{
return string.Join("", _strings);
}


#region Localized string implementation
// Return the arguments of the localized string
public IReadOnlyDictionary<string, object> arguments => _arguments;
Expand All @@ -33,7 +41,19 @@ public JoinedLocalizedString(IEnumerable<ILocalizedString> strings = null, IEnum
// Return if the localized string can be resolved and store the value
public bool TryResolve(ILocalizedStringTable table, out string value)
{
throw new System.NotImplementedException();
value = null;

var builder = new StringBuilder();
foreach (var item in _strings.Where(s => s != null))
{
if (item.TryResolve(table, out var itemValue))
builder.Append(itemValue);
else
return false;
}

value = builder.ToString();
return true;
}

// Return a new localized string with the specified argument
Expand Down
36 changes: 6 additions & 30 deletions Runtime/References/LocalizedString.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Audune.Utils.Dictionary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Audune.Localization
Expand All @@ -18,8 +16,7 @@ public class LocalizedString : ILocalizedString, IEquatable<LocalizedString>
private string _value;

// Internal state of the localized string
[SerializeField, Tooltip("The arguments of the string")]
private SerializableDictionary<string, object> _arguments;
private Dictionary<string, object> _arguments;


// Return the path of the localized string
Expand All @@ -32,27 +29,14 @@ public class LocalizedString : ILocalizedString, IEquatable<LocalizedString>
// Private constructor
internal LocalizedString(string path, string value, IEnumerable<KeyValuePair<string, object>> arguments = null)
{
_path = null;
_value = null;
_arguments = arguments != null ? new SerializableDictionary<string, object>(arguments.ToDictionary()) : new SerializableDictionary<string, object>();
_path = path;
_value = value;
_arguments = arguments != null ? new Dictionary<string, object>(arguments) : new Dictionary<string, object>();
}


// Return the string representation of the localized string
public override string ToString()
{
var builder = new StringBuilder();
if (isLocalized)
builder.Append(_path);
else
builder.Append($"<Non-Localized Value: \"{_value}\">");
if (_arguments.Count > 0)
builder.Append($" with arguments {{{string.Join(", ", _arguments.Select(e => $"{e.Key} = {e.Value}"))}}}");
return builder.ToString();
}

// Return the message representation of the localized string
public string ToMessageString()
{
return isLocalized ? $"{{={_path}}}" : _value;
}
Expand All @@ -75,7 +59,7 @@ public bool TryResolve(ILocalizedStringTable table, out string value)
if (!string.IsNullOrEmpty(_path))
return table.TryFind(_path, out value);

value = _value;
value = !string.IsNullOrEmpty(_value) ? _value : string.Empty;
return true;
}

Expand Down Expand Up @@ -136,7 +120,7 @@ public override bool Equals(object obj)
// Return if the localized string equals another localized string
public bool Equals(LocalizedString other)
{
return other is not null && _path == other._path && _value == other._value && EqualityComparer<SerializableDictionary<string, object>>.Default.Equals(_arguments, other._arguments);
return other is not null && _path == other._path && _value == other._value && EqualityComparer<Dictionary<string, object>>.Default.Equals(_arguments, other._arguments);
}

// Return the hash code of the localized string
Expand All @@ -158,13 +142,5 @@ public override int GetHashCode()
return !(left == right);
}
#endregion

#region Creating localized strings
// Create a localized string with a value using the implicit string operator
public static implicit operator LocalizedString(string value)
{
return new LocalizedString(null, value);
}
#endregion
}
}

0 comments on commit 620743a

Please sign in to comment.