fixdesk 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler, Feb 2012 bkhome.org
  3. #licence GPL3 (refer /usr/share/doc/legal)
  4. #this script has been created to translate English <-> Non-English certain files that are related to the desktop in some way.
  5. #the files to be translated, and the translations, are defined in /usr/share/sss/desk_strings/
  6. #note, the translations in /usr/share/sss are managed by MoManager (see Utility menu).
  7. #fixdesk has to be called whenever the locale is changed.
  8. #note, the SSS domain 'menu_strings' (/usr/share/sss/menu_strings) is read by /usr/sbin/fixmenus.
  9. #120209 first release.
  10. #120209 this script now called from /usr/sbin/quicksetup, whenever locale is changed.
  11. #120209 convert this script into generic handler of sss-domains, allow symlinks (ex: fixscripts now a symlink to fixdesk).
  12. #120211 major overhaul to ensure integrity of translation of target files back to en.
  13. #120212 bugfix.
  14. #120216 major rewrite, sss translation file now uses simple sed expressions.
  15. #121124 ensure that all [ ] are escaped. see also /usr/sbin/fixmenus, lang2initrd.
  16. #121125 revert 121124.
  17. #121126 restore 121124, plus escape '.' chars in regex.
  18. #Note to developers:
  19. #you can run "fixdesk en" or "fixdesk de" in a terminal to convert the target files to en or de
  20. #(or other language). this is convenient for testing your translations.
  21. SSSDOMAIN="desk_strings" #this was it prior to 120209.
  22. SSSSCRIPT=$(basename $0) #but now can have symlinks to fixdesk. ex: fixscripts
  23. case $SSSSCRIPT in
  24. fixdesk) SSSDOMAIN="desk_strings" ;;
  25. fixscripts) SSSDOMAIN="script_strings" ;;
  26. *) SSSDOMAIN="$SSSSCRIPT" ;; #for the future.
  27. esac
  28. SSSFILE="${SSSDOMAIN}" #filenames are same as the domain ex: /usr/share/sss/script_strings/script_strings.de
  29. mkdir -p /tmp/momanager
  30. mkdir -p /var/local/sss
  31. . /etc/rc.d/PUPSTATE
  32. if [ "$LANG" = "C" ];then #in case caller script did this.
  33. LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
  34. export LANG
  35. fi
  36. LANG1="`echo -n $LANG | cut -f 1 -d '_'`" #ex: de
  37. [ ! -f /usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN}.$LANG1 ] && LANG1='en' #sanity check. if translation sss-file not exist, change files back to english.
  38. #this script may be called from /usr/sbin/momanager, with passed param...
  39. [ $1 ] && LANG1="$1" #want to translate to this language.
  40. #keep a backup of original en target files...
  41. echo "Files being translated:"
  42. PATHPREFIX=""
  43. [ $PUPMODE -gt 3 ] && PATHPREFIX="/initrd/pup_ro2"
  44. for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
  45. do
  46. [ "$ASECTIONID" = "general" ] && continue
  47. xASECTIONID="$ASECTIONID"
  48. #allow any character to be the 'marker' for / in the section-id...
  49. MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
  50. TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
  51. echo " ${TARGETFILE}" #useful info when running this script in a terminal.
  52. TARGETPATH="`dirname "$TARGETFILE"`"
  53. if [ ! -f /var/local/sss"${TARGETFILE}" ];then
  54. mkdir -p /var/local/sss"${TARGETPATH}"
  55. cp -a -f "$TARGETFILE" /var/local/sss"${TARGETPATH}"/ #should only happen once.
  56. fi
  57. done
  58. #always restore the files to orig english version...
  59. for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
  60. do
  61. [ "$ASECTIONID" = "general" ] && continue
  62. #allow any character to be the 'marker' for / in the section-id...
  63. MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
  64. TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
  65. TARGETPATH="`dirname "$TARGETFILE"`"
  66. if [ -f /var/local/sss"${TARGETFILE}" ];then #precaution.
  67. cp -a -f /var/local/sss"${TARGETFILE}" "${TARGETPATH}"/ #restore en file.
  68. fi
  69. done
  70. [ "$LANG1" = "en" ] && exit #momanager may call with 'en' passed param.
  71. #now translate to $LANG1...
  72. for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
  73. do
  74. sPTN="/^\[${ASECTIONID}\]/,/^$/p" #this is a multi-line block find expression.
  75. CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
  76. [ ! "$CODEBLOCK" ] && continue #precaution.
  77. #allow any character to be the 'marker' for / in the section-id...
  78. MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
  79. TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
  80. [ ! -f "$TARGETFILE" ] && continue #precaution.
  81. #hack, really should not process /root/Choices/ROX-Filer/PuppyPin in sss-domain, only suited to static files...
  82. [ "$TARGETFILE" = "/root/Choices/ROX-Filer/PuppyPin" ] && echo -n "ICONWIPE" >/var/local/pup_event_icon_change_flag #see clean_desk_icons
  83. echo "$CODEBLOCK" > /tmp/fixdesk-translationblock
  84. #121124 ensure that all [ ] are escaped... 121125 revert... 121126 restore, plus escape '.' chars...
  85. sed -i -e 's%\[%\\[%g' -e 's$\]$\\]$g' -e 's%\\\\\[%\\[%g' -e 's%\\\\\]%\\]%g' /tmp/fixdesk-translationblock
  86. sed -i -e 's%\.%\\.%g' -e 's%\\\\\.%\\.%g' /tmp/fixdesk-translationblock #note: 2nd ptn gets rid of prior escape char, so there remains just one.
  87. sed -i -f /tmp/fixdesk-translationblock "${TARGETFILE}"
  88. done
  89. ###END###