public void AddNewDifferentProperty(string dispalyName, object newFromValue, object newToValue, string propertyName = null) { if (string.IsNullOrWhiteSpace(dispalyName)) { throw new ArgumentException($"{ nameof(dispalyName)} can't be empty", nameof(dispalyName)); } dispalyName = dispalyName.Trim().Replace('"', ','); if (string.IsNullOrWhiteSpace(propertyName)) { propertyName = dispalyName; } else { propertyName = propertyName.Trim(); } if (DifferentProperties.Any(o => o.PropertyName == propertyName)) { throw new ArgumentException($"DifferentProperties has exist {propertyName}, if you need update, please use UpdateDifferentProperty() method", nameof(propertyName)); } var from = LocalizationTools.ToString(newFromValue); var to = LocalizationTools.ToString(newToValue); if (from == to) { throw new ArgumentException($"newFromValue is same to newToValue"); } var differentProperty = new DifferentProperty(propertyName, dispalyName, from, to); DifferentProperties.Add(differentProperty); }
static void Main(string[] args) { var p1 = new Person { Id = 1, Name = "王大锤", Birthday = DateTime.Parse("2020-01-01"), CityId = 10, Sex = SexType.Man, Password = "******", IsAvailable = true, Spouse = new Person() { Id = 2, Name = "老婆", Sex = SexType.Woman } }; p1.Hobby.Add("吃饭"); p1.Hobby.Add("拍视频"); Console.WriteLine("********** ToString(): 简单 **********"); var str = LocalizationTools.ToString(p1); Console.WriteLine(str); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("**** ToString():使用 替换 和 忽略项 ***"); // 假设 10 代表北京 str = LocalizationTools.ToString(p1, new { CityId = "北京" }, nameof(Person.Password)); Console.WriteLine(str); Console.WriteLine(); Console.WriteLine(); var p2 = new Person { Id = 1, Name = "王小锤", Password = "******", CityId = 28, }; Console.WriteLine("********* Compare(): 带有忽略项 *********"); var compareResult = LocalizationTools.Compare(p1, p2, new[] { nameof(Person.Password) }); Console.WriteLine(compareResult.GetDifferenceMsg()); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("********** Compare(): 使用替换 **********"); // 假设 10 代表北京, 28 代表成都 compareResult.UpdateDifferentProperty(nameof(Person.CityId), "北京", "成都"); Console.WriteLine(compareResult.GetDifferenceMsg()); Console.WriteLine(); }
public override void OnInspectorGUI() { DrawDefaultInspector(); LocalizationTools localizationTools = (LocalizationTools)target; if (GUILayout.Button("Replace comma seperator with asterix in localization csv")) { localizationTools.ReplaceCharInLocCSV(); } }
//BOOT: 3. InitApp(): Application Initialization private void InitApp() { // enable localization LocalizationTools.InitLocalization(); //TODO: UseMockServices : Reset to settings UseMockServices = true; //UseMockServices = Settings.UseMocks; //INFO: Register the Dependencies ViewModelLocator.RegisterDependencies(UseMockServices); }
private static void ShowWindow() { Rect rc = new Rect(100, 100, 1024, 768); LocalizationTools window = EditorWindow.GetWindowWithRect(typeof(LocalizationTools), rc, true) as LocalizationTools; if (null != window) { //window.titleContent = new GUIContent("场景编辑器"); window.Init(); window.Show(); } }
public string GetDifferenceMsg(IEnumerable<string> ignorePropertyNames = null) { var sb = new StringBuilder(256); sb.Append('{'); foreach (var differentProperty in DifferentProperties) { if (ignorePropertyNames?.Contains(differentProperty.PropertyName) == true) { continue; } sb.Append($"\"{differentProperty.DispalyName}\":{{"); sb.Append($"\"从\":{differentProperty.From ?? "null"},"); sb.Append($"\"变成\":{differentProperty.To ?? "null"}}},"); } LocalizationTools.StringBuilderTrimEnd(sb); sb.Append('}'); return sb.ToString(); }
public bool UpdateDifferentProperty(string propertyName, object newFromValue, object newToValue) { var differentProperty = DifferentProperties.FirstOrDefault(o => o.PropertyName == propertyName); if (differentProperty == null) { return false; } // 如果传入的两个值是相等的, 则不更新 var from = LocalizationTools.ToString(newFromValue); var to = LocalizationTools.ToString(newToValue); if (from == to) { return false; } differentProperty.From = from; differentProperty.To = to; return true; }
public void ToStringTest() { string actual, expected; var person = new Person() { Id = 1, Name = "王大锤", Birthdate = DateTime.Parse("2001-02-01"), CityId = 10, Password = "******", Sex = SexType.Man, Height = 170, CanLogin = true, GraduateDate = null }; LocalizationTools.KeyValueSeparator = "="; actual = LocalizationTools.ToString(person); expected = "{\"唯一性标识\"=1,\"名字\"=\"王大锤\",\"Birthdate\"=\"2001/2/1 0:00:00\",\"所在城市\"=10,\"性别\"=\"男性\",\"身高\"=170,\"密码\"=\"123456789\",\"是否可以登录\"=true,\"毕业时间\"=null}"; Assert.AreEqual(expected, actual); LocalizationTools.KeyValueSeparator = ":"; actual = LocalizationTools.ToString(person); expected = "{\"唯一性标识\":1,\"名字\":\"王大锤\",\"Birthdate\":\"2001/2/1 0:00:00\",\"所在城市\":10,\"性别\":\"男性\",\"身高\":170,\"密码\":\"123456789\",\"是否可以登录\":true,\"毕业时间\":null}"; Assert.AreEqual(expected, actual); actual = LocalizationTools.ToString(person, nameof(Person.Password)); expected = "{\"唯一性标识\":1,\"名字\":\"王大锤\",\"Birthdate\":\"2001/2/1 0:00:00\",\"所在城市\":10,\"性别\":\"男性\",\"身高\":170,\"是否可以登录\":true,\"毕业时间\":null}"; Assert.AreEqual(expected, actual); actual = LocalizationTools.ToString(person, new { CityId = "北京" }, nameof(Person.Password)); expected = "{\"唯一性标识\":1,\"名字\":\"王大锤\",\"Birthdate\":\"2001/2/1 0:00:00\",\"所在城市\":\"北京\",\"性别\":\"男性\",\"身高\":170,\"是否可以登录\":true,\"毕业时间\":null}"; Assert.AreEqual(expected, actual); actual = LocalizationTools.ToString(person, new Dictionary <string, object>() { { "CityId", "北京" } }, nameof(Person.Password)); Assert.AreEqual(expected, actual); actual = LocalizationTools.ToStringInclude(person, new[] { nameof(Person.Name), nameof(Person.Birthdate), nameof(Person.CityId), nameof(Person.Sex), }); expected = "{\"名字\":\"王大锤\",\"Birthdate\":\"2001/2/1 0:00:00\",\"所在城市\":10,\"性别\":\"男性\"}"; Assert.AreEqual(expected, actual); actual = LocalizationTools.ToStringInclude(person, new[] { nameof(Person.Name), nameof(Person.Birthdate), nameof(Person.CityId), nameof(Person.Sex), }, new { CityId = "北京" }); expected = "{\"名字\":\"王大锤\",\"Birthdate\":\"2001/2/1 0:00:00\",\"所在城市\":\"北京\",\"性别\":\"男性\"}"; Assert.AreEqual(expected, actual); actual = LocalizationTools.ToStringInclude(person, new[] { nameof(Person.Name), nameof(Person.Birthdate), nameof(Person.CityId), nameof(Person.Sex), }, new Dictionary <string, object>() { { "CityId", "北京" } }); Assert.AreEqual(expected, actual); var sexTypes = new[] { SexType.Man, SexType.Woman, SexType.Ladyman }; actual = LocalizationTools.ToString(sexTypes); expected = "[\"男性\",\"女性\",\"Ladyman\"]"; Assert.AreEqual(expected, actual); var employe = new Employe() { Id = 1, Name = "王大锤", Sex = SexType.Man, CanLogin = true, Partner = new Employe() { Id = 2, Name = "渣渣" }, }; actual = LocalizationTools.ToString(employe); expected = "{\"唯一性标识\":1,\"名字\":\"王大锤\",\"性别\":\"男性\",\"可否登录\":\"可以登录\",\"搭档\":{\"唯一性标识\":2,\"名字\":\"渣渣\",\"性别\":\"男性\",\"可否登录\":\"未配置\",\"搭档\":\"没有搭档\"}}"; Assert.AreEqual(expected, actual); }
public void CompareTest() { string actual, expected; var e1 = new Employe() { Id = 1, Name = "王大锤", Sex = SexType.Man, CanLogin = true, Partner = new Employe() { Id = 2, Name = "渣渣" }, }; var e2 = new Employe() { Id = 2, Name = "王小锤", Sex = SexType.Ladyman, CanLogin = true, Partner = null, }; var compareResult = LocalizationTools.Compare(e1, e2, new[] { nameof(Employe.Id) }); Assert.IsTrue(compareResult.HasDifference); Assert.AreEqual(compareResult.DifferentProperties.Count, 3); Assert.IsTrue(compareResult.IsPropertyDifferent(nameof(Employe.Name))); Assert.IsTrue(compareResult.IsPropertyDifferent(nameof(Employe.Sex))); Assert.IsFalse(compareResult.IsPropertyDifferent(nameof(Employe.CanLogin))); Assert.IsTrue(compareResult.IsPropertyDifferent(nameof(Employe.Partner))); Assert.AreEqual("\"王大锤\"", compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Name)).From); Assert.AreEqual("\"王小锤\"", compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Name)).To); Assert.AreEqual("\"男性\"", compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Sex)).From); Assert.AreEqual("\"Ladyman\"", compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Sex)).To); expected = "{\"唯一性标识\":2,\"名字\":\"渣渣\",\"性别\":\"男性\",\"可否登录\":\"未配置\",\"搭档\":\"没有搭档\"}"; actual = compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Partner)).From; Assert.AreEqual(expected, actual); Assert.AreEqual("\"没有搭档\"", compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Partner)).To); expected = "{\"名字\":{\"从\":\"王大锤\",\"变成\":\"王小锤\"},\"性别\":{\"从\":\"男性\",\"变成\":\"Ladyman\"},\"搭档\":{\"从\":{\"唯一性标识\":2,\"名字\":\"渣渣\",\"性别\":\"男性\",\"可否登录\":\"未配置\",\"搭档\":\"没有搭档\"},\"变成\":\"没有搭档\"}}"; actual = compareResult.GetDifferenceMsg(); Assert.AreEqual(expected, actual); expected = "{}"; actual = compareResult.GetDifferenceMsg(new[] { nameof(Employe.Name), nameof(Employe.Sex), nameof(Employe.Partner) }); Assert.AreEqual(expected, actual); var updateDifferentPropertyResult = compareResult.UpdateDifferentProperty(nameof(Employe.Name), "王大锤", "王大锤"); Assert.AreEqual(updateDifferentPropertyResult, false); // 没有跟新成功, to值不应该改变 actual = compareResult.DifferentProperties.Single(o => o.PropertyName == nameof(Employe.Name)).To; Assert.AreEqual(actual, "\"王小锤\""); updateDifferentPropertyResult = compareResult.UpdateDifferentProperty(nameof(Employe.Name), "王大锤", new { 姓 = "王", = "大锤" });