Installing WordPress language files automatically

Installing WordPress language files automatically

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

A script for installing WordPress language files automatically.

Automated WordPress language files installation


#! /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    

Run it


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.

This post was updated on 06 Oct 2021 21:46:59

Tags:  BASH ,  wordpress 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

Copyrights

©Copyright, 2024 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.

Citation

Cite this article as:

Wojnowski, Sylwester. "Installing WordPress language files automatically." From sWWW - Code For The Web . https://swww.com.pl//main/index/installing-wordpress-language-files-automatically

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!