You are not logged in.

#1 2018-12-16 13:06:54

ThanosApostolou
Member
From: Athens, Greece
Registered: 2017-03-26
Posts: 7

Simple script to install missing language packages

I created a simple script to install the missing language packages for the languages I use (el_GR and en_US) depending on what software is installed.

#!/bin/bash

###########################################
### Automatically install language packages
### for el_GR and en_US"
###########################################
to_install=""

# determine if package is installed
is_installed() {
	pacman -Qq $1 &> /dev/null
}

# add for installation
# if it's not already installed
add_to_install () {
	for pkg in $@; do
		if ! is_installed $pkg; then
			to_install+="$pkg "
		fi
	done
}

# Global language packages
if is_installed poppler; then 
	add_to_install "poppler-data"
fi
if is_installed qt5-base; then 
	add_to_install "qt5-translations"
fi

# Specific language packages
if is_installed aspell; then 
	add_to_install "aspell-el" \
                                "aspell-en"
fi
if is_installed hunspell; then 
	add_to_install "hunspell-el" \
                                "hunspell-en_US"
fi
if is_installed firefox; then 
	add_to_install "firefox-i18n-el" \
                                "firefox-i18n-en-us"
fi
if is_installed firefox-developer-edition; then 
	add_to_install "firefox-developer-edition-i18n-el" \
                                "firefox-developer-edition-i18n-en-us"
fi
if is_installed thunderbird; then 
	add_to_install "thunderbird-i18n-el" \
                                "thunderbird-i18n-en-us"
fi
if is_installed gimp; then 
	add_to_install "gimp-help-el" \
                                "gimp-help-en "
fi
if is_installed vim; then 
	add_to_install "vim-spell-el" \
                                "vim-spell-en"
fi
if is_installed libreoffice-fresh; then 
	add_to_install "libreoffice-fresh-el"
fi
if is_installed libreoffice-still; then 
	add_to_install "libreoffice-still-el"
fi
if is_installed tesseract; then 
	add_to_install "tesseract-data-ell" \
                                "tesseract-data-eng"
fi
if is_installed eric; then 
	add_to_install "eric-i18n-en"
fi

# install packages
pacman -S --needed $to_install

I would like to make it more automated by reading the languages from a list, or even better from /etc/locale.gen file. However, this is very difficult because for example for en_US, various packages use all different suffixes like en_US, en-us, en, eng and it varies from language to language. If you have any ideas for improving please tell me.

Last edited by ThanosApostolou (2018-12-16 17:18:28)

Offline

#2 2018-12-16 13:47:30

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Simple script to install missing language packages

ThanosApostolou wrote:

If you have any ideas for improving please tell me.

Only regarding scripting style. (And IMHO simple scripts like these are best posted in this thread.)

  • Your shebang has /bin/sh, but you're using the function keyword, which would require a "fancier" shell like bash.

  • Parentheses around if-conditions are not required; this actually creates an unnecessary subshell every time.

  • is_installed() looks like "if pacman returns true: return true; else return false". No need to intercept the return value if you don't change it:

    is_installed() {
        pacman -Qi $1 &> /dev/null
    }
  • Since you're not interested in pacman's output, including the -i flag is pointless. You can use -q instead.

Edit: Something else to consider: every if statement maps one package name to a list of one or more package names. If you do switch to bash(/zsh/...), you could represent this mapping as an associative array and simplify the whole thing:

#!/bin/bash

declare -A language_pkgs
language_pkgs=(
    [poppler]="poppler-data"
    [aspell]="aspell-en aspell-el"
    # more entries...
)

to_install=
for pkg in $(pacman -Qq ${!language_pkgs[@]}); do
    to_install+=" ${language_pkgs[$pkg]}"
done

pacman -S --needed $to_install

This calls `pacman -Qq` only once to filter the list of array keys and then appends the corresponding values to $to_install. Like your original script, this does not properly quote every parameter expansion. That shouldn't be a problem for package names. If, for example, diffent versions of poppler should map to different lists of language packages, this needs more work, but I may have already taken this too far wink

Last edited by Raynman (2018-12-16 15:14:32)

Offline

#3 2018-12-16 15:26:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Simple script to install missing language packages

The add_to_install function is pointless.  Just add everything you want installed to "to_install" and be done with it.  You already use the --needed flag for pacman, so the add_to_install function does nothing of value (except slow down the script).

In fact, this whole thing can be greatly simplified:

#!/bin/sh

pkg_list() {
cat <<EOF
poppler poppler-data
qt5-base qt5-translations
aspell aspell-el
aspell aspell-en
hunspell hunspell-el
hunspell hunspell-en_US
firefox firefox-i18n-el
firefox firefox-i18n-us
firefox-developer-edition firefox-developer-edition-i18n-el
firefox-developer-edition firefox-developer-edition-i18n-en-us
thunderbird thunderbird-i18n-el
thunderbird thunderbird-i18n-en-us
gimp gimp-help-el
gimp gimp-help-en
vim vim-spell-el
vim vim-spell-en
libreoffice-fresh libreoffice-fresh-el
libreoffice-still libreoffice-still-el
tesseract tesseract-data-ell
tesseract tesseract-data-eng
eric eric-i18n-en
EOF
}

pkg_list | while read base lang; do
	pacman -Q $base >/dev/null 2>&1 && echo $lang
done | sudo pacman -S --needed -

Or another option, particularly good in my case where sed is a shell builtin:

#!/bin/sh

#LANG poppler poppler-data
#LANG qt5-base qt5-translations
#LANG aspell aspell-el aspell-en
#LANG hunspell hunspell-el hunspell-en_US
#LANG firefox firefox-i18n-el firefox-i18n-us
#LANG firefox-developer-edition firefox-developer-edition-i18n-el firefox-developer-edition-i18n-en-us
#LANG thunderbird thunderbird-i18n-el thunderbird-i18n-en-us
#LANG gimp gimp-help-el gimp-help-en
#LANG vim vim-spell-el vim-spell-en
#LANG libreoffice-fresh libreoffice-fresh-el
#LANG libreoffice-still libreoffice-still-el
#LANG tesseract tesseract-data-ell tesseract-data-eng
#LANG eric eric-i18n-en

sed -n 's/^#LANG \([^ ]*\).*/\1/p' "$0" | \
pacman -Qq - 2>/dev/null | while read pkg; do
	sed -n "/^#LANG $pkg /{s/#LANG $pkg //;s/ /\\n/;p;}" "$0"
done | sudo pacman -S --needed -

Last edited by Trilby (2018-12-16 15:41:05)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2018-12-16 17:32:44

ThanosApostolou
Member
From: Athens, Greece
Registered: 2017-03-26
Posts: 7

Re: Simple script to install missing language packages

Wow man thanks! I kept your 1st solution with the pkg_list() function, which I can understand better (sed commands always give me headaches). You have good scripting skills wink

Offline

Board footer

Powered by FluxBB