question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: initialize() with array of objects

See original GitHub issue

Hi @ryandrewjohnson,

I’m opening this issue about the topic, which I started in #21.

The problem in current solution is that, you can only initialize locales with language codes.

import { initialize } from 'react-localize-redux';

const languages = ['en', 'fr', 'es'];
store.dispatch(initialize(languages));

And with this scenario, it is bit hard to get the current language name.

I had to implement my own util functions to display “English” in a <LocaleSwitcher /> component, which is simple dropdown with select tag.

Something like this:

const locales = [
  {
    code: 'en',
    name: 'English',
  },
  {
    code: 'fr',
    name: 'French',
  },
  {
    code: 'es',
    name: 'Spanish',
  },
];

// localeByCode('es') => { code: 'es', name: 'Spanish' }
// this util function is used then <LocaleSwticher /> component
// to display the current language name.
const localeByCode = (code, locales) => {
  const locale = locales.filter(l => l.code === code)[0];
  return locale ? locale : locales[0];
};

// => ['en', 'fr', 'es']
const localeCodes = locales.map(l => l.code);
store.dispatch(initialize(localeCodes));

What i’m proposing is, that to have an ability to pass an array of objects in initialize action creator.

And the API would look something like this:

import { initialize } from 'react-localize-redux';

const languages = [{
    code: 'en',
    name: 'English',
  },
  {
    code: 'fr',
    name: 'French',
  },
];

store.dispatch(initialize(languages));

Also, in this case, getLanguages() and getActiveLanguage() should return different values:

// getActiveLanguage() => { code: 'en', name: 'English', active: true } 
// Instead of: 
// getActiveLanguage() => { code: 'en', active: true }

// getLanguages() => 
[{ code: 'en', name: 'English', active: true }, { code: 'fr', name: 'French', active: false }]
// Instead of: 
// getLanguages() => [{ code: 'en', active: true }, { code: 'fr', active: false }]

Other actions should not be changed, as long as, they don’t need to know about that name key.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ryandrewjohnsoncommented, Oct 19, 2017

@Landish sorry nuked my comment by accident… doing GitHub from phone always a poor choice 😜.

Yea if you have time I’d appreciate the PR. - I think it may be as easy as doing a check on type in the initialize action 1st arg. E.g: If array contains strings than just language codes, and if object with code and name prop use that. The project uses Flow for types and also contains a TypeSxript definition that would need to be updated along with unit tests.

Let me know if you have any questions.

0reactions
Landishcommented, Nov 8, 2017

@ryandrewjohnson I’ve updated to the latest version. Works excellent. Thanks 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array Of Objects In Java: How To Create, Initialize And Use
In this Java Tutorial, you can Learn to Create, Initialize, Sort the Array of Objects in Java with Complete Code Examples.
Read more >
How to create a fixed-size array of objects - Stack Overflow
In Swift, I am trying to create an array of 64 SKSpriteNode. I want first to initialize it empty, ...
Read more >
Array() constructor - JavaScript - MDN Web Docs
The Array() constructor is used to create Array objects. ... A JavaScript array is initialized with the given elements, except in the case...
Read more >
Data Structures: Objects and Arrays - Eloquent JavaScript
Many types of information require more than one atom, though. Objects allow us to group values—including other objects—to build more complex structures.
Read more >
PowerShell Array Guide: How to Use and Create - Varonis
Arrays are a fundamental feature of PowerShell. ... like configuring Office 365 – require that you know how to work with objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found