Skip to content

Instantly share code, notes, and snippets.

@johndaley-me
Last active August 29, 2015 14:22
Show Gist options
  • Save johndaley-me/4de3935c177b446cfb46 to your computer and use it in GitHub Desktop.
Save johndaley-me/4de3935c177b446cfb46 to your computer and use it in GitHub Desktop.
Lazy Load language files for angular-gettext
<script>
(function () {
// remove ng-app from body
function bootstrapApp() {
angular.element(document).ready(function () {
angular.bootstrap(document, ['myApp']);
});
}
var key = window.localStorage.getItem('language');
if (key && key !== 'en') {
$.ajax({
url: 'languages/' + key + '.js',
dataType: 'script',
success: bootstrapApp,
cache: true // otherwise jQuery reloads this every time with a timestamped query parameter
});
} else {
bootstrapApp();
}
})();
</script>
@johndaley-me
Copy link
Author

This is useful if your app is setup such that it has to completely reload after a language change. The app can window.localStorage.setItem('language', languageId); and then reload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment