Install and update translations in WordPress with Composer

Using Composer is a great way of organizing your WordPress project, with WordPress itself, plugins and themes declared as dependencies. However, an issue I’ve often seen is how you install translation files and keep them updated.

Koodimoonni’s repository

There is a very cool project by Koodimonni, who have created a Composer repository of some of the translation packs on wordpress.org. Unfortunately this repository is limited to just 4 plugins, and the twenty* themes. This is close, but not enough. I could have forked that project and included more plugins and themes myself, but if people started using my repository, I would felt responsible for keeping it running too.

Angry Creative’s autodownloader

At the contributor day of WordCamp Stockholm 2017, the Swedish agency Angry Creative released a Composer package – Composer Auto Language Updates – that connects to the API at wordpress.org to download the translations for you. That is a great idea, and works with every plugin and theme hosted on wordpress.org!

Angry Creative’s package however has its quirks and in January I started fixing bits of it, and was 95% ready to make a pull request. However, life happened, and I forgot about it.

I forgot about Angry Creative’s package, until yesterday afternoon, when I randomly picked it up again. I still wasn’t happy with it – even with my contributions from January, so I started rewriting the whole thing as a Composer plugin. With a few hours today as well, I’ve managed to finish the rewrite, package it and release it so others can benefit from it.

Meet the Composer plugin wplang

Today I released the Composer package bjornjohansen/wplang. It is available on GitHub if you want to look at it, fork it or contribute to it. There are bits of Angry Creative’s original code and logic in there, so I kept the license as GPLv2+. It is also released on Packagist, so to install it you can just run a Composer command:

composer require bjornjohansen/wplang

To define which languages you want to download, and which directory to put your language files in, you have to add a small section in your composer.jsonfile:

"extra": {
    "wordpress-languages": [ "en_GB", "nb_NO", "sv_SE" ],
    "wordpress-language-dir": "wp-content/languages"
}

You should propbably want to customize these values to suit your needs.

Finally run:

composer update

Now Composer will try to pull down translations for WordPress, and your plugins and themes from wordpress.org every time you install or update a package.

Drawbacks

This is one of the original drawbacks that I want to fix, which should be possible with the current structures: Translations are only downloaded when a package (i.e. WordPress, plugin or theme) is installed or updated. This means that if a translation package for a version you already have installed is updated, the translation won’t be updated until a new version of the plugin/theme is released.

Maybe you can help me fix it?

There are 4 comments

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.