0

I have a translation function I call like this : _("My text").

I am trying to build an array :

    var data = {
                        _("region"):[
                            {"id": "vignoble","name": _("Vineyard") },
                            {"id": "sousvignoble","name": _("Sub Vineyard") },
                            {"id": "soussousvignoble","name": _("Sub Sub Vineyard") },
                            {"id": "appellation","name": _("Appellation") },
                            {"id": "denomination","name": _("Denomination") }
                        ]};

It works great for translating property "name" : _("My text") but throws a "Syntax Error : Missing : after property id" for first level _("region").

I've tried to replace it by : 1. function(){_("region")} 2. define a variable and replace _("region") by the variable but the name of variable prints and not its value

Any idea would be very appreciated !

1 Answer 1

2

You cannot use dynamic property names in object literals. You can assign the property after the object was created, using bracket notation:

var data = {};
data[_("region")] = [...];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.