mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 22:23:29 -07:00
Merge pull request #45 from nventive/dev/yogu/implements.getlocalizedstring
Implements LocalizationStringUtil.GetLocalizedString
This commit is contained in:
commit
3942706639
1 changed files with 68 additions and 41 deletions
|
@ -1,55 +1,82 @@
|
||||||
// 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
|
||||||
{
|
{
|
||||||
public static class LocalizationStringUtil
|
public static class LocalizationStringUtil
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
EnsureInitialization(resourceKey, formatVariable);
|
EnsureInitialization(resourceKey, formatVariable);
|
||||||
return GetLocalizedString(formatVariable, args);
|
return GetLocalizedString(formatVariable, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void EnsureInitialization(string resourceKey, string formatVariable)
|
static void EnsureInitialization(string resourceKey, string formatVariable)
|
||||||
{
|
{
|
||||||
if (resourceKey == null || string.IsNullOrEmpty(resourceKey))
|
if (resourceKey == null || string.IsNullOrEmpty(resourceKey))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the formatVariable already has a value, we don't need to set it again. Simply return.
|
// If the formatVariable already has a value, we don't need to set it again. Simply return.
|
||||||
if (formatVariable != null && !string.IsNullOrEmpty(formatVariable))
|
if (formatVariable != null && !string.IsNullOrEmpty(formatVariable))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue