Get started with node-polyglot CDN

BSD-2-Clause licensed

Node-polyglot: Executes multiple languages in JS Node.js.

Tags:
  • i18n
  • internationalization
  • internationalisation
  • translation
  • interpolation
  • translate
  • polyglot

Stable version

Copied!

How to start using node-polyglot CDN


const Polyglot = require('polyglot') // Use require statement for CommonJS module import

// Create a new instance of the Polyglot translator
const translator = new Polyglot()

// Define translations for different languages
translator.addLocale('en', {
  greeting: 'Hello, {0}!',
  farewell: 'Goodbye, {0}.'
})

translator.addLocale('es', {
  greeting: 'Hola, {0}!',
  farewell: 'Adiós, {0}.'
})

// Load translations from the CDN
const script = document.createElement('script')
script.src = 'https://cdn.cdnhub.io/node-polyglot/2.5.0/index.js'
script.onload = () => {
  translator.load('es') // Load the Spanish translations

  // Translate messages using the translator instance
  const name = 'Alice'
  const greeting = translator.t('greeting', { name })
  const farewell = translator.t('farewell', { name })

  console.log(`Greeting: ${greeting}`) // Output: "Greeting: Hola, Alice!"
  console.log(`Farewell: ${farewell}`) // Output: "Farewell: Adiós, Alice."
}

document.head.appendChild(script)
Copied!
Copied!

All versions