1

I am having the following structure in my project:

locale
|_ nl
|   |_ country.ts
|_ en
|   |_ country.ts
|_ de
    |_ country.ts

Each country.ts contains a lookup object for the translation:

export default {
  'AFG': 'Afghanistan',
  'ALA': 'Åland',
  'ALB': 'Albanië',
  'DZA': 'Algerije',

Currently I am using the NL file like:

import CountryNL from 'locale/nl/country';
let locale = CountryNL['ALB'];

However to use the other locales I need dynamic importing a module. As stated in Importing modules using ES6 syntax and dynamic path this is not possible.

What would be the best approach to dynamically get the correct language file ?

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91

1 Answers1

0

First create a language object {}. List the contents of the directory recursively to get all the contents. Filter by .json extension. Read in the file and parse the json. Add the json to you master language object using the file name as a key.

Lockless
  • 497
  • 1
  • 5
  • 12
  • I thought about that, but the language can change for each request so then I need to load all languages beforehand in one big object. Didn't know if that would be the best :) – Bas van Dijk Jul 09 '18 at 20:04