summaryrefslogtreecommitdiffstats
path: root/conversions/tde-l10n/tde-l10n_split_desktop
blob: aaba8f49f8da1f21f8cb09ccb876e9cf3a2f8bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh

# default keywords
KEYWORDS="Name GenericName Comment Keywords Description ExtraNames X-TDE-Submenu"

# parse arguments
while [ $# -gt 0 ]; do
  key="$1"
  case $key in
    -k*)
      if [ "${1#-k}" = "" ]; then
        KEYWORD="$2"
        shift
      else
        KEYWORD=${1#-k}
      fi
      if [ "$KEYWORD" = "-" ]; then
        KEYWORDS=""
      else
        KEYWORDS="$KEYWORDS $KEYWORD"
      fi
      ;;

    -o*)
      if [ "${1#-o}" = "" ]; then
        POT="$2"
        shift
      else
        POT=${1#-o}
      fi
      ;;

    *)
      [ -f "$1" ] && \
      D="${D}|$1"
      ;;
  esac
  shift
done

oldIFS=$IFS
IFS="|"
set -- ${D#|}
IFS=$oldIFS

if [ -z "$1" ]; then
  echo "No desktop file specified for processing. Exiting..."
  exit 1
fi
if [ -z "$KEYWORDS" ]; then
  echo "No keywords specified to extract. Exiting..."
  exit 1
fi

# determine path for translations
TRANSLATIONS_DIR="translations/desktop_files"
if [ -n "$(git rev-parse --git-dir 2>/dev/null)" ]; then
  TRANSLATIONS_DIR="$(git rev-parse --show-toplevel)/${TRANSLATIONS_DIR}"
  APPNAME=$(basename $(git rev-parse --show-toplevel))
fi

# determine POT name
if [ -z "$POT" ]; then
  if [ $# -gt 1 ]; then
    POT="$APPNAME-desktops"
  else
    POT="$(basename "$1")/"
  fi
fi
if [ "${POT%/}" != "${POT}" ]; then
  TRANSLATIONS_DIR="$TRANSLATIONS_DIR/${POT%/}"
  POT=${POT%/}
fi
if [ "${POT%.pot}" = "${POT}" ]; then
  POT=${POT}.pot
fi
[ -d "$TRANSLATIONS_DIR" ] || \
mkdir -p "$TRANSLATIONS_DIR"

# prepare keywords to match
KEYWORDS_MATCH="$(echo "$KEYWORDS" | sed -e "s|^ *||" -e "s| *$||" -e "s# \+#\\\\|#g" )"

# prepare desktop files for xgettext
unset DO
unset DX
while [ $# -gt 0 ]; do
  D="$1"
  shift
  DO="$DO|$D.orig"
  DX="$DX|$D.tde_l10n"

  if [ ! -f "$D.orig" ]; then
    (
     printf "[<default>]\n"
     cat "$D"
    ) > "$D.orig"
    grep -v "^[a-zA-Z][^=]*\[[a-z][^=]*\]" "$D.orig" | \
    grep -Fxv "[<default>]" > "$D"
  fi

  # xgettext for desktop files generates a reference to the source file
  # one line below the actual position and does not have the option
  # to use the variable name as a comment in the POT file.
  # That's why we have our own mechanism for extracting strings.
  sed \
    -e "s|\"|\\\\\"|g" \
    < "$D" | \
  sed \
    -e "s#^\($KEYWORDS_MATCH\)[ ]*=[ ]*\(.*\)#/*\1*/i18n(\"\2\");#;t" \
    -e "s|.*||" \
    > "$D.tde_l10n"
done

# extract strings
#xgettext --foreign-user -L Desktop -k -k"Name" -k"GenericName" -k"Comment" -k"Keywords" -k"Description" -k"ExtraNames" -k"X-TDE-Submenu" -o - "$D" | \
printf "%s" "${DX#|}" | tr "|" "\0" | xargs -r0 \
xgettext --foreign-user --from-code=UTF-8 -Cc -ki18n -o - | \
sed "s|\.tde_l10n||g" | \
sed "s|Content-Type: text/plain; charset=CHARSET|Content-Type: text/plain; charset=UTF-8|"  \
> "$TRANSLATIONS_DIR/$POT"
POT_HEADER=$(sed -n "1,/^$/p" "$TRANSLATIONS_DIR/$POT")

# remove temporary files
printf "%s" "${DX#|}" | tr "|" "\0" | xargs -r0 \
rm

# process languages
printf "%s" "${DO#|}" | tr "|" "\0" | xargs -r0 cat |
sed -n "s|.*\[\([^]]*\)\][ ]*=.*|\1|p" | \
grep -vx "xx" | \
sort -u | \
while read L; do
  printf "%s\n" "$POT_HEADER" | sed "s|\(Language: \)|\1$L|" > "$TRANSLATIONS_DIR/$L.po"

  # process sections
  printf "%s\n" "${DO#|}" | tr "|" "\n" |
  while read D; do
    sed -n "s|^\[\(.*\)\]$|\1|p" "$D" | \
    sed -e 's|/|\\\\/|g' | \
    while read S; do
      # process variables
      sed -n "/^\[$S\]/,/^\[/s|^\([^#\[][^\[]*\)\[$L\][ ]*=.*|\1|p" "$D" | \
      while read V; do
        MSGID=$(sed -n -e "s|\"|\\\\\"|g" \
                    -e "/^\[$S\]/,/^\[/s|^$V[ ]*=[ ]*\(.*\)$|msgid \"\1\"|p" "$D")
        if [ -n "$MSGID" ] && [ -z "$(grep -Fx "$MSGID" "$TRANSLATIONS_DIR/$L.po")" ]; then
          printf "%s\n" "$MSGID"
          sed -n -e "s|\"|\\\\\"|g" -e "/^\[$S\]/,/^\[/s|^$V\[$L\][ ]*=[ ]*\(.*\)$|msgstr \"\1\"|p" "$D" | head -n1
          printf "\n"
        fi
      done >> "$TRANSLATIONS_DIR/$L.po"
    done
  done

  # update according to template
  printf "%s" "-- $L "
  msgmerge --update --backup=none "$TRANSLATIONS_DIR/$L.po" "$TRANSLATIONS_DIR/$POT"
done

# The LINGUAS file will be generated during build.
#ls "$TRANSLATIONS_DIR" | LANG=C sort | sed -n "s|\.po$||p" > "$TRANSLATIONS_DIR/LINGUAS"