Koala7
Koala7

Reputation: 1404

Converting the values iso languages code in languages with React

I am trying to make an idea on how to convert my existing data mock where i have ISO codes which i want to convert into languages name.

I am using => https://github.com/pubcore/iso-language-codes ( Readme has actually 2 small errors, the example is not importing from iso-language-codes and code = iso639_2T['fra'] should be code = by639_2T['fra'] )

This is the data mock i have:

export const dataForSubtitlesMenu = [
  { value: 'Lingue Audio', title: true, id:1},
  { value: 'ita', id: 2},
  { value: 'eng', id: 3},
  { value: 'fra', id: 4},
  { value: 'ben', id: 5},
  { value: 'gla', id: 6},
  { value: 'ind', id: 8},
  { value: 'nor', id: 9},
  { value: 'rus', id: 10},
  { value: 'slv', id: 11},
]

This is my app.js

<Dropdown title="Seleziona lingua e sottotitoli" items={dataForSubtitlesMenu}/>

And in Dropdown.js i do like this

<ul className="dd-list">
  {items.map((item, i) => (
    <ListItem
      item={item}
    />
  ))}
</ul>

There are not examples and clear demos, how can i convert my values in languages code using this module iso-languages-code? Is there any other better node to achieve this with my existing data mock?

Upvotes: 0

Views: 543

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 186994

It looks like you want to just create an entry for each language in that library?

If so, the default export is just an array of all data. So you should be able to:

import codes from 'iso-country-codes'

export const dataForSubtitlesMenu = [
  // Add the prompt item
  { value: 'Lingue Audio', title: true, id:1},

  // Fill the rest of the array with an item for every language.
  ...codes.map((lang, i) => ({
    value: lang.iso639_2T,
    id: i + 2, // i starts at 0, id 1 is the prompt.
  })
]

Upvotes: 1

Related Questions