From 2068b4e8911a3a49cded44b4568f6c943a8c98f8 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 27 Jul 2014 04:29:56 -0400 Subject: implement gettext message translation functions this commit replaces the stub implementations with working message translation functions. translation units are factored so as to prevent pulling in the legacy, non-library-safe functions which use a global textdomain in modern code which is using the versions with an explicit domain argument. bind_textdomain_codeset is also placed in its own file since it should not be needed by most programs. this implementation is still missing some features: the LANGUAGE environment variable (for multiple fallback languages) is not honored, and non-default plural-form rules are not supported. these issues will be addressed in a later commit. one notable difference from the GNU implementation is that there is no default path for loading translation files. in principle one could be added, but since the documented correct usage is to call the bindtextdomain function, a default path is probably unnecessary. --- src/locale/textdomain.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/locale/textdomain.c (limited to 'src/locale/textdomain.c') diff --git a/src/locale/textdomain.c b/src/locale/textdomain.c new file mode 100644 index 00000000..c501501d --- /dev/null +++ b/src/locale/textdomain.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include "libc.h" +#include "atomic.h" + +static char *current_domain; + +char *__gettextdomain() +{ + return current_domain ? current_domain : "messages"; +} + +char *textdomain(const char *domainname) +{ + if (!domainname) return __gettextdomain(); + + size_t domlen = strlen(domainname); + if (domlen > NAME_MAX) { + errno = EINVAL; + return 0; + } + + if (!current_domain) { + current_domain = malloc(NAME_MAX+1); + if (!current_domain) return 0; + } + + memcpy(current_domain, domainname, domlen+1); + + return current_domain; +} + +char *gettext(const char *msgid) +{ + return dgettext(0, msgid); +} + +char *ngettext(const char *msgid1, const char *msgid2, unsigned long int n) +{ + return dngettext(0, msgid1, msgid2, n); +} -- cgit v1.2.1