搜索和捆绑之前在Javascript取代 [英] Search and replace in Javascript before bundling

查看:94
本文介绍了搜索和捆绑之前在Javascript取代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让捆绑和缩小的过程在ASP.NET MVC应用程序进行查找和替换脚本文件里面minifies面前?

Is there any way to make the bundling and minification process in an ASP.NET MVC-application perform a "search and replace" inside the script files before it minifies them?

我有包含哪些需要被翻译成不同的语言,具体取决于当前用户的语言文字Javascript的文件中定义了一些小部件。由于JavaScript的文件被MVC精缩到ScriptBundles,是否有可能钩到这个构建过程?理想情况下,我们可以使用它来创建本地化的脚本包,脚本里面,其中捆绑过程中进行查找/替换它们是精缩了。

I have some widgets defined in Javascript-files that contain words which need to be translated into different languages, depending on the current user's language. Since the javascript-files are minified into ScriptBundles by MVC, is it possible to hook into this build process? Ideally, we could use it to create localized script bundles, where the bundling process performs the search/replace inside the scripts before they are minified.

我想避免手动创建每种语言独立的JavaScript文件,因为这将使它难以维持。这同样适用于客户端的字典,小部件会拉文从;我们已经与JavaScript性能问题,并且不希望添加前端计算的另一层。

I would like to avoid manually creating separate javascript-files per language, since it would make it difficult to maintain. The same goes for a client-side dictionary that the widgets would pull text from; we already have problems with javascript performance, and do not want to add another layer of front-end calculations.

推荐答案

随着弗拉基米尔说,你可以创建自己的捆绑改造,只需执行 IBundleTransform 。我已经写了捆绑,并与缩小Coffeescripts,可以指向你在正确的方向上的博客文章:的http://tallmaris.com/advanced-bundling-and-minification-of-coffeescripts-in-mvc4/

As Vladimir said, you can create your own Bundle transformation, simply implementing IBundleTransform. I have written a blog post about bundling and minifying Coffeescripts that can point you in the right direction : http://tallmaris.com/advanced-bundling-and-minification-of-coffeescripts-in-mvc4/

总之,创建自定义转换是这样的:

In summary, create a custom transform like this:

public class MultiLanguageBundler : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        foreach (var file in response.Files)
        {
            using (var reader = new StreamReader(file.FullName))
            {
                // "ReplaceLanguageStrings" contains the search/replace logic
                compiled += ReplaceLanguageStrings(reader.ReadToEnd());
                reader.Close();
            }
        }
        response.Content = compiled;
        response.ContentType = "text/javascript";
    }
}

然后在你的 BundleConfig

var myBundle = new Bundle("~/Scripts/localised")
                   .Include("~/JsToLocalise/*.js"); //your JS location here, or include one by one if order is important.
myBundle.Transforms.Add(new MultiLanguageBundler());
myBundle.Transforms.Add(new JsMinify());
bundles.Add(myBundle);

您可能需要调整一些东西,但让我知道如果它可以帮助你。

You may need to tweak a few things but let me know if it helps you.

这篇关于搜索和捆绑之前在Javascript取代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆