question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sync loading with Webpack

See original GitHub issue

I was looking into preloading a few locales so that async loading wouldn’t be required (I’m using this in cordova so it wouldn’t make much sense) and I came up with this:

  function getInjectedLocale() {
    var localInjector = angular.injector(['ngLocale']);
    return localInjector.get('$locale');
  }

  // put de-de language into cache
  require('angular-i18n/de-de');
  tmhDynamicLocaleCache.put('de-de', getInjectedLocale());

  // put en-gb language into cache
  require('angular-i18n/en-gb');
  tmhDynamicLocaleCache.put('en-gb', getInjectedLocale());

  // later on:
  tmhDynamicLocale.set('en-gb'); // or de-de

Seems to work very well, I’m leaving this here as a possible addition to the readme. A possible improvement might be to create a method in tmhDynamicLocale, like tmhDynamicLocale.cacheLastLoadedLocale('de-de') for instance, which can be called right after the require.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:11
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
andreialecucommented, Mar 28, 2016

@ChristianUlbrich The above code assumes you’re using webpack.

You can just put it inside a .run call like this:

app.run(function(tmhDynamicLocale, tmhDynamicLocaleCache) {
  ...
});

If you’re not using webpack, then I’m not sure how this helps you or why you would need it. Care to elaborate?

3reactions
mehl321commented, Feb 1, 2017

This chunk of code was helpful to get started but I took another path with webpack to tackle async issues. It’s using ui-router.

In webpack make a bare copy of the locals so they are packaged but not (yet) imported.

var copyStatic = new CopyWebpackPlugin([
  {from: './node_modules/angular-i18n/angular-locale_de-de.js', to: path.resolve(APP_ROOT, appConfig.buildDir + '/i18n')},
  {from: './node_modules/angular-i18n/angular-locale_en-gb.js', to: path.resolve(APP_ROOT, appConfig.buildDir + '/i18n')},
]);

Set the path to the locales in the angular config.

tmhDynamicLocaleProvider.localeLocationPattern('i18n/angular-locale_{{locale}}.js');

Finally created a ui-router hook to make sure that the locale is always set before your logic starts executing.

$transitionsProvider.onBefore({}, setLocale, { priority: 20 });
function setLocale(transition) {
  const tmhDynamicLocale = transition.injector().get('tmhDynamicLocale');
  return tmhDynamicLocale.set('fr')
    .catch((err) => {
      console.warn('Error setting locale. Using default locale.', err);
    });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Loader Interface - webpack
Loaders were originally designed to work in synchronous loader pipelines, like Node.js (using enhanced-require), and asynchronous pipelines, like in webpack.
Read more >
Module federation: sync load support · Issue #15240 - GitHub
What is the current behavior? crash with error. Uncaught Error: Shared module is not available for eager consumption: webpack/sharing/consume/ ...
Read more >
Synchronous require in Webpack - javascript - Stack Overflow
Webpack loades it synchronously but each file have its own scope. That's why in your statement import * as d3 from 'd3'; \\I'm...
Read more >
Synchronous Loading | Maps JavaScript API
In this example, the Maps API is loaded synchronously. We omitted the async and defer attributes from the script tag that loads the...
Read more >
Webpack and Dynamic Imports: Doing it Right - Medium
The above code will load the foo module at runtime, and resolving it, will log the default export of the module. As the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found