i18n.c (3276B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020, 2021 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file json/i18n.c 18 * @brief helper functions for i18n in JSON processing 19 * @author Christian Grothoff 20 */ 21 #include "taler/platform.h" 22 #include <gnunet/gnunet_util_lib.h> 23 #include "taler/taler_util.h" 24 #include "taler/taler_json_lib.h" 25 26 27 const json_t * 28 TALER_JSON_extract_i18n (const json_t *object, 29 const char *language_pattern, 30 const char *field) 31 { 32 const json_t *ret; 33 json_t *i18n; 34 double quality = -1; 35 36 ret = json_object_get (object, 37 field); 38 if (NULL == ret) 39 return NULL; /* field MUST exist in object */ 40 { 41 char *name; 42 43 GNUNET_asprintf (&name, 44 "%s_i18n", 45 field); 46 i18n = json_object_get (object, 47 name); 48 GNUNET_free (name); 49 } 50 if (NULL == i18n) 51 return ret; 52 { 53 const char *key; 54 json_t *value; 55 56 json_object_foreach (i18n, key, value) { 57 double q = TALER_pattern_matches (language_pattern, 58 key); 59 if (q > quality) 60 { 61 quality = q; 62 ret = value; 63 } 64 } 65 } 66 return ret; 67 } 68 69 70 bool 71 TALER_JSON_check_i18n (const json_t *i18n) 72 { 73 const char *field; 74 json_t *member; 75 76 if (! json_is_object (i18n)) 77 return false; 78 json_object_foreach ((json_t *) i18n, field, member) 79 { 80 if (! json_is_string (member)) 81 return false; 82 /* Field name must be either of format "en_UK" 83 or just "en"; we do not care about capitalization; 84 for syntax, see GNU Gettext manual, including 85 appendix A for rare language codes. */ 86 switch (strlen (field)) 87 { 88 case 0: 89 case 1: 90 return false; 91 case 2: 92 if (! isalpha (field[0])) 93 return false; 94 if (! isalpha (field[1])) 95 return false; 96 break; 97 case 3: 98 case 4: 99 return false; 100 case 5: 101 if (! isalpha (field[0])) 102 return false; 103 if (! isalpha (field[1])) 104 return false; 105 if ('_' != field[2]) 106 return false; 107 if (! isalpha (field[3])) 108 return false; 109 if (! isalpha (field[4])) 110 return false; 111 break; 112 case 6: 113 if (! isalpha (field[0])) 114 return false; 115 if (! isalpha (field[1])) 116 return false; 117 if ('_' != field[2]) 118 return false; 119 if (! isalpha (field[3])) 120 return false; 121 if (! isalpha (field[4])) 122 return false; 123 if (! isalpha (field[5])) 124 return false; 125 break; 126 default: 127 return false; 128 } 129 } 130 return true; 131 } 132 133 134 /* end of i18n.c */