]> git.plutz.net Git - shellwiki/blob - handlers/10_translations.sh
use global _DATE variable
[shellwiki] / handlers / 10_translations.sh
1 #!/bin/sh
2
3 # Copyright 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 _(){ printf %s\\n "$*"; }
18
19 # Set LANGUAGE_DEFAULT to enable Plugin
20 [ ! "$LANGUAGE_DEFAULT" ] && return 1
21
22 export LANGUAGE_DEFAULT="${LANGUAGE_DEFAULT:-en}"
23 export HTTP_REFERER="${HTTP_REFERER:-$(HEADER Referer)}"
24 export LANGUAGE ERROR_MSG
25
26 case ${HTTP_REFERER} in
27   */:*/*):;;
28   */:*)
29    LANGUAGE_REFERRED="${HTTP_REFERER##*/:}"
30    ;;
31 esac
32
33 LANGUAGE="${LANGUAGE_REFERRED:-${LANGUAGE_DEFAULT}}"
34
35 case ${PATH_INFO} in
36   */:?*/\[attachment\]/?*)
37     LANGUAGE="${PATH_INFO#*/:}"
38     LANGUAGE="${LANGUAGE%%/*}"
39     PATH_INFO="${PATH_INFO%%:?*/*}${PATH_INFO#*/:?*/}"
40     ;;
41   */:?*/\[attachment\])
42     LANGUAGE="${PATH_INFO#*/:}"
43     LANGUAGE="${LANGUAGE%%/*}"
44     PATH_INFO="${PATH_INFO%:?*/\[attachment\]}[attachment]"
45     ;;
46   */:?*/\[*\])
47     LANGUAGE="${PATH_INFO#*/:}"
48     LANGUAGE="${LANGUAGE%%/*}"
49     ;;
50   */:?*/:?*)
51     # Accidental double language link, last one stays valid!
52     REDIRECT "${_BASE}${PATH_INFO%/:?*/:?*}/:${PATH_INFO##*/:}"
53     ;;
54   */:?*/?*)
55     :;;  # Default attachment handler
56   */:?*/)   # Faulty URL build
57     REDIRECT "${_BASE}${PATH_INFO%/}"
58     ;;
59 # */:"${LANGUAGE_DEFAULT}")
60 #   REDIRECT "${_BASE}${PATH_INFO%:*}"
61 #   ;;
62   */:?*)
63     LANGUAGE="${PATH_INFO##*/:}"
64     PATH_INFO="${PATH_INFO%:*}"
65
66     [ "$LANGUAGE" != "$LANGUAGE_DEFAULT" ] \
67     && case "$(mdfile "${PATH_INFO}")" in
68       *"/:$LANGUAGE/#page.md")
69         :;;
70       '')
71         :;;
72       *)ERROR_MSG="TRANSLATION NOT FOUND"
73         ;;
74     esac
75     ;;
76   /|*/*/)  # Keep Language from Referer
77     if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then
78       REDIRECT "${_BASE}${PATH_INFO}:${LANGUAGE_REFERRED}"
79     fi
80     ;;
81   */\[*\])  # Keep Language from Referer
82     if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then
83       REDIRECT "${_BASE}${PATH_INFO%\[*\]}:${LANGUAGE_REFERRED}/[${PATH_INFO##*/\[}"
84     fi
85     ;;
86 esac
87
88 [ -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
89
90 return 1