json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

translation in selects #834

Closed gatperdut closed 7 years ago

gatperdut commented 7 years ago

hi!

I have a select, its schema looks like this:

    type: {
      type: 'string',
      enum: ['Event', 'BusinessEvent', 'ComedyEvent', ... ],
      default: 'Event'
    }

and its form def like this:

    {
      key: 'type',
      title: $translate.instant('templates.forms.event.type'),
      feedback: false
    }

I'm using angular translate for the i18n. The select works flawlessly, however I haven't found a way to translate the actual items in the enum. Is there a way to do this?

I was thinking of something in the form def like this:

    {
      key: 'type',
      title: $translate.instant('templates.forms.event.type'),
      feedback: false,
      showAs: function(value) {
        return $translate.instant('templates.forms.event.' + value);
      }
    }
gatperdut commented 7 years ago

Would it be possible perhaps to use titleMaps somehow like this?

    {
      key: '@type',
      type: 'select',
      title: $translate.instant('templates.chatbot.forms.event.type'),
      feedback: false,
      titleMap: function(a) {
        return $translate.instant('templates.chatbot.forms.event.' + a)
      }
    }

...instead of having to enumerate all of them with their corresponding i18n:

       titleMap: {
         'Event': $translate.instant('templates.chatbot.forms.event.Event'),
         'BusinessEvent': $translate.instant('templates.chatbot.forms.event.BusinessEvent'),
         ...etc...
       }

Does that make any sense? I think it'd be a cool addition - I can see too how enumerating them explicitly would be a problem in a dynamically fetched enum.

Anthropic commented 7 years ago

@gatperdut the current aim is to remove functions from the ui-schema where we can. Any PR would need to include a method that took a tag or id in the schema and handled the lookup in the add-on ideally.

Have you thought about handling the translation in a function that is the provider of the select? There are a few different select options that look up titleMap from a function.

gatperdut commented 7 years ago

@Anthropic I am using titleMap in the form definition. Now I'm wondering if I should be doing that at all.

I'll give a try to your suggestion with the function.

gatperdut commented 7 years ago

worked fine, thank you very much! I still believe though a function in the form definition assigned to titleMap would be very useful.

Anthropic commented 7 years ago

@gatperdut there is such an option in the Material decorator already.

You can look up the example for titlemaps.json used by the controller in example.html and see that with the following form definition it looks up the value on the scope, I intend on adding this basic ability to bootstrap too eventually.

Loading via function call

  $scope.arnieFlix = loadFlix();

Displaying based on watching the scope variable

  {
    "key": "selectOptionData",
    "type": "select",
    "optionData": "arnieFlix"
  }

I just haven't had the time to port it over yet.