chore: make the application more internationalized #39

Merged
xpamych merged 3 commits from Maks1mS/ALR:chore/i18n into master 2025-02-28 07:09:23 +00:00
11 changed files with 362 additions and 36 deletions

@ -66,6 +66,7 @@ i18n:
$(XGOTEXT_BIN) --output ./internal/translations/default.pot
msguniq --use-first -o ./internal/translations/default.pot ./internal/translations/default.pot
msgmerge --backup=off -U ./internal/translations/po/ru/default.po ./internal/translations/default.pot
bash scripts/i18n-badge.sh
test-coverage:
go test ./... -v -coverpkg=./... -coverprofile=coverage.out

@ -3,7 +3,7 @@
</p>
<b></b>
[![Go Report Card](https://goreportcard.com/badge/gitea.plemya-x.ru/Plemya-x/ALR)](https://goreportcard.com/report/gitea.plemya-x.ru/Plemya-x/ALR) ![Test coverage](./coverage-badge.svg)
[![Go Report Card](https://goreportcard.com/badge/gitea.plemya-x.ru/Plemya-x/ALR)](https://goreportcard.com/report/gitea.plemya-x.ru/Plemya-x/ALR) ![Test coverage](./assets/coverage-badge.svg) ![ru translate](./assets/i18n-ru-badge.svg)
# ALR (Any Linux Repository)

Before

Width:  |  Height:  |  Size: 926 B

After

Width:  |  Height:  |  Size: 926 B

18
assets/i18n-ru-badge.svg Normal file

@ -0,0 +1,18 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="129" height="20">
<linearGradient id="smooth" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/></linearGradient>
<mask id="round">
<rect width="129" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#round)">
<rect width="75" height="20" fill="#555"/>
<rect x="75" width="64" height="20" fill="#4c1"/>
<rect width="129" height="20" fill="url(#smooth)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="37" y="15" fill="#010101" fill-opacity=".3">ru translate</text>
<text x="37" y="14">ru translate</text>
<text x="100" y="15" fill="#010101" fill-opacity=".3">100.00%</text>
<text x="100" y="14">100.00%</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 942 B

@ -46,7 +46,7 @@ func InstallCmd() *cli.Command {
&cli.BoolFlag{
Name: "clean",
Aliases: []string{"c"},
Usage: "Build package from scratch even if there's an already built package available",
Usage: gotext.Get("Build package from scratch even if there's an already built package available"),
},
},
Action: func(c *cli.Context) error {

@ -0,0 +1,102 @@
// ALR - Any Linux Repository
// Copyright (C) 2025 Евгений Храмов
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cliutils
import (
"fmt"
"github.com/leonelquinteros/gotext"
)
// Templates are based on https://github.com/urfave/cli/blob/3b17080d70a630feadadd23dd036cad121dd9a50/template.go
//nolint:unused
var (
helpNameTemplate = `{{$v := offset .HelpName 6}}{{wrap .HelpName 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}}`
descriptionTemplate = `{{wrap .Description 3}}`
authorsTemplate = `{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
{{end}}{{$author}}{{end}}`
visibleCommandTemplate = `{{ $cv := offsetCommands .VisibleCommands 5}}{{range .VisibleCommands}}
{{$s := join .Names ", "}}{{$s}}{{ $sp := subtract $cv (offset $s 3) }}{{ indent $sp ""}}{{wrap .Usage $cv}}{{end}}`
visibleCommandCategoryTemplate = `{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{template "visibleCommandTemplate" .}}{{end}}{{end}}`
visibleFlagCategoryTemplate = `{{range .VisibleFlagCategories}}
{{if .Name}}{{.Name}}
{{end}}{{$flglen := len .Flags}}{{range $i, $e := .Flags}}{{if eq (subtract $flglen $i) 1}}{{$e}}
{{else}}{{$e}}
{{end}}{{end}}{{end}}`
visibleFlagTemplate = `{{range $i, $e := .VisibleFlags}}
{{wrap $e.String 6}}{{end}}`
copyrightTemplate = `{{wrap .Copyright 3}}`
)
func GetAppCliTemplate() string {
return fmt.Sprintf(`%s:
{{template "helpNameTemplate" .}}
%s:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[%s]{{end}}{{if .Commands}} %s [%s]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[%s...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
%s:
{{.Version}}{{end}}{{end}}{{if .Description}}
%s:
{{template "descriptionTemplate" .}}{{end}}
{{- if len .Authors}}
%s{{template "authorsTemplate" .}}{{end}}{{if .VisibleCommands}}
%s:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
%s:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
%s:{{template "visibleFlagTemplate" .}}{{end}}{{if .Copyright}}
%s:
{{template "copyrightTemplate" .}}{{end}}
`, gotext.Get("NAME"), gotext.Get("USAGE"), gotext.Get("global options"), gotext.Get("command"), gotext.Get("command options"), gotext.Get("arguments"), gotext.Get("VERSION"), gotext.Get("DESCRIPTION"), gotext.Get("AUTHOR"), gotext.Get("COMMANDS"), gotext.Get("GLOBAL OPTIONS"), gotext.Get("GLOBAL OPTIONS"), gotext.Get("COPYRIGHT"))
}
func GetCommandHelpTemplate() string {
return fmt.Sprintf(`%s:
{{template "helpNameTemplate" .}}
%s:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [%s]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[%s...]{{end}}{{end}}{{if .Category}}
%s:
{{.Category}}{{end}}{{if .Description}}
%s:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
%s:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
%s:{{template "visibleFlagTemplate" .}}{{end}}
`, gotext.Get("NAME"),
gotext.Get("USAGE"),
gotext.Get("command options"),
gotext.Get("arguments"),
gotext.Get("CATEGORY"),
gotext.Get("DESCRIPTION"),
gotext.Get("OPTIONS"),
gotext.Get("OPTIONS"),
)
}

@ -194,6 +194,62 @@ msgstr ""
msgid "Choose which optional package(s) to install"
msgstr ""
#: internal/cliutils/template.go:74 internal/cliutils/template.go:93
msgid "NAME"
msgstr ""
#: internal/cliutils/template.go:74 internal/cliutils/template.go:94
msgid "USAGE"
msgstr ""
#: internal/cliutils/template.go:74
msgid "global options"
msgstr ""
#: internal/cliutils/template.go:74
msgid "command"
msgstr ""
#: internal/cliutils/template.go:74 internal/cliutils/template.go:95
msgid "command options"
msgstr ""
#: internal/cliutils/template.go:74 internal/cliutils/template.go:96
msgid "arguments"
msgstr ""
#: internal/cliutils/template.go:74
msgid "VERSION"
msgstr ""
#: internal/cliutils/template.go:74 internal/cliutils/template.go:98
msgid "DESCRIPTION"
msgstr ""
#: internal/cliutils/template.go:74
msgid "AUTHOR"
msgstr ""
#: internal/cliutils/template.go:74
msgid "COMMANDS"
msgstr ""
#: internal/cliutils/template.go:74
msgid "GLOBAL OPTIONS"
msgstr ""
#: internal/cliutils/template.go:74
msgid "COPYRIGHT"
msgstr ""
#: internal/cliutils/template.go:97
msgid "CATEGORY"
msgstr ""
#: internal/cliutils/template.go:99 internal/cliutils/template.go:100
msgid "OPTIONS"
msgstr ""
#: internal/config/config.go:59
msgid "Error opening config file, using defaults"
msgstr ""
@ -275,25 +331,29 @@ msgstr ""
msgid "Error listing installed packages"
msgstr ""
#: main.go:44
#: main.go:45
msgid "Print the current ALR version and exit"
msgstr ""
#: main.go:60
#: main.go:61
msgid "Arguments to be passed on to the package manager"
msgstr ""
#: main.go:66
#: main.go:67
msgid "Enable interactive questions and prompts"
msgstr ""
#: main.go:91
#: main.go:92
msgid ""
"Running ALR as root is forbidden as it may cause catastrophic damage to your "
"system"
msgstr ""
#: main.go:123
#: main.go:125
msgid "Show help"
msgstr ""
#: main.go:129
msgid "Error while running app"
msgstr ""

@ -1,19 +1,19 @@
#
# Maxim Slipenko <maks1ms@alt-gnome.ru>, 2025.
# x1z53 <x1z53@yandex.ru>, 2025.
# Maxim Slipenko <maks1ms@alt-gnome.ru>, 2025.
#
msgid ""
msgstr ""
"Project-Id-Version: unnamed project\n"
"PO-Revision-Date: 2025-01-24 21:20+0300\n"
"Last-Translator: x1z53 <x1z53@yandex.ru>\n"
"PO-Revision-Date: 2025-02-27 14:27+0300\n"
"Last-Translator: Maxim Slipenko <maks1ms@alt-gnome.ru>\n"
"Language-Team: Russian\n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && "
"n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"X-Generator: Gtranslator 47.1\n"
#: build.go:44
@ -26,7 +26,7 @@ msgstr "Путь к скрипту сборки"
#: build.go:55
msgid "Specify package in script (for multi package script only)"
msgstr ""
msgstr "Укажите пакет в скрипте (только для многопакетного скрипта)"
#: build.go:60
msgid "Name of the package to build and its repo (example: default/go-bin)"
@ -43,7 +43,7 @@ msgstr "Ошибка инициализации базы данных"
#: build.go:105
msgid "Package not found"
msgstr ""
msgstr "Пакет не найден"
#: build.go:123
msgid "Error pulling repositories"
@ -54,7 +54,6 @@ msgid "Unable to detect a supported package manager on the system"
msgstr "Не удалось обнаружить поддерживаемый менеджер пакетов в системе"
#: build.go:137
#, fuzzy
msgid "Error parsing os release"
msgstr "Ошибка при разборе файла выпуска операционной системы"
@ -202,6 +201,62 @@ msgstr "Выберите, какой пакет использовать для
msgid "Choose which optional package(s) to install"
msgstr "Выберите, какой дополнительный пакет(ы) следует установить"
#: internal/cliutils/template.go:74 internal/cliutils/template.go:93
msgid "NAME"
msgstr "НАЗВАНИЕ"
#: internal/cliutils/template.go:74 internal/cliutils/template.go:94
msgid "USAGE"
msgstr "ИСПОЛЬЗОВАНИЕ"
#: internal/cliutils/template.go:74
msgid "global options"
msgstr "глобальные опции"
#: internal/cliutils/template.go:74
msgid "command"
msgstr "команда"
#: internal/cliutils/template.go:74 internal/cliutils/template.go:95
msgid "command options"
msgstr "опции команды"
#: internal/cliutils/template.go:74 internal/cliutils/template.go:96
msgid "arguments"
msgstr "аргументы"
#: internal/cliutils/template.go:74
msgid "VERSION"
msgstr "ВЕРСИЯ"
#: internal/cliutils/template.go:74 internal/cliutils/template.go:98
msgid "DESCRIPTION"
msgstr "ОПИСАНИЕ"
#: internal/cliutils/template.go:74
msgid "AUTHOR"
msgstr "АВТОР"
#: internal/cliutils/template.go:74
msgid "COMMANDS"
msgstr "КОМАНДЫ"
#: internal/cliutils/template.go:74
msgid "GLOBAL OPTIONS"
msgstr "ГЛОБАЛЬНЫЕ ОПЦИИ"
#: internal/cliutils/template.go:74
msgid "COPYRIGHT"
msgstr "АВТОРСКОЕ ПРАВО"
#: internal/cliutils/template.go:97
msgid "CATEGORY"
msgstr "КАТЕГОРИЯ"
#: internal/cliutils/template.go:99 internal/cliutils/template.go:100
msgid "OPTIONS"
msgstr "ПАРАМЕТРЫ"
#: internal/config/config.go:59
msgid "Error opening config file, using defaults"
msgstr ""
@ -270,11 +325,11 @@ msgstr "Скачивание источника"
#: internal/dl/progress_tui.go:100
msgid "%s: done!\n"
msgstr ""
msgstr "%s: выполнено!\n"
#: internal/dl/progress_tui.go:104
msgid "%s %s downloading at %s/s\n"
msgstr ""
msgstr "%s %s загружается — %s/с\n"
#: internal/logger/log.go:47
msgid "ERROR"
@ -288,19 +343,19 @@ msgstr "Список пакетов репозитория ALR"
msgid "Error listing installed packages"
msgstr "Ошибка при составлении списка установленных пакетов"
#: main.go:44
#: main.go:45
msgid "Print the current ALR version and exit"
msgstr "Показать текущую версию ALR и выйти"
#: main.go:60
#: main.go:61
msgid "Arguments to be passed on to the package manager"
msgstr "Аргументы, которые будут переданы менеджеру пакетов"
#: main.go:66
#: main.go:67
msgid "Enable interactive questions and prompts"
msgstr "Включение интерактивных вопросов и запросов"
#: main.go:91
#: main.go:92
msgid ""
"Running ALR as root is forbidden as it may cause catastrophic damage to your "
"system"
@ -308,7 +363,11 @@ msgstr ""
"Запуск ALR от имени root запрещён, так как это может привести к "
"катастрофическому повреждению вашей системы"
#: main.go:123
#: main.go:125
msgid "Show help"
msgstr "Показать справку"
#: main.go:129
msgid "Error while running app"
msgstr "Ошибка при запуске приложения"
@ -369,9 +428,8 @@ msgid "Executing build()"
msgstr "Исполнение build()"
#: pkg/build/build.go:689 pkg/build/build.go:709
#, fuzzy
msgid "Executing %s()"
msgstr "Исполнение files()"
msgstr "Исполнение %s()"
#: pkg/build/build.go:768
msgid "Error installing native packages"
@ -469,38 +527,35 @@ msgstr "Скачать все изменённые репозитории"
#: search.go:36
msgid "Search packages"
msgstr ""
msgstr "Поиск пакетов"
#: search.go:42
msgid "Search by name"
msgstr ""
msgstr "Искать по имени"
#: search.go:47
msgid "Search by description"
msgstr ""
msgstr "Искать по описанию"
#: search.go:52
#, fuzzy
msgid "Search by repository"
msgstr "Добавить новый репозиторий"
msgstr "Искать по репозиторию"
#: search.go:57
msgid "Search by provides"
msgstr ""
msgstr "Иcкать по provides"
#: search.go:62
msgid "Format output using a Go template"
msgstr ""
msgstr "Формат выходных данных с использованием шаблона Go"
#: search.go:82 search.go:99
#, fuzzy
msgid "Error parsing format template"
msgstr "Ошибка при разборе файла выпуска операционной системы"
msgstr "Ошибка при разборе шаблона"
#: search.go:107
#, fuzzy
msgid "Error executing template"
msgstr "Ошибка при получении пакетов"
msgstr "Ошибка при выполнении шаблона"
#: upgrade.go:47
msgid "Upgrade all installed packages"

@ -31,6 +31,7 @@ import (
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/translations"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/manager"
@ -118,6 +119,11 @@ func main() {
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Make the application more internationalized
cli.AppHelpTemplate = cliutils.GetAppCliTemplate()
cli.CommandHelpTemplate = cliutils.GetCommandHelpTemplate()
cli.HelpFlag.(*cli.BoolFlag).Usage = gotext.Get("Show help")
err := app.RunContext(ctx, os.Args)
if err != nil {
slog.Error(gotext.Get("Error while running app"), "err", err)

@ -25,7 +25,7 @@ elif (( $(echo "$COVERAGE < 80" | bc -l) )); then
COLOR="#dfb317"
fi
cat <<EOF > coverage-badge.svg
cat <<EOF > assets/coverage-badge.svg
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="109" height="20">
<linearGradient id="smooth" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/></linearGradient>

84
scripts/i18n-badge.sh Executable file

@ -0,0 +1,84 @@
#!/bin/bash
# ALR - Any Linux Repository
# Copyright (C) 2025 Евгений Храмов
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
TRANSLATIONS_DIR="internal/translations/po"
if [ ! -d "$TRANSLATIONS_DIR" ]; then
echo "Error: directory '$TRANSLATIONS_DIR' not found"
exit 1
fi
declare -A TOTAL_STRINGS_MAP
declare -A TRANSLATED_STRINGS_MAP
for PO_FILE in $(find "$TRANSLATIONS_DIR" -type f -name "*.po"); do
LANG_DIR=$(dirname "$PO_FILE")
LANG=$(basename "$LANG_DIR")
STATS=$(LC_ALL=C msgfmt --statistics -o /dev/null "$PO_FILE" 2>&1)
NUMBERS=($(echo "$STATS" | grep -o '[0-9]\+'))
case ${#NUMBERS[@]} in
1) TRANSLATED_STRINGS=${NUMBERS[0]}; UNTRANSLATED_STRINGS=0 ;; # all translated
2) TRANSLATED_STRINGS=${NUMBERS[0]}; UNTRANSLATED_STRINGS=${NUMBERS[1]} ;; # no fuzzy
3) TRANSLATED_STRINGS=${NUMBERS[0]}; UNTRANSLATED_STRINGS=${NUMBERS[2]} ;; # with fuzzy
*) TRANSLATED_STRINGS=0; UNTRANSLATED_STRINGS=0 ;;
esac
TOTAL_STRINGS=$((TRANSLATED_STRINGS + UNTRANSLATED_STRINGS))
TOTAL_STRINGS_MAP[$LANG]=$((TOTAL_STRINGS_MAP[$LANG] + TOTAL_STRINGS))
TRANSLATED_STRINGS_MAP[$LANG]=$((TRANSLATED_STRINGS_MAP[$LANG] + TRANSLATED_STRINGS))
done
for LANG in "${!TOTAL_STRINGS_MAP[@]}"; do
TOTAL=${TOTAL_STRINGS_MAP[$LANG]}
TRANSLATED=${TRANSLATED_STRINGS_MAP[$LANG]}
if [ "$TOTAL" -eq 0 ]; then
PERCENTAGE="0.00"
else
PERCENTAGE=$(echo "scale=2; ($TRANSLATED / $TOTAL) * 100" | bc)
fi
COLOR="#4c1"
if (( $(echo "$PERCENTAGE < 50" | bc -l) )); then
COLOR="#e05d44"
elif (( $(echo "$PERCENTAGE < 80" | bc -l) )); then
COLOR="#dfb317"
fi
cat <<EOF > assets/i18n-$LANG-badge.svg
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="129" height="20">
<linearGradient id="smooth" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/></linearGradient>
<mask id="round">
<rect width="129" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#round)">
<rect width="75" height="20" fill="#555"/>
<rect x="75" width="64" height="20" fill="${COLOR}"/>
<rect width="129" height="20" fill="url(#smooth)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="37" y="15" fill="#010101" fill-opacity=".3">$LANG translate</text>
<text x="37" y="14">$LANG translate</text>
<text x="100" y="15" fill="#010101" fill-opacity=".3">${PERCENTAGE}%</text>
<text x="100" y="14">${PERCENTAGE}%</text>
</g>
</svg>
EOF
done