Irrelon / jquery-lang-js

i18n Allow instant language switching on HTML pages without reloading the page.
https://www.irrelon.com
362 stars 132 forks source link

about complete callback in init. #100

Closed weswithley closed 7 years ago

weswithley commented 7 years ago

Hi, Irrelon,

First above all, thanks for ur awesome works, it makes a lot of amazing things on switch multi language experience. but still a little suggestion on "about to add a complete callback while init is done."

I've tried a lot ways to use the orginal method to reach that. but still can't find the way out. so, i added some little fix. here is my fixed code :

Lang.prototype.init = function (options) {
        var self = this,
              cookieLang,
              defaultLang,
              currentLang,
              allowCookieOverride,
              callbackInit;    //////// fix part1

        options = options || {};
        options.cookie = options.cookie || {};

        defaultLang = options.defaultLang;
        currentLang = options.currentLang;
        allowCookieOverride = options.allowCookieOverride;
        callbackInit = options.callbackInit;    //////// fix part2

        // Set cookie settings
        this.cookieName = options.cookie.name || 'langCookie';
        this.cookieExpiry = options.cookie.expiry || 365;
        this.cookiePath = options.cookie.path || '/';

        // Store existing mutation methods so we can auto-run
        // translations when new data is added to the page
        this._mutationCopies = {
            append: $.fn.append,
            appendTo: $.fn.appendTo,
            prepend: $.fn.prepend,
            before: $.fn.before,
            after: $.fn.after,
            html: $.fn.html
        };

        // Now override the existing mutation methods with our own
        $.fn.append = function () { return self._mutation(this, 'append', arguments) };
        $.fn.appendTo = function () { return self._mutation(this, 'appendTo', arguments) };
        $.fn.prepend = function () { return self._mutation(this, 'prepend', arguments) };
        $.fn.before = function () { return self._mutation(this, 'before', arguments) };
        $.fn.after = function () { return self._mutation(this, 'after', arguments) };
        $.fn.html = function () { return self._mutation(this, 'html', arguments) };

        // Set default and current language to the default one
        // to start with
        this.defaultLang = defaultLang || 'en';
        this.currentLang = defaultLang || 'en';

        // Check for cookie support when no current language is specified
        if ((allowCookieOverride || !currentLang) && typeof Cookies !== 'undefined') {
            // Check for an existing language cookie
            cookieLang = Cookies.get(this.cookieName);

            if (cookieLang) {
                // We have a cookie language, set the current language
                currentLang = cookieLang;
            }
        }

        $(function () {
            // Setup data on the language items
            self._start();

            // Check if the current language is not the same as our default
            if (currentLang && currentLang !== self.defaultLang) {

                // Switch to the current language
                //////// fix part3
                if(callbackInit){
                    self.change(currentLang, callbackInit);
                }else{
                    self.change(currentLang);
                }
            }
        });
    };

Hope this can be a little help for ur great works. Thanks a lot. Wish u have a good day.
: )