-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT.cs
184 lines (158 loc) · 5.19 KB
/
T.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using OftobTech.AppLocalizator.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OftobTech.AppLocalizator
{
public class T : Reader
{
/// <summary> Current language variable</summary>
protected static string? _lang;
/// <summary> Instance of this class</summary>
protected static T? _instance;
/// <summary>
/// Retview current language
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static string getLang()
{
if (_lang == null)
throw new Exception("Language not selected, please use setLang method");
return (string)_lang;
}
/// <summary>
/// Setting language
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static T setLang(string lang)
{
var instance = Init();
lang = PrepareLang(lang);
_lang = lang;
return Init();
}
/// <summary>
/// Class constructor
/// </summary>
/// <param name="lang"></param>
public T(string? lang = null)
{
if (lang != null)
{
_lang = lang;
}
else
{
_lang = Config._config.DefaultLang;
}
UpdateLangs();
}
/// <summary>
/// Init new instance of class
/// </summary>
/// <returns></returns>
public static T Init()
{
if (_instance == null)
{
_instance = new T((_lang != null ? _lang : null));
}
return _instance;
}
/// <summary>
/// String Compilation
/// </summary>
/// <param name="iteredString">Icoming string</param>
/// <param name="stricMode">Is strict mode</param>
/// <returns>Compiled string</returns>
/// <exception cref="Exception"></exception>
public string? Compile(string iteredString, bool stricMode = false)
{
if (LangModel.Languages == null)
{
throw new Exception("Languages now found");
}
var instance = Init();
if (LangModel.Languages.TryGetValue(getLang(), out var Strings))
{
if (Strings.TryGetValue(iteredString, out var result))
{
return result;
}
if (result == null && !stricMode) return iteredString;
else return null;
}
if (!stricMode) return iteredString;
else return null;
}
/// <summary>
/// String Compilation, with additional parametrs
/// </summary>
/// <param name="iteredString"></param>
/// <param name="forReplace"></param>
/// <param name="stricMode"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public string? Compile(string iteredString, Dictionary<string, string> forReplace, bool stricMode = false)
{
if (LangModel.Languages == null)
{
throw new Exception("Languages now found");
}
var instance = Init();
if (LangModel.Languages.TryGetValue(getLang(), out var Strings))
{
if (Strings.TryGetValue(iteredString, out var result))
{
foreach (var data in forReplace)
{
result = result.Replace("{" + data.Key + "}", data.Value);
}
return result;
}
if (result == null && !stricMode) return iteredString;
else return null;
}
if (!stricMode) return iteredString;
else return null;
}
/// <summary>
/// Getting a list of available languages
/// </summary>
/// <returns></returns>
public static List<string> getLangs()
{
if (LangModel.Languages != null)
return LangModel.Languages.Keys.ToList();
return new List<string>();
}
/// <summary>
/// Checking whether there is such a language in the available
/// </summary>
/// <param name="lang">Language to check</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool IsHasLang(string lang)
{
if (LangModel.Languages == null)
{
throw new Exception("Languages now found");
}
if (LangModel.Languages.ContainsKey(lang)) return true;
return false;
}
/// <summary>
/// Language preparation and checking for availability
/// </summary>
/// <param name="lang"></param>
/// <returns></returns>
private static string PrepareLang(string lang)
{
lang = lang.ToLower();
IsHasLang(lang);
return lang;
}
}
}