Installing WordPress language files automatically

Installing WordPress language files automatically

Created:20 May 2017 22:03:03 , in  Web development

Installing language file(s) is one of possibly hundreds small tasks one has to plod through while building a multilingual website or blog with WordPress. Mostly due to being documented well, the task is not difficult to carry out. Nonetheless, it diverts attention from other aspects of website development and consumes too much precious time. Eventually one ends up doing the whole thing manually, possibly repeatedly. Clearly , the task is a perfect candidate for automation.

Automated WordPress language files installation

Below is a script, I called it, for installing .mo files in WordPress automatically.

It is written in BASH and accepts three arguments, which are:

  • WordPress version to download a language file for
  • language (or language with region ),
  • and finally directory, the language file to save to.

The script uses Curl for downloads internally.It will tell you if it finds the tool is not installed on your box.

#! /usr/bin/env bash

# Script: 
# Description: download and save WordPress .mo file automatically
# Author : Sylwester Wojnowski
# WWW :
#   WP_VERSION is two digits separated with full stop: 2.3, 4.0, 4.7 etc.
#   LANG_REGION is language code: pl, gr or language_region: en_BR, pt_BR
#   SAVETO is existing, writeable by the script directory on the system

 "curl tool not found. This program relies on curl for uploads. Install it and try again.\n"
 "Incorrect WordPress version specified."
 "Incorrect language or language_region specified."
 "Incomplete input specified."
 "Language file download failed.\nIncorrect language and region specified.\n" 

  scriptname=$(basename "$0")
  printf "\n$scriptname - download and save WordPress .mo file automatically\n"
  printf "  Synopsis: $scriptname -v WP_VERSION -l LANG_REGION [-d SAVETO]\n"
  printf "  Example: Download .mo file for WordPress 4.7.x and Polish language\n"
  printf "    $scriptname -v 4.7 -l pl [-d SAVETO]\n"
  printf "  Example: Download .mo file for WordPress 4.7.x and Canadian English language\n"
  printf "    $scriptname -v 4.7 -l en_CA [-d SAVETO]\n\n"

  printf "ERROR: ${ERRORS[$1]}";

  [[ -z $(which curl) ]] && {
    bail 0
    exit 1 

  local OPTIND name
  while getopts "v:l:d:" name; do
    case "$name" in
        [[ $OPTARG =~ $vreg  ]] && {
        } || { bail 1; exit 1; };; 
        [[ $OPTARG =~ $lreg  ]] && {
        } || { bail 2; exit 1; };;
        [[ -d "$OPTARG" ]] && {
       exit 1;

  [[ -z "$WP_VERSION" ]] || [[ -z "$LANG_REGION" ]] && { bail 3; exit 1; }

  (( "${#LANG_REGION}" == 2 )) && {    

  (( "${#LANG_REGION}" == 5 )) && {
    IFS=_ read -r -a pts <<< "${LANG_REGION}"    

  curl -G -d format=mo"$WP_VERSION".x/"${ULR}"/default/export-translations > "$FILENAME"

  [[ $(file -b --mime-type "$FILENAME" ) != 'application/octet-stream'  ]] && {
    [[ -f "$FILENAME" ]] { rm "$FILENAME"; }
    bail 4
    exit 1      
  } || { printf "Language file saved to $FILENAME.\n";}   

validate_input "$@"

Usage examples

The script has some usage examples included in the head section. They cover to large degree what it is made for and what it can do. As mentioned in the section, the script provides no default values for WordPress version and language. So these two options are absolutely required.

As for directory option, the script saves .mo files to ,if it is not specified the files will be saved in the current working directory (where the script is run from). For either case make sure the directory is writeable for the script. This check is skipped in the script.

Assuming that the script has been saved to a directory in PATH and made executable,it can be invoked as follows: -l en_AU -v 4.7 -d /var/www/wordpress/wp-content  

Following code will download language file for English language and Australia and save it in WordPress installation languages directory placed in /var/www/wordpress/wp-content/languages Output language file will be called .

For majority of languages, like Polish language for example, that are used in just one region of the world, you need no region: -l pl -v 4.7 -d /var/www/wordpress/wp-content

For the unfocused, option -l received pl instead of pl_PL as its argument in the example above.

That's pretty much all what's necessary to make perform its job effectively.

After that you can navigate to General Settings in WordPress admin panel and choose the language.

Final thoughts

Building a website for WordPress ( especially a large and multilingual one ) is quite a challenge. The script above makes the challenge a wee bit smaller. I hope it will prove useful.

As always comments and shares are welcome and appreciated.

This post was updated on 25 May 2017 16:21:23

Tags:  BASH ,  wordpress 

Author, Copyright and citation


Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.


©Copyright, 2021 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.


Cite this article as:

Wojnowski, Sylwester. "Installing WordPress language files automatically." From sWWW - Code For The Web .

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!