Skip to content

Docker friendly scripts: init_lang_tpl.sh

Ryan Fischbach edited this page Oct 23, 2017 · 2 revisions
#!/bin/bash

function lowercase
{
	word=$1
	for((i=0;i<${#word};i++))
	do
		ch="${word:$i:1}"
		lc "$ch"
	done
	echo $word
}

function uppercase
{
	word=$1
	for((i=0;i<${#word};i++))
	do
		ch="${word:$i:1}"
		uc "$ch"
	done
	echo $word
}

function processCLIArgs
{
    while [[ $# -gt 0 ]] ; do
        case $1 in
            --lang-code|--lang)
                export DEFAULT_LANGUAGE_CODE=$(lowercase $2)
                shift # extra beyond the "shift" at the end of the block
                ;;
            --lang-region|--region)
                export DEFAULT_REGION_CODE=$(uppercase $2)
                shift # extra beyond the "shift" at the end of the block
                ;;
            *)
                echo -e "\033[0;93mIgnoring unrecognized option [$1].\033[0m"
                ;;
        esac
        shift # to advance the loop
    done
}

###############################################################################
# MAIN SCRIPT BODY

export DEFAULT_LANGUAGE_CODE=$( lowercase ${SITE_LANGUAGE_CODE} )
export DEFAULT_REGION_CODE=$( uppercase ${SITE_REGION_CODE} )
processCLIArgs $@

if [[ -z ${DEFAULT_LANGUAGE_CODE} ]] ; then
  # if not specified, default language is English
  DEFAULT_LANGUAGE_CODE="en"
fi
if [[ -z ${DEFAULT_REGION_CODE} ]] ; then
  # if not specified, default region is United States
  DEFAULT_REGION_CODE="US"
fi

TPL_SRC_FILE="${SITE_PATH}/res/templates/I18N.tpl"
PHP_DST_FILE="${SITE_PATH}/configs/anyhost/I18N.php"

if [ ! -f ${PHP_DST_FILE} ] ; then
  SED_PATTERN="s/%default_lang%/${DEFAULT_LANGUAGE_CODE}/"
  SED_PATTERN+=";s/%default_region%/${DEFAULT_REGION_CODE}/"
  sed ${SED_PATTERN} ${TPL_SRC_FILE} > ${PHP_DST_FILE}
fi

Clone this wiki locally