Making plugin translations a cinch

Verbb Team Verbb Team June 2024 2 min read

If you're a plugin developer like us, you might have come across the need to translate client-facing text in your plugin. You'll have used plenty of {{ 'Check out this text' | t('my-plugin') }} or Craft::t('my-plugin', 'Check out this text'); in your plugins which is best practice when dealing with text. This allows the text you write to be translatable under the 'my-plugin' category, allowing users of your plugin to supply their own translations.

Depending on your native language, the first step to providing those translations is gathering them in a single file. For us, it's English, and we end up with a file like this one (opens new window) for Hyper. This lists all the available strings to be translated.

So depending on how many translatable strings your plugin uses, this might be a quick job, or it could take hours of scouring your many plugin files and copy-pasting. For all our plugins (50+!) we wanted a way to automate this process.

Get to translating#

Enter plugin-translator (opens new window) — a module for scraping all the translatable strings in a given plugin, and generating the base translation file. For any plugin, you'll get a collection of strings in a single file:

<?php

return [
  'Add a link' => 'Add a link',
  'Additional CSS classes for the link.' => 'Additional CSS classes for the link.',
  'Additional HTML attributes for the link.' => 'Additional HTML attributes for the link.',
  'Add {type}' => 'Add {type}',
  ...

The module will extract translation strings from PHP, HTML, Twig, JS and Vue files, and creates a translations/en/plugin-handle.php file based on those strings right in your plugin files.

If you want to give it a go, install the module and run:

./craft plugin-translator/translate plugin-handle

Languages, languages#

Taking it one step further with AI (because what doesn't use AI these days), we thought — what if we could automatically translate our plugins into as many additional languages as we wanted to?

The module will automatically create extra language files in 20 languages using Deepl (opens new window). Of course, you can change that (opens new window). This makes translating our plugins into multiple languages a breeze and free!

Being an automated process, there's sure to be some errors along the way, so it'll always help to have a native speaker review what the AI comes up with. But from our testing and feedback, it'll get you 99% of the way there.

Translate away#

We hope you find this little utility useful! Check it out on GitHub (opens new window).