Advertisement
Sumafu

i18n-polyfill fix

Jun 6th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. The documentation says that you need this code in your app.module.ts to load language files dynamically:
  2. {
  3. provide: TRANSLATIONS,
  4. useFactory: (locale) => {
  5. locale = locale || 'en'; // default to english if no locale provided
  6. return require(`raw-loader!../locale/messages.${locale}.xlf`);
  7. },
  8. deps: [LOCALE_ID]
  9. }
  10.  
  11. But you have to keep in mind that this will fail if you try to load your default language file.
  12. For example we write our App in German and then generate with ng xi18n a translation.de.xlf file. This default file does
  13. correctly only have a source language and no target language. But without them i18n-polyfill will crash. So you have to
  14. extend the code:
  15.  
  16. {
  17. provide: TRANSLATIONS,
  18. useFactory: (locale) => {
  19. locale = locale || 'en';
  20. if (locale === 'de') { // de is our default
  21. return '';
  22. } else {
  23. return require(`raw-loader!../../i18n/translation.${locale}.xlf`);
  24. }
  25. },
  26. deps: [LOCALE_ID]
  27. }
  28.  
  29. Here you return nothing but an empty string if your app runs with the default language.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement