Created
June 21, 2016 09:45
-
-
Save deniszykov/0af842aded2b5cd570b3bb9269ee5aef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void PreLoadResourcesFromMainAssembly() | |
{ | |
const string RESOURCES_PREFIX = "MyApp.Properties.Resources."; | |
const string RESOURCES_EXTENTION = ".resources"; | |
// get loaded resource sets from resource manager | |
var resourceManager = Resources.ResourceManager; | |
var resourceSetByCulture = | |
( | |
resourceManager.GetType().GetField("_resourceSets", BindingFlags.Instance | BindingFlags.NonPublic) ?? | |
// ReSharper disable once PossibleNullReferenceException | |
resourceManager.GetType().GetField("ResourceSets", BindingFlags.Instance | BindingFlags.NonPublic) | |
).GetValue(resourceManager) as System.Collections.IDictionary; | |
if (resourceSetByCulture == null) | |
return; | |
var resourceAssembly = typeof(Program).Assembly; // get assembly with localization resources | |
foreach (var embeddedResourceName in resourceAssembly.GetManifestResourceNames()) | |
{ | |
if (embeddedResourceName.StartsWith(RESOURCES_PREFIX, StringComparison.Ordinal) == false || | |
embeddedResourceName.EndsWith(RESOURCES_EXTENTION, StringComparison.Ordinal) == false) | |
continue; // not localized resource | |
var locale = embeddedResourceName.Substring(RESOURCES_PREFIX.Length, Math.Max(0, embeddedResourceName.Length - RESOURCES_PREFIX.Length - RESOURCES_EXTENTION.Length)); | |
if (string.IsNullOrEmpty(locale)) | |
continue; // default locale | |
var resourceStream = resourceAssembly.GetManifestResourceStream(embeddedResourceName) ; | |
if (resourceStream == null) | |
continue; // no resource stream | |
var resourceSet = new System.Resources.ResourceSet(resourceStream); | |
var culture = CultureInfo.GetCultureInfo(locale); | |
if (resourceSetByCulture is System.Collections.Hashtable) | |
resourceSetByCulture.Add(culture, resourceSet); | |
else | |
resourceSetByCulture.Add(culture.Name, resourceSet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment