Created
May 25, 2021 09:52
-
-
Save bartwttewaall/1674dfc0712dde79324ac6a5c7fa27cc to your computer and use it in GitHub Desktop.
Creates an object with keys from an array and its translated values, then outputs it as a json string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% set labels = ['welcome', 'ok', 'cancel'] %} | |
{% set obj = labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {}) %} | |
{{ obj|json_encode }} | |
{# as a macro #} | |
{% macro translateToJSON(labels) %}{% spaceless %} | |
{{ labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {})|json_encode }} | |
{% endspaceless %}{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment