Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale Support #121

Closed
ekasprzyk opened this issue Mar 29, 2021 · 4 comments
Closed

Locale Support #121

ekasprzyk opened this issue Mar 29, 2021 · 4 comments

Comments

@ekasprzyk
Copy link

I came across this tool and it's really nice. However, I'm a bit confused at the typing on the fields, because as far as I can see, this isn't going to work for anyone, unless we have some special config here.

All our fields from Contentful always have the format:

fields: {
  field_name: {
    [LOCALE]: data
  }
}

and not

fields: {
  field_name: data
}

as this library generates.

We have typings for the locales, if you want them.

@marcolink
Copy link
Collaborator

marcolink commented Apr 1, 2021

Hi @ekasprzyk
The current output of this library only creates non-localized entries.

To achieve localized entries, I suggest a transformation on type level: Here is a little example how it would look like.

type LocalizedFields<Fields, Locales extends keyof any>  = {
    [FieldName in keyof Fields]?: {
      [LocaleName in Locales]?: Fields[FieldName];
    }
}

type LocalizedEntry<EntryType, Locales extends keyof any>  = {
 [Key in keyof EntryType] : 
  Key extends 'fields' 
  ? LocalizedFields<EntryType[Key], Locales>
  : EntryType[Key]
}

type MyFields = {
  a: string
  b?: number
}

type MyEntry = Entry<MyFields>

type MyLocale =  'en-US' | 'de-DE';

type MyLocalizedEntry = LocalizedEntry<MyEntry, MyLocale>;

const myEntry: MyEntry = {
  sys: {
    id: "my-id"
  },
  fields: {
    a: "hello"
  }
}

const myLocalizedEntry: MyLocalizedEntry = {
  sys : {
    id: 'my-id'
  },
  fields: {
    a: {
      "de-DE": 'german translation',
      "en-US": 'english translation' 
    }
  }
}

In the example, Entry refers to the type definition from contentful.js. Please let me know if this works for you!

@ekasprzyk
Copy link
Author

Hello!

I really wanted to not forget to say, many thanks for this, and it works! I utterly didn't realise at the time that the management API adds in the locales and the delivery API doesn't (depite having worked with both, this utterly escaped me), and I managed to level up my keyof understanding so I actually understand what you're doing and how great and simple an idea it is.

For anyone else looking to work with the management API, this is a working solution.

Elizabeth

P.S. Closing the issue as fixed, but might be worth adding to any documentation you have, so other don't get tripped up!

P.P.S.happy to update the docs with a PR if you don't mind.

@marcolink
Copy link
Collaborator

@ekasprzyk Happy to hear that it helped!

A PR to add documentation for this would be very welcome 🥇

@marcolink
Copy link
Collaborator

@ekasprzyk
V2 will support this out of the box, just use the --localized. flag with the CLI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants