Skip to content

Commit ca461ca

Browse files
Merge pull request #3481 from azchohfi/user/azchohfi/fixLocalization
Small fix when Independent loader might throw.
2 parents e3c08ac + 817253b commit ca461ca

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Microsoft.Toolkit.Uwp/Extensions/StringExtensions.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ namespace Microsoft.Toolkit.Uwp.Extensions
1414
/// </summary>
1515
public static class StringExtensions
1616
{
17-
private static readonly ResourceLoader IndependentLoader = ResourceLoader.GetForViewIndependentUse();
17+
private static readonly ResourceLoader IndependentLoader;
18+
19+
static StringExtensions()
20+
{
21+
try
22+
{
23+
IndependentLoader = ResourceLoader.GetForViewIndependentUse();
24+
}
25+
catch
26+
{
27+
}
28+
}
1829

1930
/// <summary>
2031
/// Retrieves the provided resource for the current view context.
@@ -52,7 +63,7 @@ public static string GetLocalized(this string resourceKey, UIContext uiContext =
5263
}
5364
else
5465
{
55-
return IndependentLoader.GetString(resourceKey);
66+
return IndependentLoader?.GetString(resourceKey);
5667
}
5768
}
5869

@@ -65,7 +76,7 @@ public static string GetLocalized(this string resourceKey, UIContext uiContext =
6576
public static string GetLocalized(this string resourceKey, string resourcePath)
6677
{
6778
// Try and retrieve resource at app level first.
68-
var result = IndependentLoader.GetString(resourceKey);
79+
var result = IndependentLoader?.GetString(resourceKey);
6980

7081
if (string.IsNullOrEmpty(result))
7182
{

0 commit comments

Comments
 (0)