2
// GettextDomain.cs: Wrappers for the libintl library.
5
// Edd Dumbill (edd@usefulinc.com)
6
// Jonathan Pryor (jonpryor@vt.edu)
7
// Lluis Sanchez Gual (lluis@novell.com)
9
// (C) 2004 Edd Dumbill
10
// (C) 2005-2006 Jonathan Pryor
11
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
13
// Permission is hereby granted, free of charge, to any person obtaining
14
// a copy of this software and associated documentation files (the
15
// "Software"), to deal in the Software without restriction, including
16
// without limitation the rights to use, copy, modify, merge, publish,
17
// distribute, sublicense, and/or sell copies of the Software, and to
18
// permit persons to whom the Software is furnished to do so, subject to
19
// the following conditions:
21
// The above copyright notice and this permission notice shall be
22
// included in all copies or substantial portions of the Software.
24
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
using System.Runtime.InteropServices;
38
namespace Mono.Addins.Localization
43
static extern IntPtr bindtextdomain (IntPtr domainname, IntPtr dirname);
45
static extern IntPtr bind_textdomain_codeset (IntPtr domainname, IntPtr codeset);
47
static extern IntPtr dgettext (IntPtr domainname, IntPtr instring);
49
static extern IntPtr dngettext (IntPtr domainname, IntPtr instring, IntPtr plural, int n);
53
public void Init (String package, string localedir)
55
if (localedir == null) {
56
localedir = System.Reflection.Assembly.GetEntryAssembly ().CodeBase;
57
FileInfo f = new FileInfo (localedir);
58
string prefix = f.Directory.Parent.Parent.Parent.ToString ();
59
prefix = Path.Combine (Path.Combine (prefix, "share"), "locale");
62
ipackage = StringToPtr (package);
63
IntPtr ilocaledir = StringToPtr (localedir);
64
IntPtr iutf8 = StringToPtr ("UTF-8");
67
if (bindtextdomain (ipackage, ilocaledir) == IntPtr.Zero)
68
throw new InvalidOperationException ("Gettext localizer: bindtextdomain failed");
69
if (bind_textdomain_codeset (ipackage, iutf8) == IntPtr.Zero)
70
throw new InvalidOperationException ("Gettext localizer: bind_textdomain_codeset failed");
73
Marshal.FreeHGlobal (ilocaledir);
74
Marshal.FreeHGlobal (iutf8);
80
Marshal.FreeHGlobal (ipackage);
83
public String GetString (String s)
85
IntPtr ints = StringToPtr (s);
87
// gettext(3) returns the input pointer if no translation is found
88
IntPtr r = dgettext (ipackage, ints);
90
return PtrToString (r);
94
Marshal.FreeHGlobal (ints);
98
public String GetPluralString (String singular, String defaultPlural, int n)
100
IntPtr ints = StringToPtr (singular);
101
IntPtr intp = StringToPtr (defaultPlural);
103
// gettext(3) returns the input pointer if no translation is found
104
IntPtr r = dngettext (ipackage, ints, intp, n);
108
return defaultPlural;
109
return PtrToString (r);
112
Marshal.FreeHGlobal (ints);
113
Marshal.FreeHGlobal (intp);
117
static IntPtr StringToPtr (string s)
121
byte[] marshal = Encoding.UTF8.GetBytes (s);
122
IntPtr mem = Marshal.AllocHGlobal (marshal.Length + 1);
123
Marshal.Copy (marshal, 0, mem, marshal.Length);
124
Marshal.WriteByte (mem, marshal.Length, 0);
128
static string PtrToString (IntPtr ptr)
130
if (ptr == IntPtr.Zero)
133
while (Marshal.ReadByte (ptr, sz) != 0)
135
byte[] bytes = new byte [sz];
136
Marshal.Copy (ptr, bytes, 0, sz);
137
return Encoding.UTF8.GetString (bytes);