Beispiel #1
0
        /// <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);
            }
        }
Beispiel #2
0
        /// <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
            }
        }
Beispiel #3
0
        /// <summary>
        /// Searches types in the assembly (no worries, Localization did this anyways, so they
        /// all either loaded or failed to load) for fields that already had loc string keys
        /// created, and fixes them if so.
        /// </summary>
        /// <param name="assembly">The assembly to check for strings.</param>
        internal static void RewriteStrings(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                foreach (var field in type.GetFields(PPatchTools.BASE_FLAGS | BindingFlags.
                                                     FlattenHierarchy | BindingFlags.Static))
                {
                    // Only use fields of type LocString
                    if (field.FieldType == typeof(LocString) && field.GetValue(null) is
                        LocString ls)
                    {
#if DEBUG
                        PDatabaseUtils.LogDatabaseDebug("Rewrote string {0}: {1} to {2}".F(ls.
                                                                                           key.String, Strings.Get(ls.key.String), ls.text));
#endif
                        Strings.Add(ls.key.String, ls.text);
                    }
                }
            }
        }