]> git.plutz.net Git - shellwiki/commitdiff
URL rewriting for translation pages
authorPaul Hänsch <paul@plutz.net>
Sat, 20 May 2023 22:15:50 +0000 (00:15 +0200)
committerPaul Hänsch <paul@plutz.net>
Sat, 20 May 2023 22:15:50 +0000 (00:15 +0200)
handlers/10_translations.sh [new file with mode: 0755]

diff --git a/handlers/10_translations.sh b/handlers/10_translations.sh
new file mode 100755 (executable)
index 0000000..e666408
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+export LANGUAGE_DEFAULT="${LANGUAGE_DEFAULT:-en}"
+export HTTP_REFERER="${HTTP_REFERER:-$(HEADER Referer)}"
+
+case ${HTTP_REFERER} in
+  */:*/*):;;
+  */:*)
+   LANGUAGE_REFERRED="${HTTP_REFERER##*/:}"
+   ;;
+esac
+
+export LANGUAGE="${LANGUAGE_REFERRED:-${LANGUAGE_DEFAULT}}"
+
+case ${PATH_INFO} in
+  */:?*/\[attachment\]/?*)
+    export LANGUAGE="${PATH_INFO#*/:}"
+           LANGUAGE="${LANGUAGE%%/*}"
+    PATH_INFO="${PATH_INFO%%:?*/*}${PATH_INFO#*/:?*/}"
+    ;;
+  */:?*/\[attachment\])
+    export LANGUAGE="${PATH_INFO#*/:}"
+           LANGUAGE="${LANGUAGE%%/*}"
+    PATH_INFO="${PATH_INFO%:?*/\[attachment\]}[attachment]"
+    ;;
+  */:*/?*)  # Never occurs
+    :;;
+  */:?*/)   # Faulty URL build
+    REDIRECT "${_BASE}${PATH_INFO%/}"
+    ;;
+# */:"${LANGUAGE_DEFAULT}")
+#   REDIRECT "${_BASE}${PATH_INFO%:*}"
+#   ;;
+  */:?*)
+    export LANGUAGE="${PATH_INFO##*/:}"
+    if mdfile "${PATH_INFO}/"; then
+      PATH_INFO="${PATH_INFO}/"
+    elif [ "$LANGUAGE" = "$LANGUAGE_DEFAULT" ]; then
+      PATH_INFO="${PATH_INFO%:*}"
+    else
+      export ERROR_MSG="TRANSLATION NOT FOUND"
+      PATH_INFO="${PATH_INFO%:*}"
+    fi
+    ;;
+  /|*/*/)
+    if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then
+      REDIRECT "${_BASE}${PATH_INFO}:${LANGUAGE_REFERRED}"
+    fi
+    ;;
+  */:*/\[*\])  # Considered valid in most cases
+    :;;
+  */\[*\])
+    if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then
+      REDIRECT "${_BASE}${PATH_INFO%\[*\]}:${LANGUAGE_REFERRED}/[${PATH_INFO##*/\[}"
+    fi
+    ;;
+esac
+
+return 1