Skip to content

Commit 7ce0149

Browse files
committed
1 new version - read localization from separate files CSV
2 refactoring
1 parent c0d744e commit 7ce0149

13 files changed

Lines changed: 383 additions & 231 deletions

Assets/LocalizeService/Plugins.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LocalizeService/Plugins/Core.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
public static class ActionExtentions
4+
{
5+
public static void SafeInvoke(this Action action)
6+
{
7+
if (action != null)
8+
{
9+
action.Invoke();
10+
}
11+
}
12+
13+
public static void SafeInvoke<T>(this Action<T> action, T arg1)
14+
{
15+
if (action != null)
16+
{
17+
action.Invoke(arg1);
18+
}
19+
}
20+
21+
public static void SafeInvoke<T1, T2>(this Action<T1, T2> action, T1 arg1, T2 arg2)
22+
{
23+
if (action != null)
24+
{
25+
action.Invoke(arg1, arg2);
26+
}
27+
}
28+
29+
public static void SafeInvoke<T1, T2, T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
30+
{
31+
if (action != null)
32+
{
33+
action.Invoke(arg1, arg2, arg3);
34+
}
35+
}
36+
37+
public static void SafeInvoke<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
38+
{
39+
if (action != null)
40+
{
41+
action.Invoke(arg1, arg2, arg3, arg4);
42+
}
43+
}
44+
}

Assets/LocalizeService/Plugins/Core/ActionExtentions.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using UnityEngine;
2+
3+
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
4+
{
5+
6+
private static T s_Instance;
7+
private static bool s_IsDestroyed;
8+
9+
public static T Instance
10+
{
11+
get
12+
{
13+
if (s_IsDestroyed)
14+
return null;
15+
16+
if (s_Instance == null)
17+
{
18+
s_Instance = FindObjectOfType(typeof(T)) as T;
19+
20+
if (s_Instance == null)
21+
{
22+
var gameObject = new GameObject(typeof(T).Name);
23+
DontDestroyOnLoad(gameObject);
24+
25+
s_Instance = gameObject.AddComponent(typeof(T)) as T;
26+
}
27+
}
28+
29+
return s_Instance;
30+
}
31+
}
32+
33+
protected virtual void OnDestroy()
34+
{
35+
if (s_Instance)
36+
Destroy(s_Instance);
37+
38+
s_Instance = null;
39+
s_IsDestroyed = true;
40+
}
41+
42+
public bool IsLive()
43+
{
44+
return s_IsDestroyed;
45+
}
46+
}

Assets/LocalizeService/Plugins/Core/MonoSingleton.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LocalizeService/Resources/Localization/localize.csv

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/LocalizeService/Resources/Localization/localize.csv.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
11
using System.Linq;
2+
using System.Text.RegularExpressions;
23
using UnityEngine;
34

4-
namespace SunCubeStudio
5+
public class CSVReader
56
{
6-
public class CSVReader
7+
// splite is [,]
8+
// splits a CSV file into a 2D string array
9+
public static string[,] SplitCsvGrid(string csvText)
710
{
8-
// splite is [,]
9-
// splits a CSV file into a 2D string array
10-
public static string[,] SplitCsvGrid(string csvText)
11+
// string[] lines = csvText.Split("\n"[0]);
12+
var lines = Regex.Split(csvText, System.Environment.NewLine);
13+
// finds the max width of row
14+
int width = 0;
15+
for (int i = 0; i < lines.Length; i++)
1116
{
12-
string[] lines = csvText.Split("\n"[0]);
17+
string[] row = SplitCsvLine(lines[i]);
18+
width = Mathf.Max(width, row.Length);
19+
}
1320

14-
// finds the max width of row
15-
int width = 0;
16-
for (int i = 0; i < lines.Length; i++)
21+
string[,] outputGrid = new string[width, lines.Length+1];
22+
for (int y = 0; y < lines.Length; y++)
23+
{
24+
string[] row = SplitCsvLine(lines[y]);
25+
for (int x = 0; x < row.Length; x++)
1726
{
18-
string[] row = SplitCsvLine(lines[i]);
19-
width = Mathf.Max(width, row.Length);
27+
outputGrid[x, y] = row[x];
28+
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
2029
}
30+
}
31+
// DebugOutputGrid(outputGrid);
2132

22-
string[,] outputGrid = new string[width, lines.Length+1];
23-
for (int y = 0; y < lines.Length; y++)
24-
{
25-
string[] row = SplitCsvLine(lines[y]);
26-
for (int x = 0; x < row.Length; x++)
27-
{
28-
outputGrid[x, y] = row[x];
29-
outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
30-
}
31-
}
33+
return outputGrid;
34+
}
3235

33-
return outputGrid;
34-
}
36+
//public static void DebugOutputGrid(string[,] grid)
37+
//{
38+
// string textOutput = "";
39+
// for (int y = 0; y <= grid.GetUpperBound(1); y++)
40+
// {
41+
// for (int x = 0; x <= grid.GetUpperBound(0); x++)
42+
// {
3543

36-
// splits a CSV row
37-
public static string[] SplitCsvLine(string line)
38-
{
39-
return (from System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(line,
40-
@"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)",
41-
System.Text.RegularExpressions.RegexOptions.ExplicitCapture)
42-
select m.Groups[1].Value).ToArray();
43-
}
44+
// textOutput += grid[x, y];
45+
// textOutput += "|";
46+
// }
47+
// textOutput += "\n";
48+
// }
49+
// Debug.Log(textOutput);
50+
//}
51+
52+
// splits a CSV row
53+
public static string[] SplitCsvLine(string line)
54+
{
55+
return (from Match m in Regex.Matches(line,
56+
@"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)",
57+
System.Text.RegularExpressions.RegexOptions.ExplicitCapture)
58+
select m.Groups[1].Value).ToArray();
4459
}
45-
}
60+
}

Assets/LocalizeService/Scripts/Editor/LocalizationServiceEditor.cs

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,58 @@
33
using UnityEngine;
44
using UnityEditor;
55

6-
namespace SunCubeStudio.Localization
7-
{
8-
[CustomEditor(typeof (LocalizationService))]
9-
public class LocalizationServiceEditor : Editor
10-
{
11-
private readonly int lineHeight = 20;
12-
private Dictionary<string, bool> lanquageFoldot = new Dictionary<string, bool>();
13-
private Dictionary<string, Vector2> lanquageScroll = new Dictionary<string, Vector2>();
14-
15-
public override void OnInspectorGUI()
16-
{
17-
DrawDefaultInspector();
18-
LocalizationService myTarget = (LocalizationService)target;
19-
EditorGUILayout.LabelField("Current Localization ", myTarget.Localization);
20-
EditorGUILayout.LabelField("Localization File Path", LocalizationService.LocalizationFilePath);
21-
EditorGUILayout.Space();
22-
23-
var localize = myTarget.LoadLocalizeFileHelper();
24-
if (localize != null)
25-
{
6+
//namespace SunCubeStudio.Localization
7+
//{
8+
// [CustomEditor(typeof (LocalizationService))]
9+
// public class LocalizationServiceEditor : Editor
10+
// {
11+
// private readonly int lineHeight = 20;
12+
// private Dictionary<string, bool> lanquageFoldot = new Dictionary<string, bool>();
13+
// private Dictionary<string, Vector2> lanquageScroll = new Dictionary<string, Vector2>();
14+
15+
// public override void OnInspectorGUI()
16+
// {
17+
// DrawDefaultInspector();
18+
// LocalizationService myTarget = (LocalizationService)target;
19+
// EditorGUILayout.LabelField("Current Localization ", myTarget.Localization);
20+
// EditorGUILayout.LabelField("Localization File Path", LocalizationService.LocalizationFilePath);
21+
// EditorGUILayout.Space();
22+
23+
// var localize = myTarget.LoadLocalizeFileHelper();
24+
// if (localize != null)
25+
// {
2626

27-
foreach (var loc in localize)
28-
{
29-
if (!lanquageFoldot.ContainsKey(loc.Key))
30-
lanquageFoldot.Add(loc.Key,false);
31-
32-
bool isShow;
33-
if(lanquageFoldot.TryGetValue(loc.Key,out isShow))
34-
lanquageFoldot[loc.Key] = EditorGUILayout.Foldout(isShow, loc.Key);
35-
36-
if (!isShow) continue;
37-
38-
if (!lanquageScroll.ContainsKey(loc.Key))
39-
lanquageScroll.Add(loc.Key, new Vector2());
40-
41-
lanquageScroll[loc.Key] = EditorGUILayout.BeginScrollView(lanquageScroll[loc.Key]);
42-
var values = GetTextOutputOfDictionary(loc.Value);
43-
EditorGUILayout.TextArea(values, GUILayout.Height(loc.Value.Count * lineHeight));
44-
EditorGUILayout.EndScrollView();
45-
}
46-
47-
}
48-
else
49-
{
50-
EditorGUILayout.HelpBox("CSV Localization File not set. Check path to file! " + LocalizationService.LocalizationFilePath, MessageType.Error);
51-
}
52-
}
53-
54-
public string GetTextOutputOfDictionary(Dictionary<string, string> dict)
55-
{
56-
var textOutput = string.Format("size = {0} \n",dict.Count);
57-
return dict.Aggregate(textOutput, (current, item) => current + string.Format("[{0}] = {1} \n", item.Key, item.Value));
58-
}
59-
}
60-
}
27+
// foreach (var loc in localize)
28+
// {
29+
// if (!lanquageFoldot.ContainsKey(loc.Key))
30+
// lanquageFoldot.Add(loc.Key,false);
31+
32+
// bool isShow;
33+
// if(lanquageFoldot.TryGetValue(loc.Key,out isShow))
34+
// lanquageFoldot[loc.Key] = EditorGUILayout.Foldout(isShow, loc.Key);
35+
36+
// if (!isShow) continue;
37+
38+
// if (!lanquageScroll.ContainsKey(loc.Key))
39+
// lanquageScroll.Add(loc.Key, new Vector2());
40+
41+
// lanquageScroll[loc.Key] = EditorGUILayout.BeginScrollView(lanquageScroll[loc.Key]);
42+
// var values = GetTextOutputOfDictionary(loc.Value);
43+
// EditorGUILayout.TextArea(values, GUILayout.Height(loc.Value.Count * lineHeight));
44+
// EditorGUILayout.EndScrollView();
45+
// }
46+
47+
// }
48+
// else
49+
// {
50+
// EditorGUILayout.HelpBox("CSV Localization File not set. Check path to file! " + LocalizationService.LocalizationFilePath, MessageType.Error);
51+
// }
52+
// }
53+
54+
// public string GetTextOutputOfDictionary(Dictionary<string, string> dict)
55+
// {
56+
// var textOutput = string.Format("size = {0} \n",dict.Count);
57+
// return dict.Aggregate(textOutput, (current, item) => current + string.Format("[{0}] = {1} \n", item.Key, item.Value));
58+
// }
59+
// }
60+
//}

0 commit comments

Comments
 (0)