Сбой учебника / примера i18next с синтаксической ошибкой "import i18n from 'i18next';" - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь создать небольшую демонстрацию на основе этой статьи: https://phrase.com/blog/posts/node-js-i18n-guide/

Когда я пытаюсь запустить приложение для создания папки локалей, оно не работает:

jj@DESKTOP-123:/mnt/c/Users/jj/source/repos/langdemo$ node --experimental-modules index.mjs
(node:348) ExperimentalWarning: The ESM module loader is experimental.
file:///mnt/c/Users/jj/source/repos/langdemo/app/i18n.config.mjs:3
import i18n from 'i18next';
       ^^^^
SyntaxError: Unexpected identifier
    at translators.set (internal/modules/esm/translators.js:43:18)

Пакет. json код:

jj@DESKTOP-123:/mnt/c/Users/jj/source/repos/langdemo$ cat package.json
{
  "name": "langdemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "i18next": "^19.6.0"
  }
}

** index.m js **

jj@DESKTOP-123:/mnt/c/Users/jj/source/repos/langdemo$ cat index.mjs
import { LocaleService } from './app/services/localeService.mjs';
import i18n from './app/i18n.config.mjs';

const localeService = new LocaleService(i18n);

console.log(localeService.getLocales()); // ['en', 'el']
console.log(localeService.getCurrentLocale()); // 'en'
console.log(localeService.translate('Hello')); //  'Hello'
console.log(localeService.translatePlurals('You have %s message', 3)); // 'You have 3 messages'

файл конфигурации

jj@DESKTOP-123:/mnt/c/Users/jj/source/repos/langdemo$ cat ./app/i18n.config.mjs
https://github.com/mashpie/i18n-node#list-of-all-configuration-options

import i18n from 'i18next';
import path from 'path';
i18n.configure({
  // this is the list of language that will be supported
  locales: ['en','el'],
  defaultLocale: 'en',
  queryParameter: 'lang',
  directory: path.join('./', 'locales'), //this directory will be used to autogen'ing the files and strings needed by the engine.
  api: {
    '__': 'translate',
    '__n': 'translateN'
  },
});
export default i18n;

Я очень зелёный с узлом в целом. И с этим модулем перевода. Так что любые советы будут оценены. Спасибо.

...