~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Localization/GettextDomain.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// GettextDomain.cs: Wrappers for the libintl library.
 
3
//
 
4
// Authors:
 
5
//   Edd Dumbill (edd@usefulinc.com)
 
6
//   Jonathan Pryor (jonpryor@vt.edu)
 
7
//   Lluis Sanchez Gual (lluis@novell.com)
 
8
//
 
9
// (C) 2004 Edd Dumbill
 
10
// (C) 2005-2006 Jonathan Pryor
 
11
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
12
//
 
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:
 
20
// 
 
21
// The above copyright notice and this permission notice shall be
 
22
// included in all copies or substantial portions of the Software.
 
23
// 
 
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.
 
31
//
 
32
 
 
33
using System;
 
34
using System.Runtime.InteropServices;
 
35
using System.IO;
 
36
using System.Text;
 
37
 
 
38
namespace Mono.Addins.Localization
 
39
{
 
40
        class GettextDomain
 
41
        {
 
42
                [DllImport("intl")]
 
43
                static extern IntPtr bindtextdomain (IntPtr domainname, IntPtr dirname);
 
44
                [DllImport("intl")]
 
45
                static extern IntPtr bind_textdomain_codeset (IntPtr domainname, IntPtr codeset);
 
46
                [DllImport("intl")]
 
47
                static extern IntPtr dgettext (IntPtr domainname, IntPtr instring);
 
48
                [DllImport("intl")]
 
49
                static extern IntPtr dngettext (IntPtr domainname, IntPtr instring, IntPtr plural, int n);
 
50
                
 
51
                IntPtr ipackage;
 
52
                
 
53
                public void Init (String package, string localedir)
 
54
                {
 
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");
 
60
                        }
 
61
                        
 
62
                        ipackage = StringToPtr (package);
 
63
                        IntPtr ilocaledir = StringToPtr (localedir);
 
64
                        IntPtr iutf8 = StringToPtr ("UTF-8");
 
65
                        
 
66
                        try {
 
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");
 
71
                        }
 
72
                        finally {
 
73
                                Marshal.FreeHGlobal (ilocaledir);
 
74
                                Marshal.FreeHGlobal (iutf8);
 
75
                        }
 
76
                }
 
77
                
 
78
                ~GettextDomain ()
 
79
                {
 
80
                        Marshal.FreeHGlobal (ipackage);
 
81
                }
 
82
 
 
83
                public String GetString (String s)
 
84
                {
 
85
                        IntPtr ints = StringToPtr (s);
 
86
                        try {
 
87
                                // gettext(3) returns the input pointer if no translation is found
 
88
                                IntPtr r = dgettext (ipackage, ints);
 
89
                                if (r != ints)
 
90
                                        return PtrToString (r);
 
91
                                return s;
 
92
                        }
 
93
                        finally {
 
94
                                Marshal.FreeHGlobal (ints);
 
95
                        }
 
96
                }
 
97
                
 
98
                public String GetPluralString (String singular, String defaultPlural, int n)
 
99
                {
 
100
                        IntPtr ints = StringToPtr (singular);
 
101
                        IntPtr intp = StringToPtr (defaultPlural);
 
102
                        try {
 
103
                                // gettext(3) returns the input pointer if no translation is found
 
104
                                IntPtr r = dngettext (ipackage, ints, intp, n);
 
105
                                if (r == ints)
 
106
                                        return singular;
 
107
                                if (r == intp)
 
108
                                        return defaultPlural;
 
109
                                return PtrToString (r);
 
110
                        }
 
111
                        finally {
 
112
                                Marshal.FreeHGlobal (ints);
 
113
                                Marshal.FreeHGlobal (intp);
 
114
                        }
 
115
                }
 
116
                
 
117
                static IntPtr StringToPtr (string s)
 
118
                {
 
119
                        if (s == null)
 
120
                                return IntPtr.Zero;
 
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);
 
125
            return mem;         
 
126
                }
 
127
                
 
128
                static string PtrToString (IntPtr ptr)
 
129
                {
 
130
            if (ptr == IntPtr.Zero)
 
131
                return null;
 
132
                        int sz = 0;
 
133
                        while (Marshal.ReadByte (ptr, sz) != 0)
 
134
                                sz++;
 
135
            byte[] bytes = new byte [sz];
 
136
            Marshal.Copy (ptr, bytes, 0, sz);
 
137
            return Encoding.UTF8.GetString (bytes);
 
138
                }
 
139
        }
 
140
}