rmariuzzo/Lang.js

Is it possible to access all keys from a lang file?

kevinsalter opened this issue · 2 comments

Thanks for your great work on this @rmariuzzo!

I'm wondering, is it possible to access all the keys in a given lang file? For example, say I have a lang file called person-questions and it contains things like :

	"name" => "What is your name?",
	"age" => "How old are you?",
	"hobbies" => "What are your hobbies?"

Is there a way, using Lang.js to grab a list of the keys, getting an array of values like ['name', 'age', 'hobbies']?

The use case is that I'd like to populate a select menu with options, and I'd like to always include every string from a certain lang file. Thanks.

Hey @kevinsalter! Thanks for using and asking this question. To answer directly: no, there's no built-in way to that via Lang.js.

However, you could achieve something similar if you do:

const messages = {
  'en.dropdown.level': {
    'low': 'Low',
    'medium': 'Medium',
    'high': 'High',
  } 
}

const lang = new Lang(messages);

// Later...

console.log(Object.keys(messages['en.dropdown.level']));
// > ['low', 'medium', 'high']

Does this help?

Awesome, thanks so much for the response! 🙏