internal static void Register(Assembly assembly) { try { Logger.Debug(String.Format("Looking for resources in assembly '{0}'", assembly.FullName)); var assemblyName = assembly.GetName().Name; Logger.Log(Level.Debug, "{0}: Looking for XML-resources...", assemblyName); string[] xmlFiles = assembly .GetManifestResourceNames() .Where(r => r.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)) .ToArray(); if (!xmlFiles.Any()) { Logger.Log(Level.Debug, "{0}: No XML-resources found.", assemblyName); return; } ProviderBasedLocalizationService providerService = ServiceLocator.Current.GetInstance <LocalizationService>() as ProviderBasedLocalizationService; if (providerService == null) { Logger.Log(Level.Warning, "{0}: Could not load ProviderBasedLocalizationService.", assemblyName); return; } var provider = new XmlLocalizationProvider(); provider.Initialize("XML-provider for " + assemblyName, new NameValueCollection()); foreach (string xmlResource in xmlFiles) { Logger.Log(Level.Debug, "{0}: Loading resource '{1}'", assemblyName, xmlResource); provider.Load(assembly.GetManifestResourceStream(xmlResource)); } providerService.Providers.Add(provider); } catch (Exception ex) { Logger.Log(Level.Error, "Failed to register localization", ex); } }
public void Initialize(InitializationEngine context) { if (context.Locate.Advanced.GetInstance <LocalizationService>() is ProviderBasedLocalizationService localizationService) { var ass = Assembly.GetAssembly(typeof(LocalizationInitialization)); string[] xmlResources = ass.GetManifestResourceNames() .Where(r => r.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)) .ToArray(); foreach (string name in xmlResources) { Stream stream = ass.GetManifestResourceStream(name); var provider = new XmlLocalizationProvider(); provider.Initialize(name, null); provider.Load(stream); localizationService.AddProvider(provider); } } }
/// <summary> /// Generates code for all XML files in specified directory. Code file will have a specified namespace and root class will have a specified name /// </summary> /// <param name="sourceDirectory">Folder to look translation files at</param> /// <param name="rootNamespace">Generated code file namespace</param> /// <param name="rootClassName">Generated code file root class name</param> public void Generate(string sourceDirectory, string rootNamespace, string rootClassName) { if (!Directory.Exists(sourceDirectory)) { this.WriteLine( $"// Directory not found: {sourceDirectory}. Please provide a directory that contains XML translation files."); return; } var directoryInfo = new DirectoryInfo(sourceDirectory); var fileInfos = directoryInfo.GetFiles("*.xml"); if (fileInfos.Length <= 0) { this.WriteLine( $"// Specified directory doesn't contain translation files: {sourceDirectory}. Please provide a directory that contains XML translation files."); return; } var sw = Stopwatch.StartNew(); var provider = new XmlLocalizationProvider(); // Load all resources files from directory foreach (var fileInfo in fileInfos) { using (var stream = fileInfo.OpenRead()) { provider.Load(stream); } } var resourceList = this.GetResourcesList(provider, rootClassName); this.RenderResources(resourceList, rootNamespace); sw.Stop(); this.WriteLine("// Code generation time (msec):" + sw.ElapsedMilliseconds); }
/// <summary> /// Generates code for all XML files in specified folder. Code file will have a specified namespace and root class will have a specified name /// </summary> /// <param name="folder">Folder to look translation files at</param> /// <param name="rootNamespace">Generated code file namespace</param> /// <param name="rootClassName">Genrated code file root class name</param> public void Generate(string folder, string rootNamespace, string rootClassName) { if (!Directory.Exists(folder)) { WriteLine(String.Format("// Directory not found: {0}. Please provide a directory that contains XML translation files.", folder)); return; } var directoryInfo = new DirectoryInfo(folder); var fileInfos = directoryInfo.GetFiles("*.xml"); if (fileInfos.Length <= 0) { WriteLine(String.Format("// Specified directory doesn't contain translation files: {0}. Please provide a directory that contains XML translation files.", folder)); return; } var sw = Stopwatch.StartNew(); var provider = new XmlLocalizationProvider(); // Load all resources files from directory foreach (var fileInfo in fileInfos) { using (var stream = fileInfo.OpenRead()) { provider.Load(stream); } } var resourceList = GetResourcesList(provider, rootClassName); RenderResources(resourceList, rootNamespace); sw.Stop(); WriteLine("// Code generation time (msec):" + sw.ElapsedMilliseconds); }