/
LocaleImpl.cs
111 lines (102 loc) · 4.09 KB
/
LocaleImpl.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
//
// Copyright (c) 2010 Yves Langisch. All rights reserved.
// http://cyberduck.ch/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Bug fixes, suggestions and comments should be sent to:
// yves@cyberduck.ch
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using ch.cyberduck.core;
using ch.cyberduck.core.i18n;
using org.apache.log4j;
namespace Ch.Cyberduck.Ui.Controller
{
public class LocaleImpl : Locale
{
private static readonly Logger Log = Logger.getLogger(typeof (LocaleImpl).FullName);
private static readonly Regex StringsRegex = new Regex("\"?(.*?)\"?[ ]*=[ ]*\"(.*)\"", RegexOptions.Compiled);
private readonly IDictionary<string, Dictionary<string, string>> _cache =
new Dictionary<string, Dictionary<string, string>>();
private readonly string _language = Preferences.instance().getProperty("application.language");
private void ReadBundleIntoCache(string bundle)
{
Log.debug("Caching bundle " + bundle);
Assembly asm = Assembly.GetExecutingAssembly();
// the dots apparently come from the relative path in the msbuild file
Stream stream = asm.GetManifestResourceStream(
string.Format("Ch.Cyberduck..........{0}.lproj.{1}.strings", _language, bundle));
if (null == stream)
{
stream =
asm.GetManifestResourceStream(
string.Format(
"Ch.Cyberduck..........lib.Sparkle.framework.Versions.A.Resources.{0}.lproj.Sparkle.strings",
_language));
}
if (null != stream)
{
using (StreamReader file = new StreamReader(stream))
{
Dictionary<string, string> bundleDict = new Dictionary<string, string>();
_cache[bundle] = bundleDict;
string line;
while ((line = file.ReadLine()) != null)
{
if (StringsRegex.IsMatch(line))
{
Match match = StringsRegex.Match(line);
string key = match.Groups[1].Value;
string value = match.Groups[2].Value;
bundleDict[key] = value;
}
}
}
}
else
{
Log.error(String.Format("Bundle {0} not found", bundle));
}
}
public override string get(string key, string table)
{
Dictionary<string, string> bundle;
if (!_cache.TryGetValue(table, out bundle))
{
ReadBundleIntoCache(table);
//try again
if (!_cache.TryGetValue(table, out bundle))
{
Log.warn(string.Format("Key '{0}' in bundle '{1}' not found", key, table));
return key;
}
}
string value;
return bundle.TryGetValue(key, out value) ? value : key;
}
public static void Register()
{
LocaleFactory.addFactory(ch.cyberduck.core.Factory.NATIVE_PLATFORM, new Factory());
}
private class Factory : LocaleFactory
{
protected override object create()
{
return new LocaleImpl();
}
}
}
}