Das "Translate" ist von dir, oder? Wie sieht das ganze dann als intern aus?
namespace System.Web.Mvc.Html
{
public static class HtmlHelperExtensions
{
public static MvcHtmlString Translate(this HtmlHelper helper, string resourceKey)
{
return MvcHtmlString.Create(Project.Resources.Translate(resourceKey));
}
}
}
namespace Project
{
using Project.Models;
public static class Resources
{
public static readonly CultureInfo DefaultCulture = new CultureInfo("de-de");
public static readonly TimeSpan ResourceCacheExpiration = TimeSpan.FromSeconds(30);
static readonly object s_lock = new object();
static bool s_initialized;
static DateTime s_expiration;
static IDictionary<string, IDictionary<string, string>> s_resources;
static internal void InitializeOnce(HttpApplicationState application)
{
if (!s_initialized || DateTime.UtcNow > s_expiration)
{
lock (s_lock)
{
if (!s_initialized || DateTime.UtcNow > s_expiration)
{
var storageAccount = application["CloudStorageAccount"] as Microsoft.WindowsAzure.CloudStorageAccount;
var context = new Project.Models.ProjectDataServiceContext(
storageAccount.TableEndpoint.ToString(),
storageAccount.Credentials);
s_resources = new SortedDictionary<string, IDictionary<string, string>>();
var resources = from r in context.ResourceTable
select r;
var query = resources.AsTableServiceQuery<Resource>();
foreach (var resource in query.Execute())
{
var culture = string.IsNullOrEmpty(resource.PartitionKey) ? DefaultCulture.Name.ToLowerInvariant() : resource.PartitionKey;
var key = resource.RowKey;
var value = resource.Value;
if (!s_resources.ContainsKey(culture))
s_resources.Add(culture, new SortedDictionary<string, string>());
s_resources[culture][key] = value;
}
s_expiration = DateTime.UtcNow.Add(ResourceCacheExpiration);
s_initialized = true;
}
}
}
}
public static string Translate(string resourceKey)
{
if (!s_initialized)
throw new Exception("Dictionary is not initialized.");
var context = (ProjectContext)HttpContext.Current.Items["ProjectContext"];
var currentCulture = context.Culture;
var cultures = new[] {
currentCulture.Name.ToLowerInvariant(),
currentCulture.TwoLetterISOLanguageName,
string.Empty // fallback values
}.Distinct();
var resourceDictionaries =
from culture in cultures
where s_resources.ContainsKey(culture)
select s_resources[culture];
var resourceQuery = (from resources in resourceDictionaries
where resources.ContainsKey(resourceKey)
select resources[resourceKey]).Concat(new string[] { resourceKey });
string result;
lock (s_lock)
{
result = resourceQuery.First();
}
if (DateTime.UtcNow > s_expiration)
{
System.Threading.Tasks.Task.Factory.StartNew((application) =>
{
InitializeOnce((HttpApplicationState)application);
}, HttpContext.Current.Application);
}
return result;
}
}
}
|
|
<!-- snip -->
<h1>@Html.Translate("Title_CustomerRegistration")</h1>
<p>
@Html.TranslatedLabelFor(model => model.Customer.Email, "Label_Email")<br/>
@Html.TextBoxFor(model => model.Customer.Email)
</p>
<!-- snip -->
|
|
/*
General Javascript library
2010 Stefc
*/
String.prototype.trim = function(){
return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/,""))};
String.prototype.startsWith = function(str) {
return (this.match("^"+str)==str)};
String.prototype.endsWith = function(str) {
return (this.match(str+"$")==str)};
String.prototype.transform = function() {
if(this.check())
{
var key =this.slice(1,this.length-1).toLowerCase();
if(strings.hasOwnProperty(key))
return strings[key];
}
return this;
};
String.prototype.check = function() {
return this.match("%.+%")!=null;}
function traverseDOMTree(node) {
if (node)
{
if(node.nodeValue && (node.nodeType == 3))
{
if(node.nodeValue.check())
node.nodeValue = node.nodeValue.transform();
}
var attrs = node.attributes;
if(attrs && attrs.length)
{
for (var length=attrs.length, j = 0; j < length;j++)
{
if(attrs[j].nodeValue.check())
{
node.setAttribute(attrs[j].name,attrs[j].nodeValue.transform());
}
}
}
var i=0;
var child=node.childNodes;
while (child)
{
traverseDOMTree(child);
child=node.childNodes[++i];
}
}
};
Im HTML mache ich dann folgendes:
<script type="text/javascript" src="/strings/de.js"></script>
<body onload="javascript:traverseDOMTree(document.documentElement);">
</body>
welche /strings/de.js ich dannn lade wird dynamisch von der asp App entschieden.
hier ein Auszug aus der de.js
/* German resource strings */
var strings = {
// Anwendung
"app.title":"test",
// Kommandos
"cmd.login":"Anmelden",
"cmd.logout":"Abmelden",
"cmd.registration":"Registrierung",
"cmd.register":"Registrieren",
"cmd.lostpassword":"Passwort vergessen",
}
[/code]
Achso in den Attriobuten und Elementen steht dann immer %app.title% bzw. %cmd.login%
|
|
| 1 |
Das hört sich auch sehr cool an. Wäre super wenn Du das posten könntest. Danke.
– phlow666 15.02.2011
|