Replacement step code to fetch the ll cc.js token file for multi lingual - akumina/AkuminaTraining GitHub Wiki

Applies to

Akumina Foundation 4.0.0.0 and later

See also Akumina JS Framework Performance Considerations

Overview

The replacement step code here is for multi lingual sites who want to optimize performance by not downloading the ll-cc.js token files.

Adding the language file content

The contents of the ll-cc.js language files in the path /Style%20Library/DigitalWorkplace/Content/language/ must be brought into the digitalworkplace.custom.js file, or a file of your choice. This is in place of the framework dynamically loading this file. Since we have multiple languages, we will set different variables, then set a default on the lang object. In the step code, we will switch languages as needed.

As a first step, we copy the ll cc files locally in your project, in the path src/lang/en-us.js, for example.

For each file, we add its contents to a variable, Akumina.Digispace.SiteContext["ll-cc-lang"]. So for example, in the en-us.js file,

lang = {
  ...

becomes

Akumina.Digispace.SiteContext["en-us-lang"] = require("../src/lang/en-us.js");

when we add it to the digitalworkplace.custom.js file. Note, we can automate this step as part of our build process. We then set lang to our default language:

// set to default
lang = Akumina.Digispace.SiteContext["en-us-lang"];

The result is shown below:

language objects in custom.js

Step

The LOADER_STEPS_ENABLE_LOADUSERLANGSETTINGS step must be disabled, either via the configuration or the following line in LoaderConfiguration.Custom:

// do not fetch the ll-cc.js TOKEN file
Akumina.Digispace.ConfigurationContext.CONSTANTS.LOADER_STEPS_ENABLE_LOADUSERLANGSETTINGS = false;

LoaderConfiguration.Custom

Code

The code below is similar to CoreSteps.LoadUserLanguage, but will use the language objects above instead of downloading the files.

The main function is AdditionalSteps.MoreSteps.LoadUserLangSettings.

var AdditionalSteps = AdditionalSteps || {
}

//You can call "MoreSteps" anything you would like, as long as it is apart of the "AdditionalSteps" object
//You can also create multiples, ie  AdditionalSteps.MyAdvancedSteps, AdditionalSteps.SomethingElse, it will always run  AdditionalSteps.XXYY.Init()
if ((typeof AdditionalSteps.MoreSteps) === 'undefined') {

    AdditionalSteps.MoreSteps = {

        Init: function () {
            
            Akumina.AddIn.Logger.WriteInfoLog('AdditionalSteps.MoreSteps');

            var steps = [];

            steps.push(
                { 
                    stepName: "Detect Multiple SiteVisible Languages", 
                    additionalSteps: [
                        { 
                            name: "Load User Lang Settings And Tokens", callback: AdditionalSteps.MoreSteps.LoadUserLangSettings 
                        }
                    ] 
                }
            );

           return steps;

        },
        LoadUserLangSettings: function () {
            debugger;
            Akumina.AddIn.Logger.WriteInfoLog('AdditionalSteps.MoreSteps.LoadUserLangSettings');

            if (Akumina.Digispace.ConfigurationContext.IsMultiLingualEnabled) {
                //Try to get language from QueryString
                var querystringLang = CoreSteps.LoadUserLanguage.GetLanguageFromQueryString().done(function (language) {
                    if (language != null) {
                        var userlang = { languageCode: language.languageCode, languageId: language.languageId, fallbackLanguageId: language.fallbackLanguageId };
                        Akumina.Digispace.UserContext.SetLanguage(userlang);
                        AdditionalSteps.MoreSteps.LoadUserLanguageTokens(userlang);
                    }
                    else {
                        //Try to get language code from User Context
                        var language = Akumina.Digispace.UserContext.GetLanguage();

                        if (language != null) {
                            AdditionalSteps.MoreSteps.LoadUserLanguageTokens(language);
                        }
                        else {
                            //get user's language settings (default en-us)
                            CoreSteps.LoadUserLanguage.GetUserLanguage().done(function (language) {
                                AdditionalSteps.MoreSteps.LoadUserLanguageTokens(language);
                            });
                        }
                    }
                });
            }
            else {
                var language = Akumina.Digispace.UserContext.GetLanguage();

                if (language != null) {
                    AdditionalSteps.MoreSteps.LoadUserLanguageTokens(language);
                }
                else {
                    //get user's language settings (default en-us)
                    var language = {
                        languageCode: "en-us", languageId: 1033
                    };
                    AdditionalSteps.MoreSteps.LoadUserLanguageTokens(language);
                }
            }
        },
        LoadUserLanguageTokens: function (language) {
            var userlang = { languageCode: language.languageCode, languageId: language.languageId, fallbackLanguageId: language.fallbackLanguageId };
            Akumina.Digispace.UserContext.SetLanguage(userlang);

            lang = Akumina.Digispace.SiteContext[language.languageCode.toLowerCase() + "-lang"];

            Akumina.Digispace.AppPart.Eventing.Publish('/loader/onexecuted/');
        }
    }
}

Result

After the site is deployed, the language tokens will no longer be obatined from the ll-cc.js files.

References