sanity-io / sanity-plugin-internationalized-array

A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
MIT License
42 stars 10 forks source link

Default language #17

Closed gabrielmaldi closed 1 year ago

gabrielmaldi commented 1 year ago

Is it possible to define a default language, so that the user does not need to click + EN?

i.e. when the document is rendered, get this:

Screenshot 2023-06-12 at 07 29 50

instead of this:

Screenshot 2023-06-12 at 07 29 43

Thanks!

SimeonGriggs commented 1 year ago

Sure, set an initialValue on the field: https://www.sanity.io/docs/initial-value-templates

gabrielmaldi commented 1 year ago

Surely I'm missing something, but using:

defineField({
  name: 'name',
  type: 'internationalizedArrayString',
  title: 'Name',
  initialValue: [{_key: 'en', value: 'hello'}],
})

renders:

Screenshot 2023-06-12 at 10 16 47
Mtillmann commented 1 year ago

I'm also stuck with this issue. The docs do not suggest how make this work

SimeonGriggs commented 1 year ago

Ah of course.

_key's and _id's can't be set with initial values in schema. Will report back with a solution.

SimeonGriggs commented 1 year ago

(in the mean time it does work at the document-level!)

export default defineType({
  name: 'post',
  title: 'Post',
  type: 'document',
  initialValue: {
    "strings": [
      {
        "_type": "internationalizedArrayStringValue",
        "_key": "en",
        "value": 'hello'
      },
      {
        "_type": "internationalizedArrayStringValue",
        "_key": "fr",
        "value": 'bonjour'
      }
    ]
  },
  fields: [
    defineField({
      name: 'strings',
      description: 'Internationalized array of STRING',
      type: 'internationalizedArrayString',
    }),
  ]
})
gabrielmaldi commented 1 year ago

Great! Just to be clear: my intention is to spare the user of having to click on EN on each field, i.e. have every internationalized input pre-rendered for English but without a value. Maybe as a workaround value: "" can be used (and that'd be fine with me), but a nicer solution would be to perhaps set defaultLanguage: "en" at the document level and have the plugin do the heavy lifting behind the scenes.

Thank you!

SimeonGriggs commented 1 year ago

Version 1.7.0 now supports a defaultLanguages config setting which will create empty array items for specific languages in new documents.

gabrielmaldi commented 1 year ago

Great, thank you!