Skip to content

Commit c0a96d0

Browse files
Merge pull request #1483 from Unity-Technologies/uwp-culture-info-fix
Fix CultureInfo implementation when running as UWP application.
2 parents 5384fae + 4ad95d9 commit c0a96d0

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Permission is hereby granted, free of charge, to any person obtaining
2+
// a copy of this software and associated documentation files (the
3+
// "Software"), to deal in the Software without restriction, including
4+
// without limitation the rights to use, copy, modify, merge, publish,
5+
// distribute, sublicense, and/or sell copies of the Software, and to
6+
// permit persons to whom the Software is furnished to do so, subject to
7+
// the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be
10+
// included in all copies or substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
//
20+
21+
using System;
22+
23+
namespace Mono.Interop
24+
{
25+
// Internal in order to not conflict with other definitions
26+
[AttributeUsage (AttributeTargets.Method)]
27+
internal sealed class MonoPInvokeCallbackAttribute : Attribute
28+
{
29+
public MonoPInvokeCallbackAttribute (Type t)
30+
{
31+
}
32+
}
33+
}

mcs/class/corlib/System.Globalization/CultureInfo.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
//
3232

33+
using Mono.Interop;
3334
using System.Collections.Generic;
3435
using System.Threading;
3536
using System.Runtime.CompilerServices;
@@ -1123,6 +1124,39 @@ internal static CultureInfo UserDefaultCulture {
11231124
}
11241125
}
11251126

1127+
static CultureInfo s_UserPreferredCultureInfoInAppX;
1128+
1129+
delegate void OnCultureInfoChangedDelegate ([MarshalAs(UnmanagedType.LPWStr)] string language);
1130+
1131+
[DllImport("__Internal")]
1132+
static extern void InitializeUserPreferredCultureInfoInAppX (OnCultureInfoChangedDelegate onCultureInfoChangedInAppX);
1133+
1134+
[DllImport("__Internal")]
1135+
static extern void SetUserPreferredCultureInfoInAppX ([MarshalAs(UnmanagedType.LPWStr)] string name);
1136+
1137+
[MonoPInvokeCallback (typeof(OnCultureInfoChangedDelegate))]
1138+
static void OnCultureInfoChangedInAppX ([MarshalAs(UnmanagedType.LPWStr)] string language)
1139+
{
1140+
if (language != null) {
1141+
s_UserPreferredCultureInfoInAppX = new CultureInfo(language);
1142+
} else {
1143+
s_UserPreferredCultureInfoInAppX = null;
1144+
}
1145+
}
1146+
1147+
internal static CultureInfo GetCultureInfoForUserPreferredLanguageInAppX ()
1148+
{
1149+
if (s_UserPreferredCultureInfoInAppX == null)
1150+
InitializeUserPreferredCultureInfoInAppX (OnCultureInfoChangedInAppX);
1151+
1152+
return s_UserPreferredCultureInfoInAppX;
1153+
}
1154+
1155+
internal static void SetCultureInfoForUserPreferredLanguageInAppX (CultureInfo cultureInfo)
1156+
{
1157+
SetUserPreferredCultureInfoInAppX (cultureInfo.Name);
1158+
s_UserPreferredCultureInfoInAppX = cultureInfo;
1159+
}
11261160

11271161
#region reference sources
11281162
// TODO:

mcs/class/corlib/corlib.dll.sources

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Mono/DataConverter.cs
4141
Mono.Interop/ComInteropProxy.cs
4242
Mono.Interop/IDispatch.cs
4343
Mono.Interop/IUnknown.cs
44+
Mono.Interop/MonoPInvokeCallbackAttribute.cs
4445
../Mono.Security/Mono.Math/BigInteger.cs
4546
../Mono.Security/Mono.Math.Prime/ConfidenceFactor.cs
4647
../Mono.Security/Mono.Math.Prime/PrimalityTests.cs

mcs/class/referencesource/mscorlib/system/threading/synchronizationcontext.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace System.Threading
1818
{
1919
#if !MONO
2020
using Microsoft.Win32.SafeHandles;
21+
#else
22+
using Mono.Interop;
2123
#endif
2224
using System.Security.Permissions;
2325
using System.Runtime.InteropServices;
@@ -437,14 +439,6 @@ private static void InvocationEntry(IntPtr arg)
437439
}
438440
}
439441

440-
[AttributeUsage (AttributeTargets.Method)]
441-
sealed class MonoPInvokeCallbackAttribute : Attribute
442-
{
443-
public MonoPInvokeCallbackAttribute(Type t)
444-
{
445-
}
446-
}
447-
448442
class InvocationContext
449443
{
450444
private SendOrPostCallback m_Delegate;

mcs/class/referencesource/mscorlib/system/threading/thread.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,12 +1209,11 @@ public static void SetData(LocalDataStoreSlot slot, Object data)
12091209
public CultureInfo CurrentUICulture {
12101210
get {
12111211
Contract.Ensures(Contract.Result<CultureInfo>() != null);
1212-
#if FEATURE_APPX
1212+
12131213
if(AppDomain.IsAppXModel()) {
12141214
return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentUICultureNoAppX();
12151215
}
12161216
else
1217-
#endif
12181217
{
12191218
return GetCurrentUICultureNoAppX();
12201219
}
@@ -1237,6 +1236,12 @@ public CultureInfo CurrentUICulture {
12371236
// If you add more pre-conditions to this method, check to see if you also need to
12381237
// add them to CultureInfo.DefaultThreadCurrentUICulture.set.
12391238

1239+
if (AppDomain.IsAppXModel ())
1240+
{
1241+
CultureInfo.SetCultureInfoForUserPreferredLanguageInAppX (value);
1242+
return;
1243+
}
1244+
12401245
#if FEATURE_LEAK_CULTURE_INFO
12411246
if (nativeSetThreadUILocale(value.SortName) == false)
12421247
{
@@ -1335,12 +1340,10 @@ public CultureInfo CurrentCulture {
13351340
get {
13361341
Contract.Ensures(Contract.Result<CultureInfo>() != null);
13371342

1338-
#if FEATURE_APPX
13391343
if(AppDomain.IsAppXModel()) {
13401344
return CultureInfo.GetCultureInfoForUserPreferredLanguageInAppX() ?? GetCurrentCultureNoAppX();
13411345
}
13421346
else
1343-
#endif
13441347
{
13451348
return GetCurrentCultureNoAppX();
13461349
}
@@ -1359,6 +1362,12 @@ public CultureInfo CurrentCulture {
13591362
// If you add more pre-conditions to this method, check to see if you also need to
13601363
// add them to CultureInfo.DefaultThreadCurrentCulture.set.
13611364

1365+
if (AppDomain.IsAppXModel ())
1366+
{
1367+
CultureInfo.SetCultureInfoForUserPreferredLanguageInAppX (value);
1368+
return;
1369+
}
1370+
13621371
#if FEATURE_LEAK_CULTURE_INFO
13631372
//If we can't set the nativeThreadLocale, we'll just let it stay
13641373
//at whatever value it had before. This allows people who use

0 commit comments

Comments
 (0)