97077d344e3135548fa4cc91c633062f4e0440e9
[pspp-builds.git] / doc / dev / i18n.texi
1 @node Internationalisation
2 @chapter Internationalisation
3
4 Internationalisation in pspp is complicated.
5 The most annoying aspect is that of character-encoding.
6 This chapter attempts to describe the problems and current ways 
7 in which they are addressed.
8
9
10 @section The working locales
11 Pspp has three ``working'' locales:
12
13 @itemize
14 @item The locale of the user interface.
15 @item The locale of the output.
16 @item The locale of the data. Only the character encoding is relevant.
17 @end itemize
18
19 Each of these locales may, at different times take 
20 separate (or identical) values.
21 So for example, a French statistician can use pspp to prepare a report 
22 in the English language, using 
23 a datafile which has been created by a Japanese researcher hence 
24 uses a Japanese character set.
25
26 It's rarely, if ever, necessary to interrogate the system to find out
27 the values of the 3 locales.
28 However it's important to be aware of the source (destination) locale
29 when reading (writing) string data.
30 When transfering data between a source and a destination, the appropriate
31 recoding must be performed.
32
33
34 @subsection The user interface locale
35 This is the locale which is visible to the person using pspp.
36 Error messages and confidence indications  are written in this locale.
37 For example ``Cannot open file'' will be written in the user interface locale.
38
39 This locale is set from the environment of the user who starts pspp@{ire@} or
40 from the system locale if not set.
41
42 @subsection The output locale
43 This locale is the one that should be visible to the person reading a 
44 report generated by pspp.  Non-data related strings (Eg: ``Page number'',
45 ``Standard Deviation'' etc.) will appear in this locale.
46
47 @subsection The data locale
48 This locale is the one associated with the data being analysed with pspp.
49 The only important aspect of this locale is the character encoding.
50 @footnote {It might also be desirable for the LC_COLLATE category to be used for the purposes of sorting data.}
51 The dictionary pertaining to the data contains a field denoting the encoding.
52 Any string data stored in a @union{value} will be encoded in the
53 dictionary's character set.
54
55
56
57 @section System files
58 @file{*.sav} files contain a field which is supposed to identify the encoding
59 of the data they contain (@pxref{Machine Integer Info Record}).  
60 However, many
61 files produced by early versions of spss set this to ``2'' (ASCII) regardless
62 of the encoding of the data.
63 Later versions contain an additional
64 record (@pxref{Character Encoding Record}) describing the encoding.
65 When a system file is read, the dictionary's encoding is set using information 
66 gleened from the system file.
67 If the encoding cannot be determined or would be unreliable, then it 
68 remains unset.
69
70
71 @section GUI
72 The psppire graphic user interface is written using the Gtk+ api, for which 
73 all strings must be encoded in UTF8.
74 All strings passed to the GTK+/GLib library functions (except for filenames)
75 must be UTF-8 encoded otherwise errors will occur.
76 Thus, for the purposes of the programming psppire, the user interface locale 
77 should be assumed to be UTF8, even if setlocale and/or nl_langinfo
78 indicates otherwise.
79
80 @subsection Filenames
81 The GLib API has some special functions for dealing with filenames.
82 Strings returned from functions like gtk_file_chooser_dialog_get_name are not, 
83 in general, encoded in UTF8, but in ``filename'' encoding.
84 If that filename is passed to another GLib function which expects a filename, 
85 no conversion is necessary.
86 If it's passed to a function for the purposes of displaying it (eg. in a 
87 window's title-bar) it must be converted to UTF8 --- there is a special 
88 function for this: g_filename_display_name or g_filename_basename.
89 If however, a filename needs to be passed outside of GTK+/GLib (for example to fopen) it must be converted to the local system encoding.
90
91
92 @section Existing locale handling functions
93 The major aspect of locale handling which the programmer has to consider is
94 that of character encoding.
95
96 The following function is used to recode strings:
97
98 @deftypefun char * recode_string (const char *@var{to}, const char *@var{from}, const char *@var{text}, int @var{len});
99
100 Converts the string @var{text}, which is encoded in @var{from} to a new string encoded in @var{to} encoding.
101 If @var{len} is not -1, then it must be the number of bytes in @var{text}.
102 It is the caller's responsibility to free the returned string when no 
103 longer required.
104 @end deftypefun
105
106
107 For example, in order to display a string variable's value in a label widget in the psppire gui one would use code similar to
108 @example
109
110 struct variable *var = /* assigned from somewhere */
111 struct case c = /* from somewhere else */
112
113 const union value *val = case_data (&c, var);
114
115 char *utf8string = recode_string (UTF8, dict_get_encoding (dict), val->s,
116       var_get_width (var));
117
118 GtkWidget *entry = gtk_entry_new();
119 gtk_entry_set_text (entry, utf8string);
120 gtk_widget_show (entry);
121
122 free (utf8string);
123
124 @end example
125
126
127
128 @section Quirks
129 For historical reasons, not all locale handling follows posix conventions.
130 This makes it difficult (impossible?) to elegantly handle the issues.
131 For example, it would make sense for the gui's datasheet to display
132 numbers formatted according to the LC_NUMERIC category of the data locale.
133 Instead however there is the @func{data_out} function 
134 (@pxref{Obtaining Properties of Format Types}) which uses the
135 @func{settings_get_decimal_char} function instead of the decimal separator 
136 of the locale.  Similarly, formatting of monetary values is displayed 
137 in a pspp/spss specific fashion instead of using the LC_MONETARY category.
138
139
140
141 @c  LocalWords:  pspp itemize Eg LC Spss cmd sav pxref spss GUI psppire Gtk api
142 @c  LocalWords:  UTF gtk setlocale nl langinfo deftypefun enum conv var const
143 @c  LocalWords:  int len gui struct val utf GtkWidget posix gui's datasheet
144 @c  LocalWords:  func