Resource.Manager: added ResxManager

This commit is contained in:
pavelbannov 2019-08-13 14:17:18 +03:00
parent dc33d6566c
commit 1ad30a247f
5 changed files with 181 additions and 81 deletions

View File

@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>

View File

@ -31,13 +31,14 @@ using System.Linq;
using System.Xml;
using ASC.Common.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Formatting = Newtonsoft.Json.Formatting;
namespace ASC.Resource.Manager
{
public static class JsonManager
{
public static void UploadJson(string fileName, Stream fileStream, string projectName, string moduleName)
public static void Upload(string fileName, Stream fileStream, string projectName, string moduleName)
{
var culture = GetCultureFromFileName(fileName);
@ -99,7 +100,7 @@ namespace ASC.Resource.Manager
}
}
public static void ExportJson(string project, string module, string language, string exportPath, bool toJson = true, bool withDefaultValue = true)
public static void Export(string project, string module, string language, string exportPath, string key = null)
{
var filter = new ResCurrent
{
@ -119,10 +120,43 @@ namespace ASC.Resource.Manager
foreach (var fileWords in words)
{
var wordsDictionary = new Dictionary<string, object>();
var firstWord = fileWords.FirstOrDefault();
var fileName = firstWord == null
? module
: Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName);
var zipFileName = Path.Combine(exportPath, language == "Neutral" ? "en" : language, $"{fileName}.json");
foreach (var word in fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title)))
var dirName = Path.GetDirectoryName(zipFileName);
if (!Directory.Exists(dirName))
{
if (string.IsNullOrEmpty(word.ValueTo) && !withDefaultValue) continue;
Directory.CreateDirectory(dirName);
}
var toAdd = new List<ResWord>();
if (!string.IsNullOrEmpty(key))
{
if (File.Exists(zipFileName))
{
var jObject = JObject.Parse(File.ReadAllText(zipFileName));
foreach(var j in jObject)
{
toAdd.Add(new ResWord { Title = j.Key, ValueFrom = j.Value.ToString() });
}
}
if (!toAdd.Any(r => r.Title == key))
{
toAdd.Add(fileWords.FirstOrDefault(r => r.Title == key));
}
}
else
{
toAdd.AddRange(fileWords.OrderBy(x => x.Title).Where(word => !wordsDictionary.ContainsKey(word.Title)));
}
foreach (var word in toAdd)
{
if (string.IsNullOrEmpty(word.ValueTo)) continue;
var newVal = word.ValueTo ?? word.ValueFrom;
@ -135,66 +169,11 @@ namespace ASC.Resource.Manager
wordsDictionary.Add(newKey.Keys.First(), newKey.Values.First());
}
var firstWord = fileWords.FirstOrDefault();
var fileName = firstWord == null
? module
: Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName);
string zipFileName = null;
if (toJson)
{
zipFileName = Path.Combine(exportPath, language == "Neutral" ? "en" : language, $"{fileName}.json");
}
else
{
zipFileName = Path.Combine(exportPath, project, module, $"{fileName}{(language == "Neutral" ? string.Empty : "." + language)}.resx");
}
var dirName = Path.GetDirectoryName(zipFileName);
if (!Directory.Exists(dirName))
{
Directory.CreateDirectory(dirName);
}
using TextWriter writer = new StreamWriter(zipFileName);
if (toJson)
{
var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented);
writer.Write(obj);
}
else
{
var data = new XmlDocument();
var resources = data.CreateElement("resources");
foreach (var ind in wordsDictionary)
{
var stringAttr = data.CreateAttribute("name");
stringAttr.Value = ind.Key;
var child = data.CreateElement("string");
child.Attributes.Append(stringAttr);
child.InnerText = ind.Value.ToString();
resources.AppendChild(child);
}
data.AppendChild(resources);
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace,
OmitXmlDeclaration = false,
ConformanceLevel = ConformanceLevel.Fragment
};
using var xmlTextWriter = XmlWriter.Create(writer, settings);
data.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
}
var obj = JsonConvert.SerializeObject(wordsDictionary, Formatting.Indented);
writer.Write(obj);
}
}

View File

@ -10,10 +10,16 @@ namespace ASC.Resource.Manager
[Option('m', "module", Required = false, HelpText = "Module")]
public string Module { get; set; }
[Option('e', "exportpath", Required = false, HelpText = "Export Path")]
[Option('e', "exportpath", Required = false, HelpText = "Export Path", Default = "..\\..\\..\\..\\ASC.Common\\")]
public string ExportPath { get; set; }
[Option('c', "culture", Required = false, HelpText = "Culture")]
public string Culture { get; set; }
[Option('f', "format", Required = false, HelpText = "Format", Default = "xml")]
public string Format { get; set; }
public void Deconstruct(out string project, out string module, out string exportPath, out string culture, out string format)
=> (project, module, exportPath, culture, format) = (Project, Module, ExportPath, Culture, Format);
}
}

View File

@ -30,19 +30,31 @@ namespace ASC.Resource.Manager
var cultures = new List<string>();
var projects = new List<ResFile>();
var enabledSettings = new EnabledSettings();
Action<string, string, string, string, string> export = null;
try
{
var projectName = options.Project;
var moduleName = options.Module;
var exportPath = options.ExportPath;
var culture = options.Culture;
var (project, module, exportPath, culture, format) = options;
if(format == "json")
{
export = JsonManager.Export;
}
else
{
export = ResxManager.Export;
}
if (string.IsNullOrEmpty(exportPath))
{
exportPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
if (!Path.IsPathRooted(exportPath))
{
exportPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), exportPath));
}
if (!Directory.Exists(exportPath))
{
Console.WriteLine("Error!!! Export path doesn't exist! Please enter a valid directory path.");
@ -53,7 +65,7 @@ namespace ASC.Resource.Manager
cultures = ResourceData.GetCultures().Where(r => r.Available).Select(r => r.Title).Intersect(enabledSettings.Langs).ToList();
projects = ResourceData.GetAllFiles();
ExportWithProject(projectName, moduleName, culture, exportPath);
ExportWithProject(project, module, culture, exportPath);
Console.WriteLine("The data has been successfully exported!");
}
@ -62,47 +74,44 @@ namespace ASC.Resource.Manager
Console.WriteLine(err);
}
void ExportWithProject(string projectName, string moduleName, string culture, string exportPath)
void ExportWithProject(string projectName, string moduleName, string culture, string exportPath, string key = null)
{
if (!string.IsNullOrEmpty(projectName))
{
ExportWithModule(projectName, moduleName, culture, exportPath);
ExportWithModule(projectName, moduleName, culture, exportPath, key);
}
else
{
foreach (var p in projects.Select(r => r.ProjectName).Intersect(enabledSettings.Projects))
{
ExportWithModule(p, moduleName, culture, exportPath);
ExportWithModule(p, moduleName, culture, exportPath, key);
}
}
}
void ExportWithModule(string projectName, string moduleName, string culture, string exportPath)
void ExportWithModule(string projectName, string moduleName, string culture, string exportPath, string key = null)
{
if (!string.IsNullOrEmpty(moduleName))
{
ExportWithCulture(projectName, moduleName, culture, exportPath);
ExportWithCulture(projectName, moduleName, culture, exportPath, key);
}
else
{
foreach (var m in projects.Where(r => r.ProjectName == projectName).Select(r => r.ModuleName))
{
ExportWithCulture(projectName, m, culture, exportPath);
ExportWithCulture(projectName, m, culture, exportPath, key);
}
}
}
void ExportWithCulture(string projectName, string moduleName, string culture, string exportPath)
void ExportWithCulture(string projectName, string moduleName, string culture, string exportPath, string key = null)
{
if (!string.IsNullOrEmpty(culture))
{
JsonManager.ExportJson(projectName, moduleName, culture, exportPath);
export(projectName, moduleName, culture, exportPath, key);
}
else
{
foreach (var c in cultures)
{
JsonManager.ExportJson(projectName, moduleName, c, exportPath);
}
ParallelEnumerable.ForAll(cultures.AsParallel(), c => export(projectName, moduleName, c, exportPath, key));
}
}
}

View File

@ -0,0 +1,105 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
namespace ASC.Resource.Manager
{
public class ResxManager
{
public static void Export(string project, string module, string language, string exportPath, string key = null)
{
var filter = new ResCurrent
{
Project = new ResProject { Name = project },
Module = new ResModule { Name = module },
Language = new ResCulture { Title = language }
};
var words = ResourceData.GetListResWords(filter, string.Empty).GroupBy(x => x.ResFile.FileID).ToList();
if (!words.Any())
{
Console.WriteLine("Error!!! Can't find appropriate project and module. Possibly wrong names!");
return;
}
foreach (var fileWords in words)
{
var wordsDictionary = new Dictionary<string, object>();
var firstWord = fileWords.FirstOrDefault();
var fileName = firstWord == null
? module
: Path.GetFileNameWithoutExtension(firstWord.ResFile.FileName);
var zipFileName = Path.Combine(exportPath, project, module, $"{fileName}{(language == "Neutral" ? string.Empty : "." + language)}.resx");
var dirName = Path.GetDirectoryName(zipFileName);
if (!Directory.Exists(dirName))
{
Directory.CreateDirectory(dirName);
}
var toAdd = new List<ResWord>();
if (!string.IsNullOrEmpty(key))
{
if (File.Exists(zipFileName))
{
using var resXResourceReader = new ResXResourceReader(zipFileName);
foreach (var v in resXResourceReader.Cast<DictionaryEntry>())
{
toAdd.Add(new ResWord { Title = v.Key.ToString(), ValueFrom = v.Value?.ToString() });
}
}
if(!toAdd.Any(r=> r.Title == key))
{
toAdd.Add(fileWords.FirstOrDefault(r => r.Title == key));
}
}
else
{
toAdd.AddRange(fileWords.Where(word => !wordsDictionary.ContainsKey(word.Title)));
}
using var resXResourceWriter = new ResXResourceWriter(zipFileName);
foreach (var word in toAdd.Where(r=> r != null).OrderBy(x => x.Title))
{
resXResourceWriter.AddResource(word.Title, word.ValueFrom);
}
resXResourceWriter.Generate();
resXResourceWriter.Close();
}
}
}
}