简体   繁体   中英

ASP.NET MVC bundle scripts with translation

I have localized MVC according to Nadeem Afana's blog and using a script translator similar to Mads Kristensen's blog . The problem is that bundles are registered in Application_Start method. This causes the scripts are translated in one - default - language and stored in the browser/server cache. Changing the language/culture will not generate their linguistic equivalent. Is it possible to implement a cache profile VaryByCustom = "culture" similarly to Views? Is there any better practice/solution? Is it possible to use HTML5 Application Cache with translated scripts?

Script translator class:

public class ScriptTranslator : IBundleTransform {

    public ScriptTranslator(ResourceManager manager) {
        RM = manager;
    }

    public void Process(BundleContext context, BundleResponse response) {
        response.Content = TranslateScript(response.Content);
    }

    private static ResourceManager RM;
    private static Regex REGEXP = new Regex( @"translate\(""([^\))]*)""\)", RegexOptions.Singleline | RegexOptions.Compiled);

    private string TranslateScript(string text) {
        MatchCollection matches = REGEXP.Matches(text);
        foreach (Match key in matches) {
            object obj = RM.GetObject(kay.Groups[1].Value);
            if (obj != null) {
                text = text.Replace(kay.Value, CleanText(obj.ToString()));
            }
        }
        return text;
    }

    private static string CleanText(string text) {
        return string.Format("\"{0}\"", text.Replace("\\", "\\\\"));
    }
}

Resource files using Custom tool namespace Resources.Javascript; My bundle config is:

public class BundleConfig {
    public static void RegisterBundles(BundleCollection bundles) {

        var DEFAULT_SCRIPT_PATH = "~/Scripts/Modules/";
        var DEFAULT_SCRIPT_BOUNDLE_PATH = "~/ScriptBundles/";

        var mainBoundle = new ScriptBundle(DEFAULT_SCRIPT_BOUNDLE_PATH + "main")
        .Include(DEFAULT_SCRIPT_PATH + "test.js");

        mainBoundle.Transforms.Clear();
        mainBoundle.Transforms.Add(new ScriptTranslator(Resources.JavaScript.test.ResourceManager));
        mainBoundle.Transforms.Add(new JsMinify());

        bundles.Add(mainBoundle);
    }
}

Our solution for this problem was not only to store different bundles by controller/action combination but also by CurrentUICulture.

string.Format("scriptsDependency{0}{1}_{2}", controler, action, Thread.CurrentThread.CurrentUICulture.Name);

I also recommend to store static libraries like JQuery or Bootstrap. But for dynamic content i would recommend NoCache policy.

public class NonCacheableTransformer : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        context.UseServerCache = false;
        response.Cacheability = HttpCacheability.NoCache;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM