Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error "System is not defined(…)" INTERNATIONALIZATION (I18N), angular-cli

Tags:

angular

I was trying to learn this tutorial with JiT and angular-cli. I have got the following errors in browser console but no error in terminal to run/build the app.

Uncaught ReferenceError: System is not defined(…)

Uncaught Error: Cannot find module './locale/messages.fr.xlf!text'.(…)

enter image description here

My code in github https://github.com/sayedrakib/internationalization_project.git

I have added systemjs-text-plugin.js in the root folder, according to instruction in the tutorial. File structure enter image description here

I think I have messed up with file path in some files, because I felt the assumed file structure in tutorial and angular-cli created file structure is not same.

like image 991
SayedRakib Avatar asked Nov 22 '16 14:11

SayedRakib


2 Answers

I can see you are using angular-cli, than if you want to load 3party libery you need to work with angular-cli.json file.

First run at your app directory npm install systemjs Second you need to add the path of systemjs at your angular-cli.json file.

"scripts": [
    "../node_modules/systemjs/dist/system.js"
  ]

if you need to load other 3party libery just repeat the 2 stages.

like image 101
Yoav Schniederman Avatar answered Oct 02 '22 20:10

Yoav Schniederman


Took me ages to figure this out!

If you followed the documentation, change this:

  1. Download system.js, but it in root (/app) and import it before the script tag.
  2. In index.html remove the System.import('app')
  3. Rename everything from System to SystemJS (index.html, i18n-provides.ts)

Finally it looks like this:

index.html

<body>
  <app-root>Loading...</app-root>

  <script src="system.js"></script>
  <script>
    // Get the locale id somehow
    document.locale = 'es';

    // Map to the text plugin
    SystemJS.config({
      map: {
        text: 'systemjs-text-plugin.js'
      }
    });
  </script>
</body>

i18n-providers.ts

export function getTranslationProviders(): Promise<Object[]> {
   [...]
}

declare let SystemJS: any;

function getTranslationsWithSystemJs(file: string) {
  return SystemJS.import(file + '!text'); // relies on text plugin
}

I opened a GitHub Issue to update documentation.

like image 40
Mick Avatar answered Oct 02 '22 19:10

Mick