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 get_wp_lang_mo.sh, 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: get_wp_lang_mp.sh 
# Description: download and save WordPress .mo file automatically
# Author : Sylwester Wojnowski
# WWW : wojnowski.net.pl
# 
# USAGE : get_wp_lang_mp.sh -v WP_VERSION -l LANG_REGION [-d SAVETO]
#   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
 

WP_VERSION= 
LANG_REGION=
SAVETO=$(pwd)
ERRORS=(
 "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" 
)

help(){
  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"
}

bail(){
  printf "ERROR: ${ERRORS[$1]}";
  help
}

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

validate_input(){
  local OPTIND name
  while getopts "v:l:d:" name; do
    case "$name" in
      v)
        vreg='^[0-9]{1,}.[0-9]$';
        [[ $OPTARG =~ $vreg  ]] && {
          WP_VERSION="$OPTARG"
        } || { bail 1; exit 1; };; 
      l) 
        lreg='^[a-z]{2}(_[a-z]{2})?$';
        OPTARG=${OPTARG,,} 
        [[ $OPTARG =~ $lreg  ]] && {
          LANG_REGION="$OPTARG"
        } || { bail 2; exit 1; };;
      d)
        [[ -d "$OPTARG" ]] && {
           SAVETO="${OPTARG%/}"
        };;
      ?)
       help
       exit 1;
    esac
  done 

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

get(){
  FILENAME=
  OLR=
  ULR=${LANG_REGION/_/-}
  
  (( "${#LANG_REGION}" == 2 )) && {    
    OLR="${LANG_REGION}_${LANG_REGION^^}"
  }

  (( "${#LANG_REGION}" == 5 )) && {
    IFS=_ read -r -a pts <<< "${LANG_REGION}"    
    OLR="${pts[0]}_${pts[1]^^}";
  }
  
  FILENAME="$SAVETO/$OLR.mo"

  curl -G -d format=mo  https://translate.wordpress.org/projects/wp/"$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";}   
}

check_prereqs 
validate_input "$@"
get    

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:


get_wp_lang_mp.sh -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 en_AU.mo .

For majority of languages, like Polish language for example, that are used in just one region of the world, you need no region:


get_wp_lang_mp.sh -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 get_wp_lang_mp.sh 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