Giter Club home page Giter Club logo

Comments (2)

adams85 avatar adams85 commented on June 25, 2024

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.

from bundling.

topgun743 avatar topgun743 commented on June 25, 2024

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;
        }
    }

from bundling.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.