Commit ad4c3071 authored by Peter Serwylo's avatar Peter Serwylo

Attempt to automatically download relevant language flags as neccesary.

Also download high quality flags, then shrink them to 100x50px for space
reasons. Finally, the browser will shrink them again for visual reasons,
but at least we are shrinking a 100x50px image, rather than using
imagemagick to generate tiny low-quality images to start with.
parent 3aabf8ab
......@@ -178,6 +178,8 @@ body {
.lang-flag {
margin: 0 0.1em;
vertical-align: middle;
max-width: 2em;
max-height: 1em;
}
}
......
assets/lang/en.png

1.2 KB | W: | H:

assets/lang/en.png

1.95 KB | W: | H:

assets/lang/en.png
assets/lang/en.png
assets/lang/en.png
assets/lang/en.png
  • 2-up
  • Swipe
  • Onion skin
assets/lang/fr.png

325 Bytes | W: | H:

assets/lang/fr.png

384 Bytes | W: | H:

assets/lang/fr.png
assets/lang/fr.png
assets/lang/fr.png
assets/lang/fr.png
  • 2-up
  • Swipe
  • Onion skin
#!/usr/bin/env bash
# Fail on errors.
set -eu -o pipefail
DIR=`dirname $0`
JEKYLL_DIR=${DIR}/../
LANGS='"en"'
#
# For each directory `_data/[LANG]/ which contains a `strings.json` file, we take LANG and:
# * Add it to our config file so that Jekyll knows to generate a site for that language.
# * Attempt to download an icon for that language if it doesn't already exist.
#
# Note the icon downloading code may fail, because it assumes the country code is the same as the language
# code used by https://github.com/googlei18n/region-flags. For example, we special case English because there
# is not an "en" flag, only a "gb" flag. In the case of failure, it will just not render that flag when in that
# language. The user will still be able to see their current language and change languages via a drop down list.
#
function update_langs_in_config {
LANGS='"en"'
attempt_to_download_flag "gb" "en"
for I18N_DIR in `find ${JEKYLL_DIR}/_data/*/ -name 'strings.json' -printf '%h\n' | sort -u`; do
# Check if string contains $LANG already or not: http://stackoverflow.com/a/229606/2391921
LANG=`basename ${I18N_DIR}`
if [[ ${LANGS} != *"$LANG"* ]]; then
attempt_to_download_flag ${LANG} ${LANG}
LANGS="$LANGS, \"$LANG\""
fi
done
echo "Updating languages to: ${LANGS}"
sed -i "s/^languages: \[.*\]/languages: [ ${LANGS} ]/" ${JEKYLL_DIR}/_config.yml
}
for I18N_DIR in `find ${JEKYLL_DIR}/_data/*/ -name 'strings.json' -printf '%h\n' | sort -u`; do
function attempt_to_download_flag {
COUNTRY=$1
LANG=$2
COUNTRY_UPPER=`echo ${COUNTRY} | tr '[:lower:]' '[:upper:]'`
FLAG_DIR=${JEKYLL_DIR}/assets/lang
FLAG_FILE=${FLAG_DIR}/${LANG}.png
URL="https://raw.githubusercontent.com/googlei18n/region-flags/gh-pages/png/$COUNTRY_UPPER.png"
LANG=`basename ${I18N_DIR}`
if [ ! -f ${FLAG_FILE} ]; then
echo "Trying to download flag for ${LANG} from ${URL}"
mkdir -p ${FLAG_DIR}
curl ${URL} | convert -resize 100x50 - ${FLAG_FILE}
if [ ! -f ${FLAG_FILE} ]; then
echo "Could not find flag for language ${LANG}."
fi
else
echo "Flag for ${LANG} already exists"
fi
# Check if string contains $LANG already or not: http://stackoverflow.com/a/229606/2391921
if [[ ${LANGS} != *"$LANG"* ]]; then
LANGS="$LANGS, \"$LANG\""
fi
done
}
echo "Updating languages to: ${LANGS}"
sed -i "s/^languages: \[.*\]/languages: [ ${LANGS} ]/" ${JEKYLL_DIR}/_config.yml
\ No newline at end of file
update_langs_in_config
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment