Search code examples
javascriptinternationalizationtranslate

i18n how translate text in javascript


i try translate some text in javascript script. I have:

i18n: {
  messages: {
    en: {
      i18n_profile_alert:'Please draw a geometry with the Geometry selection',
    },
    de: {
      i18n_profile_alert:'Bitte zeichnen Sie mit der Geometry Auswahl eine Geometry',
    },
  },
},

in HTML i can get my translate message like this:

<span>{{$t('i18n_profile_alert')}}</span>

but in javascript it not working:

entegetGeometry() {
    const drawingUtil = vcs.vcm.util.Drawing.getInstance();
    const drawingId = this.$refs.geometrySelection.getDrawingId();
    if (!drawingId) {
      this.notifyError({{$t('i18n_profile_alert')}});
      return;
    }

How can I refer to my message in the javascript code?


Solution

  • If you are working with angular and you have created your project using Angular CLI, then you can use this package. https://www.npmjs.com/package/ng2-translate

    Using ng2-translate you can use multiple languages translation in your project.