/// <summary> /// Localizes the specified mod assembly. /// </summary> /// <param name="modAssembly">The assembly to localize.</param> /// <param name="locale">The locale file name to be used.</param> private static void Localize(Assembly modAssembly, Localization.Locale locale) { string path = PUtil.GetModPath(modAssembly); string locCode = locale.Code; if (string.IsNullOrEmpty(locCode)) { locCode = Localization.GetCurrentLanguageCode(); } var poFile = Path.Combine(Path.Combine(path, TRANSLATIONS_DIR), locCode + PLibLocalization.TRANSLATIONS_EXT); try { Localization.OverloadStrings(Localization.LoadStringsFile(poFile, false)); RewriteStrings(modAssembly); } catch (FileNotFoundException) { // No localization available for this locale #if DEBUG PDatabaseUtils.LogDatabaseDebug("No {0} localization available for mod {1}".F( locCode, modAssembly.GetNameSafe() ?? "?")); #endif } catch (DirectoryNotFoundException) { } catch (IOException e) { PDatabaseUtils.LogDatabaseWarning("Failed to load {0} localization for mod {1}:". F(locCode, modAssembly.GetNameSafe() ?? "?")); PUtil.LogExcWarn(e); } }
/// <summary> /// Loads codex entries from the specified directory. /// </summary> /// <param name="entries">The location where the data will be placed.</param> /// <param name="dir">The directory to load.</param> /// <param name="category">The category to assign to each entry thus loaded.</param> private static void LoadFromDirectory(ICollection <CodexEntry> entries, string dir, string category) { string[] codexFiles = new string[0]; try { // List codex data files in the codex directory codexFiles = Directory.GetFiles(dir, CODEX_FILES); } catch (UnauthorizedAccessException ex) { PUtil.LogExcWarn(ex); } catch (IOException ex) { PUtil.LogExcWarn(ex); } var widgetTagMappings = WIDGET_TAG_MAPPINGS?.GetValue(null) as WidgetMappingList; if (widgetTagMappings == null) { PDatabaseUtils.LogDatabaseWarning("Unable to load codex files: no tag mappings found"); } foreach (string str in codexFiles) { try { string filename = str; var codexEntry = YamlIO.LoadFile <CodexEntry>(filename, YamlParseErrorCB, widgetTagMappings); if (codexEntry != null) { codexEntry.category = category; entries.Add(codexEntry); } } catch (IOException ex) { PDatabaseUtils.LogDatabaseWarning("Unable to load codex files from {0}:". F(dir)); PUtil.LogExcWarn(ex); } }
/// <summary> /// Registers the specified assembly for automatic PLib localization. If the argument /// is omitted, the calling assembly is registered. /// </summary> /// <param name="assembly">The assembly to register for PLib localization.</param> public void Register(Assembly assembly = null) { if (assembly == null) { assembly = Assembly.GetCallingAssembly(); } var types = assembly.GetTypes(); if (types == null || types.Length == 0) { PDatabaseUtils.LogDatabaseWarning("Registered assembly " + assembly. GetNameSafe() + " that had no types for localization!"); } else { RegisterForForwarding(); toLocalize.Add(assembly); // This call searches all types in the assembly implicitly Localization.RegisterForTranslation(types[0]); #if DEBUG PDatabaseUtils.LogDatabaseDebug("Localizing assembly {0} using base namespace {1}". F(assembly.GetNameSafe(), types[0].Namespace)); #endif } }