Beispiel #1
0
            private static void UIBinderLoadPostfix(UIBinder __instance, TranslationHookState __state)
            {
                var gameObject = __instance.gameObject;
                var path       = __state.Path;

                var components   = (List <Component>)__state.Context[0];
                var scopes       = (List <int>)__state.Context[1];
                var origValues   = (List <string>)__state.Context[2];
                var origResizers = (List <XuaResizerResult>)__state.Context[3];

                var items = EnumerateTextComponents(gameObject).ToList();

                if (items.Count != components.Count)
                {
                    BaseTextDumpPlugin.Logger.LogWarning(
                        $"UIBinder {gameObject}: Component count has changed, may not be able to get all translations");
                }
                else
                {
                    components = items.Select(t => t.Value).ToList();
                }

                var results  = new TranslationDictionary();
                var resizers = new ResizerCollection();

                for (var i = 0; i < components.Count; i++)
                {
                    var key = origValues[i];
                    var val = GetTextFromSupportedComponent(components[i]);

                    var scope = scopes[i];
                    _instance.AddLocalizationToResults(results.GetScope(scope), key, val);


                    var currentResizer = GetTextResizerFromComponent(components[i]);

                    var resizePath = components[i].GetXuaResizerPath();
                    if (!string.IsNullOrEmpty(resizePath))
                    {
                        var delta          = currentResizer.Delta(origResizers[i]);
                        var scopedResizers = resizers.GetScope(scope);
                        scopedResizers[resizePath] = delta.GetDirectives().ToList();
                    }
                }

                var outputName = CombinePaths("Bind/UI", path);

                HookedTextLocalizationGenerators.Add(new StringTranslationDumper(outputName, () => results));
                HookedTextLocalizationGenerators.Add(new ResizerDumper(outputName, () => resizers));
            }
 protected override List <string> CreateLines(string filePath, TranslationDictionary translations)
 {
     return(filePath.StartsWith(AssetsRoot)
         ? CreateResourceReplacementLines(translations)
         : CreateLocalizationLines(translations));
 }
        private List <string> CreateLocalizationLines(TranslationDictionary translations)
        {
            var formatStringRegex = LocalizationDumpHelper.FormatStringRegex;
            var lines             = new List <string>();
            var scopeLines        = new List <string>();

            foreach (var scope in translations.Scopes)
            {
                scopeLines.Clear();
                foreach (var localization in translations.GetScope(scope))
                {
                    var key   = localization.Key;
                    var value = localization.Value;
                    value = value.IsNullOrWhiteSpace() ? string.Empty : value;
                    if (key.Trim() == value.Trim())
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(value) &&
                        TextResourceHelper.GlobalMappings.TryGetValue(key, out var globalValue))
                    {
                        value = globalValue;
                    }

                    if (string.IsNullOrEmpty(key) || key == value ||
                        string.IsNullOrEmpty(value) && !ContainsNonAscii(key))
                    {
                        continue;
                    }

                    LocalizationDumpHelper.PrepareLineForDump(ref key, ref value);


                    if (!key.StartsWith("r:") && !key.StartsWith("sr:"))
                    {
                        key   = key.Replace("=", "%3D");
                        value = (value?.Replace("=", "%3D") ?? string.Empty)
                                .TrimEnd(TextResourceHelper.WhitespaceCharacters);

                        var isFormat = formatStringRegex.IsMatch(key);
                        var regex    = isFormat;
                        if (isFormat)
                        {
                            for (var count = 1; formatStringRegex.IsMatch(key); count++)
                            {
                                var match = formatStringRegex.Match(key);
                                key = formatStringRegex.Replace(key, FormatStringPlaceholder, 1);
                                try
                                {
                                    value = new Regex(Regex.Escape(match.Value)).Replace(value, $"${count}", 1);
                                }
                                catch (ArgumentException err)
                                {
                                    Logger.LogWarning(
                                        $"Unable to correctly create replacements for {match.Value} in {value}, check your output files: {err}");
                                }
                            }
                        }

                        if (regex)
                        {
                            key = Regex.Escape(key);
                            if (isFormat)
                            {
                                key = key.Replace(FormatStringPlaceholder, "(.+)");
                            }
                        }

                        if (regex)
                        {
                            key = (isFormat ? "sr:" : "r:") + "\"^" + key + "$\"";
                        }
                    }

                    if (string.IsNullOrEmpty(value))
                    {
                        key = $"//{key}";
                    }
                    scopeLines.Add(JoinStrings("=", key, value));
                }

                if (scopeLines.Count <= 0)
                {
                    continue;
                }
                if (lines.Count > 0)
                {
                    lines.Add("");
                }
                if (scope != -1)
                {
                    lines.Add($"#set level {scope}");
                }
                lines.AddRange(scopeLines);
                if (scope != -1)
                {
                    lines.Add($"#unset level {scope}");
                }
            }

            return(lines);
        }
Beispiel #4
0
        protected override List <string> CreateLines(string filePath, TranslationDictionary translations)
        {
            var lines      = new List <string>();
            var scopeLines = new List <string>();

            foreach (var scope in translations.Scopes)
            {
                scopeLines.Clear();
                foreach (var localization in translations.GetScope(scope))
                {
                    var key   = localization.Key;
                    var value = localization.Value;
                    value = value.IsNullOrWhiteSpace() ? string.Empty : value.FixRedirected();

                    if (key.Trim() == value.Trim())
                    {
                        continue;
                    }

                    if (key.StartsWith(FilePattern) && value.EndsWith(FilePattern))
                    {
                        scopeLines.Add(string.Empty);
                        scopeLines.Add($"// {key.Substring(FilePattern.Length)}");
                        continue;
                    }

                    if (string.IsNullOrEmpty(key) || key == value ||
                        string.IsNullOrEmpty(value) && !EntryNeedsTranslation(key))
                    {
                        continue;
                    }

                    // comment out potential partial entries
                    if (string.IsNullOrEmpty(value) || LanguageHelper.IsTranslatable(value))
                    {
                        key = $"//{key}";
                    }

                    scopeLines.Add(JoinStrings("=", Encode(key), Encode(value)));
                }

                if (scopeLines.Count <= 0)
                {
                    continue;
                }
                if (lines.Count > 0)
                {
                    lines.Add("");
                }
                if (scope != -1)
                {
                    lines.Add($"#set level {scope}");
                }
                lines.AddRange(scopeLines);
                if (scope != -1)
                {
                    lines.Add($"#unset level {scope}");
                }
            }

            return(lines);
        }