mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 22:23:29 -07:00
Implements LocalizationStringUtil.GetLocalizedString
This commit is contained in:
parent
9bf438baf3
commit
a64e9b482a
1 changed files with 68 additions and 41 deletions
|
@ -1,55 +1,82 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace CalculatorApp
|
||||
{
|
||||
namespace Common
|
||||
{
|
||||
public static class LocalizationStringUtil
|
||||
{
|
||||
public static string GetLocalizedString(string pMessage, params object[] args)
|
||||
{
|
||||
// UNO TODO
|
||||
namespace Common
|
||||
{
|
||||
public static class LocalizationStringUtil
|
||||
{
|
||||
public static string GetLocalizedString(string pMessage, params object[] args)
|
||||
{
|
||||
var stringBuilder = new StringBuilder(pMessage);
|
||||
|
||||
//string returnString = "";
|
||||
//const uint length = 1024;
|
||||
//std.unique_ptr<wchar_t[]> spBuffer = std.unique_ptr<wchar_t[]>(new wchar_t[length]);
|
||||
//va_list args = NULL;
|
||||
//va_start(args, pMessage);
|
||||
//int fmtReturnVal = FormatMessage(FORMAT_MESSAGE_FROM_STRING, pMessage, 0, 0, spBuffer.get(), length, &args);
|
||||
//va_end(args);
|
||||
// This will capture all %<number> strings
|
||||
var matches = Regex
|
||||
.Matches(pMessage, @"%(\d+)")
|
||||
.AsEnumerable()
|
||||
.OrderBy(str => str.Value.Replace("%", ""));
|
||||
|
||||
//if (fmtReturnVal != 0)
|
||||
//{
|
||||
// returnString = spBuffer.get();
|
||||
//}
|
||||
// If our starting index begin at 1, we will decrease all of them in order to use string.Format
|
||||
if (matches.FirstOrDefault()?.Value == "%1")
|
||||
{
|
||||
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)
|
||||
{
|
||||
EnsureInitialization(resourceKey, formatVariable);
|
||||
return GetLocalizedString(formatVariable, args);
|
||||
}
|
||||
public static string GetLocalizedNarratorAnnouncement(string resourceKey, string formatVariable, params object[] args)
|
||||
{
|
||||
EnsureInitialization(resourceKey, formatVariable);
|
||||
return GetLocalizedString(formatVariable, args);
|
||||
}
|
||||
|
||||
static void EnsureInitialization(string resourceKey, string formatVariable)
|
||||
{
|
||||
if (resourceKey == null || string.IsNullOrEmpty(resourceKey))
|
||||
{
|
||||
return;
|
||||
}
|
||||
static void EnsureInitialization(string resourceKey, string formatVariable)
|
||||
{
|
||||
if (resourceKey == null || string.IsNullOrEmpty(resourceKey))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// If the formatVariable already has a value, we don't need to set it again. Simply return.
|
||||
if (formatVariable != null && !string.IsNullOrEmpty(formatVariable))
|
||||
{
|
||||
return;
|
||||
}
|
||||
// If the formatVariable already has a value, we don't need to set it again. Simply return.
|
||||
if (formatVariable != null && !string.IsNullOrEmpty(formatVariable))
|
||||
{
|
||||
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