Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to apply transforms to a Bundle like we used to do in Asp.Net MVC 4? #2

Closed
topgun743 opened this issue Nov 5, 2018 · 2 comments

Comments

@topgun743
Copy link

In Asp.Net 4 we use to do bundling via BundleConfig => RegisterBundles via the following way:

    foreach (var culture in languageCultures)
    {
        var modulesGlobalsJSBundle = new Bundle(string.Format("~/Scripts/modulesGlobalsJS-{0}", culture.code))
        .Include("~/Scripts/MyModules/build/my-modules.js");

       modulesGlobalsJSBundle.Transforms.Clear();
       modulesGlobalsJSBundle.Transforms.Add(new JSTranslator());
       modulesGlobalsJSBundle.Transforms.Add(new JsMinify());

      bundles.Add(modulesGlobalsJSBundle);
    }

That is, a separate bundle will be created for each culture available via list of languageCultures.

Now I can't find way in Asp.Net Core to apply transform to my static files application startup. We used JSTranslator earlier as a transform (shown in code above) for replacing any localizable strings present in .js files w.r.t current UI culture.

How can we do that using your way of Bundling and Minification?
A sample would be of great help. Thanks in advance.

@adams85
Copy link
Owner

adams85 commented Nov 8, 2018

I put together a code sample demonstrating how to achieve this using my library.

I don't know where the JSTranslator type comes from (I assume it's some custom component) so I implemented a simple translator transformation. To plug in your own implementation, all you need to do is implement the IBundleItemTransform interface (or inherit from BundleItemTransform class) and register your bundles using this transformation as shown here.

Furthermore, I use PO-based translation in the sample. It can be replaced to a resource based or other localization mechanism easily. Just read my comments in the Startup class.

@topgun743
Copy link
Author

Thanks Adams for guiding.
I have implemented Translator my way.
It changes current culture via a dropdown available on site footer, just as you described in your sample app. Localized JS bundle is loaded via
<script src="~/bundles/js/modules/myModulesBundle.@(currentCulture.Name).js"></script>

Bundling is done in Startup.cs =>Configure() method like so:

        app.UseBundling(bundles =>
        {
            // building an advanced configuration in code
            //bundles.AddCss("/virtual-path/to/bundle.css")
              //  .Include("/physical-path/to/include.css")
                //.Include("/another/physical-path/to/pattern*.css")
                //.Exclude("/**/*.min.css");

            //bundles.AddLess("/virtual-path/to/less-bundle.css")
              //  .Include("/physical-path/to/main.less");
            
            foreach (var culture in SupportedCultures)
            {
                bundles.AddJs($"/js/modules/myModulesBundle.{culture.Name}.js")
                    .Include("/js/module1.js")
                    .Include("/js/module2.js")
                    // this is THE KEY STEP: insert the translator in the bundle item transformation pipeline
                    .UseItemTransforms(transforms => transforms.Insert(0, new JSTranslator()));
            }
        });

And here is my JSTranslator component

public class JSTranslator : BundleItemTransform
    {
        public override void Transform(IBundleItemTransformContext context)
        {
            string translated = TH.STranslator(context.Content);  // TH is some Helper class to do the exact translation
            context.Content = translated;
        }
    }

@adams85 adams85 closed this as completed Dec 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants