Search code examples
javascriptbackbone.jsinternationalizationrequirejs

Using require.js with a unique CDN


I am working on a require.js application to localize it. We have our nls files in a specific folder structure.

nls/
   fr/
     register.js
   ...
   register.js

When we push the app to production our build system adds a build number to all files like so main.54b8801bf2c6e13b.js​​. This does work Locally on my machine because we do not add the unique number.

Require.js does not bring in any js files in the subfolders for example nls/fr/register.js will not get loaded in the main.js bundle (expected). When our app comes across this in staging, it goes to pull nls/fr/register.js from the CND but looks for nls/fr/register.js instead of nls/fr/register.54b8801bf2c6e13b.js​​ (yes the file exists in our CDN)

Does anyone know of a way to dynamically have require.js look for nls/fr/register.54b8801bf2c6e13b.js​​. I think we haven't run into this before because all JS files are loaded up into the main.js file (which I am not opposed to doing here as well if it is easier)

nls/register.js

define(['text!json/root/register.json', 'settings'], function(json, settings) {
    return {
        "fr": true
    }
});

nls/fr/register.js

define(['text!json/fr/register.json'], function(json) {
    return JSON.parse(json);
});

views/register.js

 define([
  'jquery',
  'plugins/ui.1.11.0',
  'underscore',
  'backbone',
  'views/base_view',
  'settings',
  'utils/gtm',
  'utils/storage',
  'text!templates/login_registration/registration_modal.html',
  'i18n!nls/register'
], function($, jquery_ui_is_jquery_plugin, _, 
  Backbone,
  BaseView,
  Settings,
  gtm,
  Storage,
  Template_Registration,
  RegisterLocales) {
//code to get locales and set up view
}

Solution

  • I solved this by adding the NLS files to my require.js compiled file (main.js)via my grunt task (grunt-contrib-requirejs)

    modules: [
              {
                name: 'main',
                include: [
                  'nls/de/activity_types',
                  'nls/de/admin',
                  'nls/de/app_callout',
                  'nls/de/browser_support'
                   ]
                }]