1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
This file is part of TALER
Copyright (C) 2020, 2021 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 <http://www.gnu.org/licenses/>
*/
/**
* @file json/i18n.c
* @brief helper functions for i18n in JSON processing
* @author Christian Grothoff
*/
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_util.h"
#include "taler_json_lib.h"
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;
}
bool
TALER_JSON_check_i18n (const json_t *i18n)
{
const char *field;
json_t *member;
if (! json_is_object (i18n))
return false;
json_object_foreach ((json_t *) i18n, field, member)
{
if (! json_is_string (member))
return false;
/* Field name must be either of format "en_UK"
or just "en"; we do not care about capitalization;
for syntax, see GNU Gettext manual, including
appendix A for rare language codes. */
switch (strlen (field))
{
case 0:
case 1:
return false;
case 2:
if (! isalpha (field[0]))
return false;
if (! isalpha (field[1]))
return false;
break;
case 3:
case 4:
return false;
case 5:
if (! isalpha (field[0]))
return false;
if (! isalpha (field[1]))
return false;
if ('_' != field[2])
return false;
if (! isalpha (field[3]))
return false;
if (! isalpha (field[4]))
return false;
break;
case 6:
if (! isalpha (field[0]))
return false;
if (! isalpha (field[1]))
return false;
if ('_' != field[2])
return false;
if (! isalpha (field[3]))
return false;
if (! isalpha (field[4]))
return false;
if (! isalpha (field[5]))
return false;
break;
default:
return false;
}
}
return true;
}
/* end of i18n.c */
|