xref: /trunk/main/offapi/com/sun/star/resource/XResourceBundle.idl (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
1*d1766043SAndrew Rist/**************************************************************
2cdf0e10cSrcweir *
3*d1766043SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*d1766043SAndrew Rist * or more contributor license agreements.  See the NOTICE file
5*d1766043SAndrew Rist * distributed with this work for additional information
6*d1766043SAndrew Rist * regarding copyright ownership.  The ASF licenses this file
7*d1766043SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*d1766043SAndrew Rist * "License"); you may not use this file except in compliance
9*d1766043SAndrew Rist * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*d1766043SAndrew Rist *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*d1766043SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*d1766043SAndrew Rist * software distributed under the License is distributed on an
15*d1766043SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d1766043SAndrew Rist * KIND, either express or implied.  See the License for the
17*d1766043SAndrew Rist * specific language governing permissions and limitations
18*d1766043SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*d1766043SAndrew Rist *************************************************************/
21*d1766043SAndrew Rist
22*d1766043SAndrew Rist
23cdf0e10cSrcweir#ifndef __com_sun_star_resource_XResourceBundle_idl__
24cdf0e10cSrcweir#define __com_sun_star_resource_XResourceBundle_idl__
25cdf0e10cSrcweir
26cdf0e10cSrcweir#ifndef __com_sun_star_container_XNameAccess_idl__
27cdf0e10cSrcweir#include <com/sun/star/container/XNameAccess.idl>
28cdf0e10cSrcweir#endif
29cdf0e10cSrcweir
30cdf0e10cSrcweir#ifndef __com_sun_star_lang_Locale_idl__
31cdf0e10cSrcweir#include <com/sun/star/lang/Locale.idl>
32cdf0e10cSrcweir#endif
33cdf0e10cSrcweir
34cdf0e10cSrcweir
35cdf0e10cSrcweir//=============================================================================
36cdf0e10cSrcweir
37cdf0e10cSrcweirmodule com { module sun { module star { module resource {
38cdf0e10cSrcweir
39cdf0e10cSrcweir//=============================================================================
40cdf0e10cSrcweir/** Resource bundles contain locale-specific objects.
41cdf0e10cSrcweir
42cdf0e10cSrcweir    <p>When your program needs a locale-specific resource, such as
43cdf0e10cSrcweir    <code>String</code> for example, your program can load it from the
44cdf0e10cSrcweir    resource bundle that is appropriate for the current user's locale. In
45cdf0e10cSrcweir    this way, you can write program code that is largely independent of
46cdf0e10cSrcweir    the user's locale, which isolates most, if not all, of the
47cdf0e10cSrcweir    locale-specific information in resource bundles.
48cdf0e10cSrcweir
49cdf0e10cSrcweir    <p>This allows you to write programs that can:
50cdf0e10cSrcweir
51cdf0e10cSrcweir    <UL type=SQUARE>
52cdf0e10cSrcweir
53cdf0e10cSrcweir    <LI> be easily localized, or translated, into different
54cdf0e10cSrcweir    languages.
55cdf0e10cSrcweir
56cdf0e10cSrcweir    <LI> handle multiple locales at once.
57cdf0e10cSrcweir
58cdf0e10cSrcweir    <LI> be easily modified, later, to support even more locales.
59cdf0e10cSrcweir
60cdf0e10cSrcweir    </UL>
61cdf0e10cSrcweir
62cdf0e10cSrcweir    <P> One resource bundle is, conceptually, a set of related services
63cdf0e10cSrcweir    that supports <code>XResourceBundle</code>. Each related service of
64cdf0e10cSrcweir    <code>XResourceBundle</code> has the same base name plus an
65cdf0e10cSrcweir    additional component that identifies its locale. For example, suppose
66cdf0e10cSrcweir    your resource bundle is named <code>MyResources</code>. The first
67cdf0e10cSrcweir    service you are likely to implement is the default resource bundle,
68cdf0e10cSrcweir    which has the same name as its family--<code>MyResources</code>. You
69cdf0e10cSrcweir    can also provide as many related locale-specific services as you need.
70cdf0e10cSrcweir
71cdf0e10cSrcweir    For example, perhaps you would provide a German one named
72cdf0e10cSrcweir    <code>MyResources_de</code>.
73cdf0e10cSrcweir
74cdf0e10cSrcweir    <P>
75cdf0e10cSrcweir    Each related implementation of <code>XResourceBundle</code> contains
76cdf0e10cSrcweir    the same items, but the items have been translated for the locale
77cdf0e10cSrcweir    represented by that <code>XResourceBundle</code> implementation. For
78cdf0e10cSrcweir    example, both <code>MyResources</code> and <code>MyResources_de</code>
79cdf0e10cSrcweir    may have a <code>String</code> that is used on a button for
80cdf0e10cSrcweir    confirming operations. In <code>MyResources</code> the
81cdf0e10cSrcweir    <code>String</code> may contain <code>OK</code> and in
82cdf0e10cSrcweir    <code>MyResources_de</code> it may contain <code>Gut</code>.
83cdf0e10cSrcweir
84cdf0e10cSrcweir    <P>
85cdf0e10cSrcweir    If there are different resources for different countries, you
86cdf0e10cSrcweir    can make specializations: for example, <code>MyResources_de_CH</code>
87cdf0e10cSrcweir    is the German language (de) in Switzerland (CH). If you only want to
88cdf0e10cSrcweir    modify some of the resources in the specialization, you can do so.
89cdf0e10cSrcweir
90cdf0e10cSrcweir    <P>
91cdf0e10cSrcweir    When your program needs a locale-specific object, it loads
92cdf0e10cSrcweir
93cdf0e10cSrcweir    the <code>XResourceBundle</code> implementation using the
94cdf0e10cSrcweir    <type>XResourceBundleLoader</type> service:
95cdf0e10cSrcweir
96cdf0e10cSrcweir    <listing>
97cdf0e10cSrcweir    XResourceBundle myResources = xLoader.getBundle("MyResources", currentLocale);
98cdf0e10cSrcweir    </listing>
99cdf0e10cSrcweir
100cdf0e10cSrcweir    <p>The first argument specifies the family name of the resource
101cdf0e10cSrcweir    bundle that contains the object in question. The second argument
102cdf0e10cSrcweir    indicates the desired locale. <code>getBundle</code> uses these two
103cdf0e10cSrcweir    arguments to construct the name of the <code>ResourceBundle</code>
104cdf0e10cSrcweir    subclass it should load according to the following specifications.
105cdf0e10cSrcweir
106cdf0e10cSrcweir    <P>The resource bundle lookup searches for services with various
107cdf0e10cSrcweir    suffixes on the basis of (1) the desired locale and (2) the current
108cdf0e10cSrcweir    default locale as returned by Locale.getDefault(), and (3) the root
109cdf0e10cSrcweir    resource bundle (baseclass), in the following order from lower-level
110cdf0e10cSrcweir    (more specific) to parent-level (less specific):
111cdf0e10cSrcweir    <p> baseclass + "_" + language1 + "_" + country1 + "_" + variant1
112cdf0e10cSrcweir    <BR> baseclass + "_" + language1 + "_" + country1
113cdf0e10cSrcweir    <BR> baseclass + "_" + language1
114cdf0e10cSrcweir    <BR> baseclass + "_" + language2 + "_" + country2 + "_" + variant2
115cdf0e10cSrcweir    <BR> baseclass + "_" + language2 + "_" + country2
116cdf0e10cSrcweir    <BR> baseclass + "_" + language2
117cdf0e10cSrcweir    <BR> baseclass
118cdf0e10cSrcweir
119cdf0e10cSrcweir    <P> For example, if the current default locale is <TT>en_US</TT>, the
120cdf0e10cSrcweir    locale that the caller is interested in is <TT>fr_CH</TT>, and the
121cdf0e10cSrcweir    resource bundle name is <TT>MyResources</TT>; resource bundle lookup
122cdf0e10cSrcweir    will search for the following services, in order:
123cdf0e10cSrcweir    <BR> <TT>MyResources_fr_CH
124cdf0e10cSrcweir    <BR> MyResources_fr
125cdf0e10cSrcweir    <BR> MyResources_en_US
126cdf0e10cSrcweir    <BR> MyResources_en
127cdf0e10cSrcweir    <BR> MyResources</TT>
128cdf0e10cSrcweir
129cdf0e10cSrcweir    <P> The result of the lookup is a service, but that service may be
130cdf0e10cSrcweir    backed by a property file on disk. If a lookup fails,
131cdf0e10cSrcweir    <code>getBundle()</code> throws a
132cdf0e10cSrcweir    <code>MissingResourceException</code>.
133cdf0e10cSrcweir
134cdf0e10cSrcweir    <P> The base service <strong>must</strong> be fully qualified (for
135cdf0e10cSrcweir    example, <code>myPackage::MyResources</code>, not just
136cdf0e10cSrcweir    <code>MyResources</code>).
137cdf0e10cSrcweir
138cdf0e10cSrcweir    <P> Resource bundles contain key/value pairs. The keys uniquely
139cdf0e10cSrcweir    identify a locale-specific object in the bundle. Here is an
140cdf0e10cSrcweir    example of a <code>XResourceBundle</code> implementation that contains
141cdf0e10cSrcweir    two key/value pairs:
142cdf0e10cSrcweir
143cdf0e10cSrcweir    <listing>
144cdf0e10cSrcweir    class MyResource extends com.sun.star.resource.XResourceBundle
145cdf0e10cSrcweir    {
146cdf0e10cSrcweir        // some queryInterface stuff
147cdf0e10cSrcweir        // ...
148cdf0e10cSrcweir        public final Object getDirectElement(String key)
149cdf0e10cSrcweir        {
150cdf0e10cSrcweir            if (key.equals("okKey")) return "Ok";
151cdf0e10cSrcweir            if (key.equals("cancelKey")) return "Cancel";
152cdf0e10cSrcweir            return null;
153cdf0e10cSrcweir        }
154cdf0e10cSrcweir    }
155cdf0e10cSrcweir    </listing>
156cdf0e10cSrcweir
157cdf0e10cSrcweir    <p>Keys are always <code>String</code>s. In this example, the keys
158cdf0e10cSrcweir    are <code>OkKey</code> and <code>CancelKey</code>. In the above
159cdf0e10cSrcweir    example, the values are also <code>String</code>s--<code>OK</code>
160cdf0e10cSrcweir    and <code>Cancel</code>--but they do not have to be. The values can
161cdf0e10cSrcweir    be any type of object.
162cdf0e10cSrcweir
163cdf0e10cSrcweir    <P> You retrieve an object from resource bundle using the appropriate
164cdf0e10cSrcweir    get method. Because <code>OkKey</code> and <code>CancelKey</code>
165cdf0e10cSrcweir    are both strings, you use <code>getByName</code> to retrieve them:
166cdf0e10cSrcweir
167cdf0e10cSrcweir    <listing>
168cdf0e10cSrcweir    button1 = new Button(myResourceBundle.getByName("OkKey").getString());
169cdf0e10cSrcweir    button2 = new Button(myResourceBundle.getByName("CancelKey").getString());
170cdf0e10cSrcweir    </listing>
171cdf0e10cSrcweir
172cdf0e10cSrcweir    <p>The get methods all require the key as an argument and return
173cdf0e10cSrcweir    the object if found. If the object is not found, the get methods
174cdf0e10cSrcweir    throw a <type scope="com::sun::star::container">NoSuchElementException</type>.
175cdf0e10cSrcweir
176cdf0e10cSrcweir    <P> <STRONG>NOTE:</STRONG> You should always supply a base service
177cdf0e10cSrcweir    with no suffixes. This will be the class of "last resort" if a
178cdf0e10cSrcweir    locale is requested that does not exist. In fact, you must provide
179cdf0e10cSrcweir    <I>all</I> of the services in any given inheritance chain for which
180cdf0e10cSrcweir    you provide a resource.  For example, if you provide
181cdf0e10cSrcweir    <TT>MyResources_fr_BE</TT>, you must provide <I>both</I>
182cdf0e10cSrcweir    <TT>MyResources</TT> <I>and</I> <TT>MyResources_fr</TT>, or the
183cdf0e10cSrcweir    resource bundle lookup will not work right.
184cdf0e10cSrcweir
185cdf0e10cSrcweir    <P>You do not have to restrict yourself to using a single family of
186cdf0e10cSrcweir    <code>ResourceBundle</code>s. For example, you could have a set of
187cdf0e10cSrcweir    bundles for exception messages, <code>ExceptionResources</code>
188cdf0e10cSrcweir    (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...),
189cdf0e10cSrcweir    and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>,
190cdf0e10cSrcweir    <code>WidgetResources_de</code>, ...); breaking up the resources however you like.
191cdf0e10cSrcweir
192cdf0e10cSrcweir    @see        MissingResourceException
193cdf0e10cSrcweir    @see         Locale
194cdf0e10cSrcweir    @version     0.1 26 May 1999
195cdf0e10cSrcweir    @author      Mark Davis
196cdf0e10cSrcweir    @author      Markus Meyer
197cdf0e10cSrcweir    @deprecated draft
198cdf0e10cSrcweir*/
199cdf0e10cSrcweirpublished interface XResourceBundle: com::sun::star::container::XNameAccess
200cdf0e10cSrcweir{
201cdf0e10cSrcweir    //-------------------------------------------------------------------------
202cdf0e10cSrcweir    /** contains the parent bundle of this bundle.
203cdf0e10cSrcweir
204cdf0e10cSrcweir        <p>The parent bundle is searched by the method
205cdf0e10cSrcweir        <method scope="com::sun::star::container">XNameAccess::getByName</method>
206cdf0e10cSrcweir        when this bundle does not contain a particular resource.
207cdf0e10cSrcweir     */
208cdf0e10cSrcweir    [attribute] XResourceBundle Parent;
209cdf0e10cSrcweir
210cdf0e10cSrcweir    //-------------------------------------------------------------------------
211cdf0e10cSrcweir    /** @returns
212cdf0e10cSrcweir        the locale for this resource bundle.
213cdf0e10cSrcweir
214cdf0e10cSrcweir        <p>This function can be used to determine whether the
215cdf0e10cSrcweir        resource bundle that is returned really corresponds to the
216cdf0e10cSrcweir        requested locale or is a fallback.
217cdf0e10cSrcweir
218cdf0e10cSrcweir    */
219cdf0e10cSrcweir    com::sun::star::lang::Locale getLocale();
220cdf0e10cSrcweir
221cdf0e10cSrcweir    //-------------------------------------------------------------------------
222cdf0e10cSrcweir    /** @returns
223cdf0e10cSrcweir        an object from a resource bundle or NULL if no resource
224cdf0e10cSrcweir        exists.
225cdf0e10cSrcweir
226cdf0e10cSrcweir        <p>It does not look in the parents.
227cdf0e10cSrcweir
228cdf0e10cSrcweir        @param key
229cdf0e10cSrcweir            specifies the element.
230cdf0e10cSrcweir    */
231cdf0e10cSrcweir    any getDirectElement( [in] string key );
232cdf0e10cSrcweir
233cdf0e10cSrcweir};
234cdf0e10cSrcweir
235cdf0e10cSrcweir//=============================================================================
236cdf0e10cSrcweir
237cdf0e10cSrcweir}; }; }; };
238cdf0e10cSrcweir
239cdf0e10cSrcweir#endif
240