Skip to content

Commit

Permalink
core: add translation support (what a milestone)
Browse files Browse the repository at this point in the history
currently for testing we gonna use the ./po directory
  • Loading branch information
Toni500github committed May 9, 2024
1 parent 8790475 commit 649bf0d
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SRC = $(sort $(wildcard src/*.cpp))
OBJ = $(SRC:.cpp=.o)
LDFLAGS = -L./src/fmt -L./src/cpr -lcpr -lalpm -lfmt -lidn2 -lssh2 -lcurl -lssl -lcrypto -lpsl -lgssapi_krb5 -lzstd -lbrotlidec -lz
TARGET = taur
CPPFLAGS = -ggdb -pedantic -funroll-all-loops -march=native -isystem include -Wall -std=c++20
CPPFLAGS = -ggdb -pedantic -funroll-all-loops -march=native -isystem include -Wall -std=c++20 -DENABLE_NLS=1

is_cpr_installed := $(shell ldconfig -p | grep libcpr > /dev/null && echo -n yes)

Expand Down
1 change: 1 addition & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
-pedantic
-Wall
-std=c++20
-DENABLE_NLS=1
89 changes: 38 additions & 51 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ using std::string_view;
using std::vector;
using std::unique_ptr;

// taken from pacman
#define _(str) (char *)str
#ifdef ENABLE_NLS
/* here so it doesn't need to be included elsewhere */
#include <libintl.h>
#include <locale.h>
/* define _() as shortcut for gettext() */
#define _(str) gettext(str)
#else
#define _(s) (char *)s
#endif

struct TaurPkg_t;
class TaurBackend;

Expand Down Expand Up @@ -121,7 +129,7 @@ template <typename T>
constexpr bool is_fmt_convertible_v = is_fmt_convertible<T>::value;

template <typename... Args>
void log_println(log_level log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
void log_println(log_level log, const fmt::text_style ts, fmt::runtime_format_string<> fmt, Args&&... args) {
switch (log) {
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
Expand All @@ -137,64 +145,29 @@ void log_println(log_level log, const fmt::text_style& ts, std::string_view fmt,
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
// I don't want to add a '\n' each time i'm writing a log_printf(), I just forget it all the time
fmt::println(ts, fmt::runtime(fmt), std::forward<Args>(args)...);
fmt::println(ts, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void log_println(log_level log, std::string_view fmt, Args&&... args) {
switch (log) {
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::println(fmt::runtime(fmt), std::forward<Args>(args)...);
void log_println(log_level log, fmt::runtime_format_string<> fmt, Args&&... args) {
log_println(log, fmt::text_style(), fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, std::string_view fmt, Args&&... args) {
switch (log) {
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::print(fmt::runtime(fmt), std::forward<Args>(args)...);
void log_println(log_level log, string_view fmt, Args&&... args) {
log_println(log, fmt::text_style(), fmt::runtime(fmt), std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
void log_println(log_level log, const fmt::text_style ts, string_view fmt, Args&&... args) {
log_println(log, ts, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, const fmt::text_style ts, fmt::runtime_format_string<> fmt, Args&&... args) {
switch (log) {
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
Expand All @@ -210,11 +183,25 @@ void log_printf(log_level log, const fmt::text_style& ts, std::string_view fmt,
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::print(ts, fmt::runtime(fmt), std::forward<Args>(args)...);
fmt::print(ts, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, fmt::runtime_format_string<> fmt, Args&&... args) {
log_printf(log, fmt::text_style(), fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, string_view fmt, Args&&... args) {
log_printf(log, fmt::text_style(), fmt::runtime(fmt), std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(log_level log, const fmt::text_style ts, string_view fmt, Args&&... args) {
log_printf(log, ts, fmt, std::forward<Args>(args)...);
}

/** Ask the user a yes or no question.
Expand Down
168 changes: 168 additions & 0 deletions po/taur_italian.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Italian translations for PACKAGE package.
# Copyright (C) 2024 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: taur v0.6.4\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-05-09 18:05+0200\n"
"PO-Revision-Date: 2024-05-09 18:05+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: src/config.cpp:31
#, c++-format
msgid "Warning: TabAUR config folder was not found, Creating folders at {}!"
msgstr ""

#: src/config.cpp:37
#, c++-format
msgid "Warning: {} not found, generating new one"
msgstr ""

#: src/main.cpp:576
msgid "ERROR: only one operation may be used at a time"
msgstr ""

#: src/main.cpp:625
#, c++-format
msgid "ERROR: invalid option '--{}'"
msgstr ""

#: src/main.cpp:701
#, c++-format
msgid "no operation specified (use {} -h for help)"
msgstr "nessuna operation, spariti con un bel {} -h lolo"

#: src/pacman.cpp:115 src/pacman.cpp:200 src/pacman.cpp:219
#, c-format
msgid "None"
msgstr ""

#: src/util.cpp:22
msgid "Architecture"
msgstr ""

#: src/util.cpp:23
msgid "Backup Files"
msgstr ""

#: src/util.cpp:24
msgid "Build Date"
msgstr ""

#: src/util.cpp:25
msgid "Compressed Size"
msgstr ""

#: src/util.cpp:26
msgid "Conflicts With"
msgstr ""

#: src/util.cpp:27
msgid "Depends On"
msgstr ""

#: src/util.cpp:28
msgid "Description"
msgstr ""

#: src/util.cpp:29
msgid "Download Size"
msgstr ""

#: src/util.cpp:30
msgid "Groups"
msgstr ""

#: src/util.cpp:31
msgid "Install Date"
msgstr ""

#: src/util.cpp:32
msgid "Install Reason"
msgstr ""

#: src/util.cpp:33
msgid "Install Script"
msgstr ""

#: src/util.cpp:34
msgid "Installed Size"
msgstr ""

#: src/util.cpp:35
msgid "Licenses"
msgstr ""

#: src/util.cpp:36
msgid "MD5 Sum"
msgstr ""

#: src/util.cpp:37
msgid "Name"
msgstr ""

#: src/util.cpp:38
msgid "Optional Deps"
msgstr ""

#: src/util.cpp:39
msgid "Optional For"
msgstr ""

#: src/util.cpp:40
msgid "Packager"
msgstr ""

#: src/util.cpp:41
msgid "Provides"
msgstr ""

#: src/util.cpp:42
msgid "Replaces"
msgstr ""

#: src/util.cpp:43
msgid "Repository"
msgstr ""

#: src/util.cpp:44
msgid "Required By"
msgstr ""

#: src/util.cpp:45
msgid "SHA-256 Sum"
msgstr ""

#: src/util.cpp:46
msgid "Signatures"
msgstr ""

#: src/util.cpp:47
msgid "URL"
msgstr ""

#: src/util.cpp:48
msgid "Validated By"
msgstr ""

#: src/util.cpp:49
msgid "Version"
msgstr ""

#: src/util.cpp:81
msgid " [installed]"
msgstr ""

#: src/util.cpp:204
#, c++-format
msgid "ERROR: No such enviroment variable: {}"
msgstr "ERRORE::: nessuna variante troavte, sicuro sia questa: {}?"
29 changes: 25 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool execPacman(int argc, char *argv[]) {

char *args[argc + 1]; // null terminator

args[0] = _("pacman");
args[0] = (char*)"pacman";
for (int i = 1; i < argc; ++i)
args[i] = argv[i];

Expand Down Expand Up @@ -500,6 +500,23 @@ bool queryPkgs(alpm_list_t *pkgNames) {
return true;
}

/** Sets up gettext localization. Safe to call multiple times.
*/
/* Inspired by the monotone function localize_monotone. */
#if defined(ENABLE_NLS)
static void localize(void)
{
static int init = 0;
if(!init) {
setlocale(LC_ALL, "");
bindtextdomain("taur", "/home/toni/TabAUR/po");
textdomain("taur");
init = 1;
}
}
#endif

// function taken from pacman
int parseargs(int argc, char* argv[]) {
// default
op.op = OP_MAIN;
Expand Down Expand Up @@ -554,7 +571,7 @@ int parseargs(int argc, char* argv[]) {
execPacman(argc, argv);
}
if (op.op == 0) {
log_println(NONE, "ERROR: only one operation may be used at a time");
log_println(NONE, _("ERROR: only one operation may be used at a time"));
return 1;
}

Expand Down Expand Up @@ -603,7 +620,7 @@ int parseargs(int argc, char* argv[]) {
if (opt < 1000) {
log_println(NONE, "ERROR: invalid option '-{}'", (char)opt);
} else {
log_println(NONE, "ERROR: invalid option '--{}'", opts[option_index].name);
log_println(NONE, _("ERROR: invalid option '--{}'"), opts[option_index].name);
}
}
return result;
Expand All @@ -625,7 +642,11 @@ int parseargs(int argc, char* argv[]) {
// main
int main(int argc, char *argv[]) {
config = std::make_unique<Config>();


#if defined(ENABLE_NLS)
localize();
#endif

configfile = (getConfigDir() + "/config.toml");
themefile = (getConfigDir() + "/theme.toml");

Expand Down

0 comments on commit 649bf0d

Please sign in to comment.