Search code examples
node.jsfirebasei18n-js

Can't get i18n-js working with firebase functions


I have set up a minimal working example of i18n-js:

// located in: src/i18n.ts 
const i18n = require('i18n-js')

i18n.locale = 'en'
i18n.fallbacks = true
i18n.translations = {
  en: {
    hello: 'Hello',
  },
  es: {
    hello: 'Hola',
  },
  fr: {
    hello: 'Bonjour',
  },
}

exports.i18n = i18n

When I try to use it in a firebase functions as follows, I get the error below.

// located in: src/testFunction/testFunction.ts
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const i18n = require('../i18n')
admin.initializeApp()

const testFunction = functions.https.onCall(
  async (
    data: {dummyVar: string},
    context
  ) => {
    console.log(i18n.t('hello'), dummyVar)
  }
)

export default testFunction
// located in: src/index.ts

import testFunction from './testFunction'

exports.testFunction = testFunction

Error Message:

functions: Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Unexpected token 'export'

Do you have an idea?


Solution

  • I didn't manage to find a solution using i18n-js. So I switched to i18next, which now works fine!