Globalization در ASP.NET MVC - قسمت دوم
نویسنده: یوسف نژاد
تاریخ: ۱۳۹۲/۰۱/۳۱ ۱۳:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
private void TestResXResourceReader()
{
using (var reader = new ResXResourceReader("Resource1.fa.resx"))
{
foreach (var item in reader)
{
var resource = (DictionaryEntry)item;
Console.WriteLine("{0}: {1}", resource.Key, resource.Value);
}
}
}public class ResXResourceEntry
{
public string Key { get; set; }
public string Value { get; set; }
public ResXResourceEntry() { }
public ResXResourceEntry(object key, object value)
{
Key = key.ToString();
Value = value.ToString();
}
public ResXResourceEntry(DictionaryEntry dictionaryEntry)
{
Key = dictionaryEntry.Key.ToString();
Value = dictionaryEntry.Value != null ? dictionaryEntry.Value.ToString() : string.Empty;
}
public DictionaryEntry ToDictionaryEntry()
{
return new DictionaryEntry(Key, Value);
}
}private static List<ResXResourceEntry> Read(string filePath)
{
using (var reader = new ResXResourceReader(filePath))
{
return reader.Cast<object>().Cast<DictionaryEntry>().Select(de => new ResXResourceEntry(de)).ToList();
}
}private static void Write(IEnumerable<ResXResourceEntry> resources, string filePath)
{
using (var writer = new ResXResourceWriter(filePath))
{
foreach (var resource in resources)
{
writer.AddResource(resource.Key, resource.Value);
}
}
}private static void AddOrUpdate(ResXResourceEntry resource, string filePath)
{
var list = Read(filePath);
var entry = list.SingleOrDefault(l => l.Key == resource.Key);
if (entry == null)
{
list.Add(resource);
}
else
{
entry.Value = resource.Value;
}
Write(list, filePath);
}private static void Remove(string key, string filePath)
{
var list = Read(filePath);
list.RemoveAll(l => l.Key == key);
Write(list, filePath);
}public class ResXResourceManager
{
private static readonly object Lock = new object();
public string ResourcesPath { get; private set; }
public ResXResourceManager(string resourcesPath)
{
ResourcesPath = resourcesPath;
}
public IEnumerable<ResXResourceEntry> GetAllResources(string resourceCategory)
{
var resourceFilePath = GetResourceFilePath(resourceCategory);
return Read(resourceFilePath);
}
public void AddOrUpdateResource(ResXResourceEntry resource, string resourceCategory)
{
var resourceFilePath = GetResourceFilePath(resourceCategory);
AddOrUpdate(resource, resourceFilePath);
}
public void DeleteResource(string key, string resourceCategory)
{
var resourceFilePath = GetResourceFilePath(resourceCategory);
Remove(key, resourceFilePath);
}
private string GetResourceFilePath(string resourceCategory)
{
var extension = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName == "en" ? ".resx" : ".fa.resx";
var resourceFilePath = Path.Combine(ResourcesPath, resourceCategory.Replace(".", "\\") + extension);
return resourceFilePath;
}
private static void AddOrUpdate(ResXResourceEntry resource, string filePath)
{
var list = Read(filePath);
var entry = list.SingleOrDefault(l => l.Key == resource.Key);
if (entry == null)
{
list.Add(resource);
}
else
{
entry.Value = resource.Value;
}
Write(list, filePath);
}
private static void Remove(string key, string filePath)
{
var list = Read(filePath);
list.RemoveAll(l => l.Key == key);
Write(list, filePath);
}
private static List<ResXResourceEntry> Read(string filePath)
{
lock (Lock)
{
using (var reader = new ResXResourceReader(filePath))
{
var list = reader.Cast<object>().Cast<DictionaryEntry>().ToList();
return list.Select(l => new ResXResourceEntry(l)).ToList();
}
}
}
private static void Write(IEnumerable<ResXResourceEntry> resources, string filePath)
{
lock (Lock)
{
using (var writer = new ResXResourceWriter(filePath))
{
foreach (var resource in resources)
{
writer.AddResource(resource.Key, resource.Value);
}
}
}
}
}public class BuildManager
{
public string ProjectPath { get; private set; }
public BuildManager(string projectPath)
{
ProjectPath = projectPath;
}
public void Build()
{
var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0");
if (regKey == null) return;
var msBuildExeFilePath = Path.Combine(regKey.GetValue("MSBuildToolsPath").ToString(), "MSBuild.exe");
var startInfo = new ProcessStartInfo
{
FileName = msBuildExeFilePath,
Arguments = ProjectPath,
WindowStyle = ProcessWindowStyle.Hidden
};
var process = Process.Start(startInfo);
process.WaitForExit();
}
}public class ResXResourceFileManager
{
public static readonly string BinPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///", ""));
public static readonly string ResourcesPath = Path.Combine(BinPath, @"..\App_Data\Resources");
public static readonly string ResourceProjectPath = Path.Combine(ResourcesPath, "Resources.csproj");
public static readonly string DefaultsPath = Path.Combine(ResourcesPath, "Defaults");
public static void CopyDlls()
{
File.Copy(Path.Combine(ResourcesPath, @"bin\debug\Resources.dll"), Path.Combine(BinPath, "Resources.dll"), true);
File.Copy(Path.Combine(ResourcesPath, @"bin\debug\fa\Resources.resources.dll"), Path.Combine(BinPath, @"fa\Resources.resources.dll"), true);
Directory.Delete(Path.Combine(ResourcesPath, "bin"), true);
Directory.Delete(Path.Combine(ResourcesPath, "obj"), true);
}
public static void RestoreAll()
{
RestoreDlls();
RestoreResourceFiles();
}
public static void RestoreDlls()
{
File.Copy(Path.Combine(DefaultsPath, @"bin\Resources.dll"), Path.Combine(BinPath, "Resources.dll"), true);
File.Copy(Path.Combine(DefaultsPath, @"bin\fa\Resources.resources.dll"), Path.Combine(BinPath, @"fa\Resources.resources.dll"), true);
}
public static void RestoreResourceFiles(string resourceCategory)
{
RestoreFile(resourceCategory.Replace(".", "\\"));
}
public static void RestoreResourceFiles()
{
RestoreFile(@"Global\Configs");
RestoreFile(@"Global\Exceptions");
RestoreFile(@"Global\Paths");
RestoreFile(@"Global\Texts");
RestoreFile(@"ViewModels\Employees");
RestoreFile(@"ViewModels\LogOn");
RestoreFile(@"ViewModels\Settings");
RestoreFile(@"Views\Employees");
RestoreFile(@"Views\LogOn");
RestoreFile(@"Views\Settings");
}
private static void RestoreFile(string subPath)
{
File.Copy(Path.Combine(DefaultsPath, subPath + ".resx"), Path.Combine(ResourcesPath, subPath + ".resx"), true);
File.Copy(Path.Combine(DefaultsPath, subPath + ".fa.resx"), Path.Combine(ResourcesPath, subPath + ".fa.resx"), true);
}
}window.location.reload();