Implements LocalizationStringUtil.GetLocalizedString

This commit is contained in:
Yohan Guerin 2019-05-24 14:58:19 -04:00
commit a64e9b482a

View file

@ -1,6 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CalculatorApp namespace CalculatorApp
{ {
namespace Common namespace Common
@ -9,24 +14,33 @@ namespace CalculatorApp
{ {
public static string GetLocalizedString(string pMessage, params object[] args) public static string GetLocalizedString(string pMessage, params object[] args)
{ {
// UNO TODO var stringBuilder = new StringBuilder(pMessage);
//string returnString = ""; // This will capture all %<number> strings
//const uint length = 1024; var matches = Regex
//std.unique_ptr<wchar_t[]> spBuffer = std.unique_ptr<wchar_t[]>(new wchar_t[length]); .Matches(pMessage, @"%(\d+)")
//va_list args = NULL; .AsEnumerable()
//va_start(args, pMessage); .OrderBy(str => str.Value.Replace("%", ""));
//int fmtReturnVal = FormatMessage(FORMAT_MESSAGE_FROM_STRING, pMessage, 0, 0, spBuffer.get(), length, &args);
//va_end(args);
//if (fmtReturnVal != 0) // If our starting index begin at 1, we will decrease all of them in order to use string.Format
//{ if (matches.FirstOrDefault()?.Value == "%1")
// returnString = spBuffer.get(); {
//} var addedCharacterFromOriginal = 0;
//return returnString; foreach (var match in matches)
{
if(int.TryParse(match.Value.Replace("%", ""), out var argumentIndex))
{
stringBuilder.Remove(match.Index + addedCharacterFromOriginal, match.Length);
stringBuilder.Insert(match.Index + addedCharacterFromOriginal, "{" + (argumentIndex - 1).ToString() + "}");
// Since we are replace %<number> by {<number>}, we will add one character for each iteration
// so we need to consider that
addedCharacterFromOriginal++;
}
}
}
return $"[{pMessage}]"; return $"{string.Format(stringBuilder.ToString(), args)}";
} }
public static string GetLocalizedNarratorAnnouncement(string resourceKey, string formatVariable, params object[] args) public static string GetLocalizedNarratorAnnouncement(string resourceKey, string formatVariable, params object[] args)
@ -50,6 +64,19 @@ namespace CalculatorApp
formatVariable = AppResourceProvider.GetInstance().GetResourceString(resourceKey); formatVariable = AppResourceProvider.GetInstance().GetResourceString(resourceKey);
} }
static IEnumerable<Match> AsEnumerable(this MatchCollection source)
{
var enumerator = source.GetEnumerator();
var returnedEnum = new List<Match>();
while (enumerator.MoveNext())
{
returnedEnum.Add((Match)enumerator.Current);
}
return returnedEnum;
}
} }
} }
} }