Example #1
0
        public LocalizationFile ResolveFormContent(string content)
        {
            var re = new LocalizationFile();

            if (content.StartsWith("[Type:Newbe.Claptrap.L]"))
            {
                re.Culture = string.Empty;
            }
            else
            {
                // [Culture:{file.Culture}:Type:Newbe.Claptrap.L] >
                var culture = content.Substring(0, content.IndexOf("]", StringComparison.OrdinalIgnoreCase) + 1)
                              .Replace("[Culture:", string.Empty)
                              .Replace(":Type:Newbe.Claptrap.L]", string.Empty);
                re.Culture = culture;
            }

            var lines             = content.Split(Environment.NewLine);
            var localizationItems = lines
                                    .ToObservable()
                                    .Skip(2)
                                    .Buffer(2)
                                    .Where(x => x.Count == 2 && x.All(a => !string.IsNullOrEmpty(a)))
                                    .Select(twoLine =>
            {
                var sourceLine = twoLine[0];
                var textLine   = twoLine[1];
                var x          = textLine.Split("=");
                var item       = new LocalizationItem
                {
                    Key        = x[0].TrimEnd(),
                    Text       = x[1].TrimStart(),
                    SourceText = sourceLine.TrimStart('#').TrimStart()
                };
                return(item);
            })
                                    .ToEnumerable()
                                    .ToList();

            re.Items = localizationItems;
            return(re);
        }
Example #2
0
        public string Create(LocalizationFile file)
        {
            var sb = new StringBuilder();

            sb.AppendLine($"# file is created by Newbe.Claptrap.DevTools at {DateTimeOffset.UtcNow:R}");
            if (string.IsNullOrEmpty(file.Culture))
            {
                sb.AppendLine("[Type:Newbe.Claptrap.L]");
            }
            else
            {
                sb.AppendLine($"[Culture:{file.Culture}:Type:Newbe.Claptrap.L]");
            }

            foreach (var item in file.Items)
            {
                sb.AppendLine($"# {item.SourceText}");
                sb.AppendLine($"{item.Key} = {item.Text}");
            }

            var re = sb.ToString();

            return(re);
        }
Example #3
0
        public async Task RunAsync()
        {
            var fileContent =
                await File.ReadAllTextAsync(
                    "../../../../Newbe.Claptrap.Localization/LK.cs");

            var root             = CSharpSyntaxTree.ParseText(fileContent).GetCompilationUnitRoot();
            var items            = GetLocalizationItems().OrderBy(x => x.Key).ToArray();
            var localizationFile = new LocalizationFile
            {
                Culture = string.Empty,
                Items   = items
            };
            var globalFile = _localizationFileFactory.Create(localizationFile);

            var translationResults = await _translator.TranslateAsync(localizationFile, new[]
            {
                "zh-Hans",
                "zh-Hant",
                "ja",
                "ru",
            });

            const string globalFilePath = "../../../../Newbe.Claptrap.Localization/Docs/L.ini";
            await File.WriteAllTextAsync(globalFilePath, globalFile);

            foreach (var(k, v) in translationResults)
            {
                var cnFilePath = $"../../../../Newbe.Claptrap.Localization/Docs/L-{k}.ini";
                var createNew  = true;
                if (createNew)
                {
                    var cnFile = _localizationFileFactory.Create(v);
                    await File.WriteAllTextAsync(cnFilePath, cnFile);
                }
                else
                {
                    var oldFileString = await File.ReadAllTextAsync(cnFilePath);

                    var oldFile        = _localizationFileFactory.ResolveFormContent(oldFileString);
                    var oldFileItemDic = oldFile.Items.ToDictionary(x => x.Key);

                    var cnItems = CreateNewItems().OrderBy(x => x.Key).ToArray();
                    var cnFile  = _localizationFileFactory.Create(new LocalizationFile
                    {
                        Culture = "cn",
                        Items   = cnItems
                    });
                    await File.WriteAllTextAsync(cnFilePath, cnFile);

                    IEnumerable <LocalizationItem> CreateNewItems()
                    {
                        foreach (var item in v.Items !)
                        {
                            if (oldFileItemDic !.TryGetValue(item.Key, out var oldItem))
                            {
                                if (oldItem.SourceText != item.SourceText)
                                {
                                    yield return(item);
                                }
                                else
                                {
                                    yield return(oldItem);
                                }
                            }