angular-dynamic-locale icon indicating copy to clipboard operation
angular-dynamic-locale copied to clipboard

Any examples for loading directly into cache?

Open amritk opened this issue 8 years ago • 11 comments

I'll probably create a gulp plugin or something that would load selected locales directly into the cache so there's no need to load a file on language change. Could you provide an example of what to load into the cache?

amritk avatar Mar 31 '16 20:03 amritk

I think also that something like this would be really valuable.

@lgalfaso does the library support loading a data uri via in a way like localeLocationPattern(datauri) ?

if yes one in the build process could simply do a rewrite from localeLocationPattern(/file/path/for/lang) to localeLocationPattern(dataUriOf(/file/path/for/lang))

We are doing it in GlobaLeaks in our build process for other reasons and it works great in general: https://github.com/globaleaks/GlobaLeaks/blob/master/client/Gruntfile.js#L9

evilaliv3 avatar Apr 14 '16 22:04 evilaliv3

@evilaliv3 it should work in any browser that allows <script src="data:text/javascript;base64,..."></script>, but it is kind of clunky. That I why I just cut release 0.1.32 that adds the method tmhDynamicLocaleProvider.addLocalePatternValue to add a local value to the interpolation locale location pattern. Eg.

// Untested code.
tmhDynamicLocaleProvider.addLocalePatternValue('base64Locales',
  {
    'en': 'data:text/javascript;base64,TheEnglishLocaleInBase64',
    'fr': 'data:text/javascript;base64,TheFrenchLocaleInBase64'
});
tmhDynamicLocaleProvider.localeLocationPattern('{{base64Locales[locale]}}');

If you would like to give it a spin, it would be great!

lgalfaso avatar Apr 15 '16 20:04 lgalfaso

I am sure that this can be cleaned up a lot, but given the number of people that are asking for this I will post it here.

Note: This only works with versions 0.1.32 or newer.

  • Create a file generateLocales.js with this content https://gist.github.com/lgalfaso/cb18110aa2d87279884dd18fe1fbf851
  • Modify generateLocales.js to generate the locales you are interested in.
  • From a command prompt run
npm install angular-i18n
node generateLocales.js > tmhPreloadedLocales.js
  • Include tmhPreloadedLocales.js into your project
  • Add the module 'tmh.dynamicLocalePreload' to the dependencies of your project

lgalfaso avatar Apr 16 '16 14:04 lgalfaso

i leave here some comment so that we can iterate on a good solution eventually to be integrated in the library.

new Buffer(content).toString('base64') does not require to use other libraries not already included in node like js-base64

i will be back also when finalized the solution that we are integratin in GlobaLeaks

evilaliv3 avatar Apr 16 '16 15:04 evilaliv3

@evilaliv3 thanks, updated the code and the comment.

lgalfaso avatar Apr 16 '16 18:04 lgalfaso

Thanks for your work guys! In case anyone is trying to use this with gulp (like me), here's a more elaborate example.

schnatterer avatar Jul 21 '16 08:07 schnatterer

Hey, I have the same problem this solution worked for me on chrome and firefox but not work on IE @lgalfaso if you have an idea how to solve it on IE

yboug avatar Sep 27 '16 12:09 yboug

@yboug the solution posted should work with relative new version of IE without any issues. For older versions you will have to work around the limitation of IE on data schema in script. A possible workaround would be:

Modify the solution in this thread so it generates a module that:

  • Keeps a map where in one side is the locale key and in the other side a function that wraps the angular locale implementation (the body of the function would be the content of the angular locale file for the given locale). I am going to call this map localeMap
  • Creates an implementation of $cache (that I am going to call localeCache) that, when asked for a key (that is one of the key that should be pre-loaded), does:
    • Looks for the function associated with the locale key at localeMap
    • Executes this function
    • Returns angular.injector(['ngLocale']).localInjector.get('$locale');
  • Decorate the service tmhDynamicLocaleCache and replace it with localeCache

If you put this script together, please post it in this thread.

lgalfaso avatar Sep 27 '16 21:09 lgalfaso

@lgalfaso Thanx for your response. I use IE 9. So i dont'think thats this problem come from IE version

yboug avatar Sep 28 '16 08:09 yboug

@lgalfaso i use another approch to manage this: i add a gulp task: 1) gulp.task('angularI18n',function () { return gulp.src('bower_components/angular-i18n/**.js') .pipe(gulp.dest(path.join(conf.paths.dist, '/angular-i18n'))) });

  1. add 'tmh.dynamicLocale' to angular.module 3)add this two line into app.config.js tmhDynamicLocaleProvider.localeLocationPattern('angular-i18n/angular-locale_{{locale | lowercase}}.js'); tmhDynamicLocaleProvider.defaultLocale(navigator.language);

yboug avatar Sep 29 '16 12:09 yboug

Hi, The solution you posted is good and it solves the issue of how to pack the locales in your app. It does not solve the other issue raised here that is to populate the cache with the locales so they do not have to be loaded from the server.

Anyhow, thanks for your post and I hope that this also help other developers.

On Thu, Sep 29, 2016 at 2:38 PM, yboug [email protected] wrote:

@lgalfaso https://github.com/lgalfaso i use another approch to manage this: i add a gulp task: 1) gulp.task('angularI18n',function () { return gulp.src('bower_components/angular-i18n/**.js') .pipe(gulp.dest(path.join(conf.paths.dist, '/angular-i18n'))) });

  1. add 'tmh.dynamicLocale' to angular.module 3)add this two line into app.config.js tmhDynamicLocaleProvider.localeLocationPattern(' angular-i18n/angular-locale_{{locale | lowercase}}.js'); tmhDynamicLocaleProvider.defaultLocale(navigator.language);

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lgalfaso/angular-dynamic-locale/issues/93#issuecomment-250454030, or mute the thread https://github.com/notifications/unsubscribe-auth/AAX44gyYWJA8TM78YOVz2M2NlbN_B9gVks5qu7FHgaJpZM4H9KMY .

lgalfaso avatar Sep 29 '16 18:09 lgalfaso