forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.cs
122 lines (113 loc) · 3.77 KB
/
ResourceManager.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
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd.
//------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace Utilities
{
/// <summary>
/// ResourceManager
/// 资源管理器
///
/// 修改纪录
/// 2007.05.16 版本:1.0 JiRiGaLa 重新调整代码的规范化。
///
/// 版本:1.0
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2007.05.16</date>
/// </author>
/// </summary>
public class ResourceManager
{
private volatile static ResourceManager instance = null;
private static object locker = new Object();
public static ResourceManager Instance
{
get
{
if (instance == null)
{
lock (locker)
{
if (instance == null)
{
instance = new ResourceManager();
}
}
}
return instance;
}
}
private string FolderPath = string.Empty;
public SortedList<String, Resources> LanguageResources = new SortedList<String, Resources>();
public void Serialize(Resources resources, string filePath)
{
ResourcesSerializer.Serialize(filePath, resources);
}
public void Init(string filePath)
{
FolderPath = filePath;
DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
LanguageResources.Clear();
if (!directoryInfo.Exists)
{
return;
}
FileInfo[] FileInfo = directoryInfo.GetFiles();
for (int i = 0; i < FileInfo.Length; i++)
{
Resources resources = ResourcesSerializer.DeSerialize(FileInfo[i].FullName);
resources.createIndex();
LanguageResources.Add(resources.language, resources);
}
}
public Hashtable GetLanguages()
{
Hashtable hashtable = new Hashtable();
IEnumerator<KeyValuePair<String, Resources>> iEnumerator = LanguageResources.GetEnumerator();
while (iEnumerator.MoveNext())
{
hashtable.Add(iEnumerator.Current.Key, iEnumerator.Current.Value.displayName);
}
return hashtable;
}
public Hashtable GetLanguages(string path)
{
Hashtable hashtable = new Hashtable();
DirectoryInfo directoryInfo = new DirectoryInfo(path);
if (!directoryInfo.Exists)
{
return hashtable;
}
FileInfo[] fileInfo = directoryInfo.GetFiles();
for (int i = 0; i < fileInfo.Length; i++)
{
Resources resources = ResourcesSerializer.DeSerialize(fileInfo[i].FullName);
hashtable.Add(resources.language, resources.displayName);
}
return hashtable;
}
public Resources GetResources(string filePath)
{
Resources resources = new Resources();
if (File.Exists(filePath))
{
resources = ResourcesSerializer.DeSerialize(filePath);
resources.createIndex();
}
return resources;
}
public string Get(string language, string key)
{
if (!LanguageResources.ContainsKey(language))
{
return string.Empty;
}
return LanguageResources[language].Get(key);
}
}
}