javascript - RequireJS: how to add a prefix to path -
i'm developing spa using knockout.js v3 , requirejs.
i have ko components written this:
define(['text!settings.html'], function( htmlstring) { 'use strict'; function settingsviewmodel(params) { ... } // return component definition return { viewmodel: settingsviewmodel, template: htmlstring }; });
now want support localization , have duplicated html each supported language, example:
en/settings.html de/settings.html se/settings.html
i let user change language , refresh app new language, possible instruct require text plugin add language prefix html, when write:
text!settings.html
it load:
text!de/settings.html
not sure if can let text plugin prefix urls. might able create custom template loader:
var templatefromlanguageurlloader = { loadtemplate: function(name, templateconfig, callback) { if (templateconfig.languageurl) { // language config or default language var lang = templateconfig.lang || 'de'; var fullurl = lang + '/' + templateconfig.languageurl; $.get(fullurl, function(markupstring) { ko.components.defaultloader.loadtemplate(name, markupstring, callback); }); } else { // unrecognized config format. let loader handle it. callback(null); } } }; // register ko.components.loaders.unshift(templatefromlanguageurlloader );
then component this:
define([], function() { 'use strict'; function settingsviewmodel(params) { ... } // return component definition return { viewmodel: settingsviewmodel, template: { languageurl: 'settings.html', language: 'nl' // overwrite default } }; });
Comments
Post a Comment