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