From 46dde9368f75013b2383c24d4c8a11763ac8e31e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 19 Aug 2020 18:19:00 +0200 Subject: implement i18n lookup logic for #6458 --- src/include/taler_json_lib.h | 31 +++++++++++++++ src/include/taler_mhd_lib.h | 16 -------- src/include/taler_util.h | 16 ++++++++ src/json/Makefile.am | 1 + src/json/i18n.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ src/mhd/mhd_legal.c | 54 ++----------------------- src/util/Makefile.am | 1 + src/util/lang.c | 71 +++++++++++++++++++++++++++++++++ 8 files changed, 219 insertions(+), 66 deletions(-) create mode 100644 src/json/i18n.c create mode 100644 src/util/lang.c diff --git a/src/include/taler_json_lib.h b/src/include/taler_json_lib.h index 171b3d009..a1e4d8830 100644 --- a/src/include/taler_json_lib.h +++ b/src/include/taler_json_lib.h @@ -293,6 +293,37 @@ TALER_JSON_exchange_wire_signature_make ( const struct TALER_MasterPrivateKeyP *master_priv); +/** + * Extract a string from @a object under the field @a field, but respecting + * the Taler i18n rules and the language preferences expressed in @a + * language_pattern. + * + * Basically, the @a object may optionally contain a sub-object + * "${field}_i18n" with a map from IETF BCP 47 language tags to a localized + * version of the string. If this map exists and contains an entry that + * matches the @a language pattern, that object (usually a string) is + * returned. If the @a language_pattern does not match any entry, or if the + * i18n sub-object does not exist, we simply return @a field of @a object + * (also usually a string). + * + * If @a object does not have a member @a field we return NULL (error). + * + * @param object the object to extract internationalized + * content from + * @param language_pattern a language preferences string + * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1", following + * https://tools.ietf.org/html/rfc7231#section-5.3.1 + * @param field name of the field to extract + * @return NULL on error, otherwise the member from + * @a object. Note that the reference counter is + * NOT incremented. + */ +const json_t * +TALER_JSON_extract_i18n (const json_t *object, + const char *language_pattern, + const char *field); + + /** * Obtain the wire method associated with the given * wire account details. @a wire_s must contain a payto://-URL diff --git a/src/include/taler_mhd_lib.h b/src/include/taler_mhd_lib.h index 7d281662d..4b34f41df 100644 --- a/src/include/taler_mhd_lib.h +++ b/src/include/taler_mhd_lib.h @@ -96,22 +96,6 @@ MHD_RESULT TALER_MHD_can_compress (struct MHD_Connection *connection); -/** - * Check if @a lang matches the @a language_pattern, and if so with - * which preference. - * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1 - * - * @param language_pattern a language preferences string - * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1" - * @param lang the 2-digit language to match - * @return q-weight given for @a lang in @a language_pattern, 1.0 if no weights are given; - * 0 if @a lang is not in @a language_pattern - */ -double -TALER_MHD_language_matches (const char *language_pattern, - const char *lang); - - /** * Send JSON object as response. * diff --git a/src/include/taler_util.h b/src/include/taler_util.h index 1d1c01eaf..2a64fe8e9 100644 --- a/src/include/taler_util.h +++ b/src/include/taler_util.h @@ -184,6 +184,22 @@ char * TALER_urlencode (const char *s); +/** + * Check if @a lang matches the @a language_pattern, and if so with + * which preference. + * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1 + * + * @param language_pattern a language preferences string + * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1" + * @param lang the 2-digit language to match + * @return q-weight given for @a lang in @a language_pattern, 1.0 if no weights are given; + * 0 if @a lang is not in @a language_pattern + */ +double +TALER_language_matches (const char *language_pattern, + const char *lang); + + /** * Find out if an MHD connection is using HTTPS (either * directly or via proxy). diff --git a/src/json/Makefile.am b/src/json/Makefile.am index 2910d0773..d7c569d60 100644 --- a/src/json/Makefile.am +++ b/src/json/Makefile.am @@ -10,6 +10,7 @@ lib_LTLIBRARIES = \ libtalerjson.la libtalerjson_la_SOURCES = \ + i18n.c \ json.c \ json_helper.c \ json_wire.c diff --git a/src/json/i18n.c b/src/json/i18n.c new file mode 100644 index 000000000..b92d63ed1 --- /dev/null +++ b/src/json/i18n.c @@ -0,0 +1,95 @@ +/* + This file is part of TALER + Copyright (C) 2020 Taler Systems SA + + TALER 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, or (at your option) any later version. + + TALER 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 + TALER; see the file COPYING. If not, see +*/ +/** + * @file json/i18n.c + * @brief helper functions for i18n in JSON processing + * @author Christian Grothoff + */ +#include "platform.h" +#include +#include "taler_util.h" +#include "taler_json_lib.h" + + +/** + * Extract a string from @a object under the field @a field, but respecting + * the Taler i18n rules and the language preferences expressed in @a + * language_pattern. + * + * Basically, the @a object may optionally contain a sub-object + * "${field}_i18n" with a map from IETF BCP 47 language tags to a localized + * version of the string. If this map exists and contains an entry that + * matches the @a language pattern, that object (usually a string) is + * returned. If the @a language_pattern does not match any entry, or if the + * i18n sub-object does not exist, we simply return @a field of @a object + * (also usually a string). + * + * If @a object does not have a member @a field we return NULL (error). + * + * @param object the object to extract internationalized + * content from + * @param language_pattern a language preferences string + * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1", following + * https://tools.ietf.org/html/rfc7231#section-5.3.1 + * @param field name of the field to extract + * @return NULL on error, otherwise the member from + * @a object. Note that the reference counter is + * NOT incremented. + */ +const json_t * +TALER_JSON_extract_i18n (const json_t *object, + const char *language_pattern, + const char *field) +{ + const json_t *ret; + json_t *i18n; + double quality = -1; + + ret = json_object_get (object, + field); + if (NULL == ret) + return NULL; /* field MUST exist in object */ + { + char *name; + + GNUNET_asprintf (&name, + "%s_i18n", + field); + i18n = json_object_get (object, + name); + GNUNET_free (name); + } + if (NULL == i18n) + return ret; + { + const char *key; + json_t *value; + + json_object_foreach (i18n, key, value) { + double q = TALER_language_matches (language_pattern, + key); + if (q > quality) + { + quality = q; + ret = value; + } + } + } + return ret; +} + + +/* end of i18n.c */ diff --git a/src/mhd/mhd_legal.c b/src/mhd/mhd_legal.c index 7de189cce..0f2433c2c 100644 --- a/src/mhd/mhd_legal.c +++ b/src/mhd/mhd_legal.c @@ -154,52 +154,6 @@ xmime_matches (const char *accept_pattern, } -/** - * Check if @a lang matches the @a language_pattern, and if so with - * which preference. - * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1 - * - * @param language_pattern a language preferences string - * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1" - * @param lang the 2-digit language to match - * @return q-weight given for @a lang in @a language_pattern, 1.0 if no weights are given; - * 0 if @a lang is not in @a language_pattern - */ -double -TALER_MHD_language_matches (const char *language_pattern, - const char *lang) -{ - char *p = GNUNET_strdup (language_pattern); - char *sptr; - double r = 0.0; - - for (char *tok = strtok_r (p, ",", &sptr); - NULL != tok; - tok = strtok_r (NULL, ",", &sptr)) - { - char *sptr2; - char *lp = strtok_r (tok, ";", &sptr2); - char *qp = strtok_r (NULL, ";", &sptr2); - double q = 1.0; - - while (isspace ((int) *lp)) - lp++; - if (NULL != qp) - while (isspace ((int) *qp)) - qp++; - GNUNET_break_op ( (NULL == qp) || - (1 == sscanf (qp, - "q=%lf", - &q)) ); - if (0 == strcasecmp (lang, - lp)) - r = GNUNET_MAX (r, q); - } - GNUNET_free (p); - return r; -} - - /** * Generate a response with a legal document in the format and language of the * user's choosing. @@ -271,10 +225,10 @@ TALER_MHD_reply_legal (struct MHD_Connection *conn, if ( (NULL == t) || (! xmime_matches (mime, t->mime_type)) || - (TALER_MHD_language_matches (lang, - p->language) > - TALER_MHD_language_matches (lang, - t->language) ) ) + (TALER_language_matches (lang, + p->language) > + TALER_language_matches (lang, + t->language) ) ) t = p; } } diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 3831dd3fb..c25e5700d 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -38,6 +38,7 @@ libtalerutil_la_SOURCES = \ crypto.c \ crypto_wire.c \ getopt.c \ + lang.c \ mhd.c \ payto.c \ taler_error_codes.c \ diff --git a/src/util/lang.c b/src/util/lang.c new file mode 100644 index 000000000..3f6a4291f --- /dev/null +++ b/src/util/lang.c @@ -0,0 +1,71 @@ +/* + This file is part of TALER + Copyright (C) 2020 Taler Systems SA + + TALER 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, or (at your option) any later version. + + TALER 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 + TALER; see the file COPYING. If not, see +*/ +/** + * @file lang.c + * @brief Utility functions for parsing and matching RFC 7231 language strings. + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_util.h" + + +/** + * Check if @a lang matches the @a language_pattern, and if so with + * which preference. + * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1 + * + * @param language_pattern a language preferences string + * like "fr-CH, fr;q=0.9, en;q=0.8, *;q=0.1" + * @param lang the 2-digit language to match + * @return q-weight given for @a lang in @a language_pattern, 1.0 if no weights are given; + * 0 if @a lang is not in @a language_pattern + */ +double +TALER_language_matches (const char *language_pattern, + const char *lang) +{ + char *p = GNUNET_strdup (language_pattern); + char *sptr; + double r = 0.0; + + for (char *tok = strtok_r (p, ",", &sptr); + NULL != tok; + tok = strtok_r (NULL, ",", &sptr)) + { + char *sptr2; + char *lp = strtok_r (tok, ";", &sptr2); + char *qp = strtok_r (NULL, ";", &sptr2); + double q = 1.0; + + while (isspace ((int) *lp)) + lp++; + if (NULL != qp) + while (isspace ((int) *qp)) + qp++; + GNUNET_break_op ( (NULL == qp) || + (1 == sscanf (qp, + "q=%lf", + &q)) ); + if (0 == strcasecmp (lang, + lp)) + r = GNUNET_MAX (r, q); + } + GNUNET_free (p); + return r; +} + + +/* end of lang.c */ -- cgit v1.2.3