mattias-persson / statamic-translation-manager

A Statamic V2 addon for importing and exporting translations
MIT License
5 stars 2 forks source link

Add item slug to data if available #12

Closed aryehraber closed 4 years ago

aryehraber commented 4 years ago

This PR adds support for translating slugs!

Basically, during field preparation, slug gets pushed to the data array, if it's available. Directly after this, data is looped over to prepare all the fields and check if they should be translated; here the slug will get filtered out again if the user didn't add localizable: true for the slug attribute in their fieldset:

subtitle:
  type: text
  localizable: true

I initially wanted to use method_exists to check if ->slug() was available, but it kept returning false, I think this is due to Statamic using magic methods internally so the method may not "officially" exist on the class. As a way around this I simply suppress errors using @ since we don't really need to act if an error is thrown here, we just want to skip the slug assignment in that case. If you have a better/cleaner approach, please let me know!