From 07889f19b33d4dbce8d6ba61d991346db972d4d9 Mon Sep 17 00:00:00 2001 From: southerntofu Date: Wed, 5 Jan 2022 17:07:01 +0100 Subject: [PATCH] Load translations properly when forgebuild is not setup, just cloned --- forgebuild.sh | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/forgebuild.sh b/forgebuild.sh index b1c328c..3ba95e4 100755 --- a/forgebuild.sh +++ b/forgebuild.sh @@ -32,15 +32,44 @@ if [[ "$LANG" != "NONE" ]]; then # Extract two letters from $LANG locale="${LANG:0:2}" - # Ensure translations have been setup - if [ -d /usr/share/forgebuild/i18n ]; then - I18N_DIR=/usr/share/forgebuild/i18n - elif [ -d $HOME/.local/share/forgebuild/i18n ]; then - I18N_DIR=$HOME/.local/share/forgebuild/i18n + # Figure out where to load translations from. If the person is hacking on a new version and/or forgebuild + # hasn't been setup, it's very likely that the build spec repo can be found at either ../build or ./spec + # relative to forgebuild.sh. If none of these exist, try to load translations from a normal setup: + # from ~/.local/forgebuild/i18n or /usr/share/forgebuild/i18n + BINDIR="$(dirname "$0")" + # ../build has higher precedence in case you're hacking on several implementations + if [ -d "$BINDIR"/../build/i18n ]; then + I18N_DIR="$BINDIR"/../build/i18n + elif [ -d "$BINDIR"/spec/i18n ]; then + # We are in a cloned repo and submodule has been initialized + I18N_DIR="$BINDIR"/spec/i18n else - echo "ERROR: could not find translations. Maybe you need to run the setup.sh script?" - exit 1 + # Either submodule hasn't been initalized, or we are not in a repo at all + # First try normal install paths, then try to clone spec submodule + # In this order because we'd like to rely on Internet access as last resort only + if [ -d /usr/share/forgebuild/i18n ]; then + I18N_DIR=/usr/share/forgebuild/i18n + elif [ -d $HOME/.local/share/forgebuild/i18n ]; then + I18N_DIR=$HOME/.local/share/forgebuild/i18n + elif [ -d "$BINDIR"/spec ]; then + # Submodule isn't initialized + PREVDIR="$(pwd)" + cd "$BINDIR" + git submodule init + if ! git submodule update; then + echo "ERROR: Failed to find local translations and to download them from the Internet" + exit 1 + fi + cd "$PREVDIR" + I18N_DIR="$BINDIR"/spec/i18n + else + # We are not in a repo and local translations were not found, abort! + echo "ERROR: could not find translations. Maybe you need to run the setup.sh script?" + exit 1 + fi fi + + # TODO: add debug message about where translations are loaded from # Initialize translations [ -f $I18N_DIR/$locale.json ] && locale_strings="$(cat $I18N_DIR/$locale.json)" || locale_strings="$(cat $I18N_DIR/en.json)"