@@ -67,24 +67,18 @@ abstract class AbstractLocalizedTextProvider implements LocalizedTextProvider {
6767 private final Set <String > missingBundles = ConcurrentHashMap .newKeySet ();
6868 private final ConcurrentMap <Integer , ClassLoader > delegatedClassLoaderMap = new ConcurrentHashMap <>();
6969
70- /**
71- * Adds the bundle to the internal list of default bundles.
72- * If the bundle already exists in the list it will be re-added.
73- *
74- * @param resourceBundleName the name of the bundle to add.
75- */
7670 @ Override
77- public void addDefaultResourceBundle (String resourceBundleName ) {
71+ public void addDefaultResourceBundle (String bundleName ) {
7872 //make sure this doesn't get added more than once
7973 final ClassLoader ccl = getCurrentThreadContextClassLoader ();
8074 synchronized (XWORK_MESSAGES_BUNDLE ) {
8175 List <String > bundles = classLoaderMap .computeIfAbsent (ccl .hashCode (), k -> new CopyOnWriteArrayList <>());
82- bundles .remove (resourceBundleName );
83- bundles .add (0 , resourceBundleName );
76+ bundles .remove (bundleName );
77+ bundles .add (0 , bundleName );
8478 }
8579
8680 if (LOG .isDebugEnabled ()) {
87- LOG .debug ("Added default resource bundle '{}' to default resource bundles for the following classloader '{}'" , resourceBundleName , ccl .toString ());
81+ LOG .debug ("Added default resource bundle '{}' to default resource bundles for the following classloader '{}'" , bundleName , ccl .toString ());
8882 }
8983 }
9084
@@ -113,103 +107,62 @@ public void setCustomI18NResources(String bundles) {
113107 }
114108 }
115109
116- /**
117- * Returns a localized message for the specified key, aTextName. Neither the key nor the
118- * message is evaluated.
119- *
120- * @param aTextName the message key
121- * @param locale the locale the message should be for
122- * @return a localized message based on the specified key, or null if no localized message can be found for it
123- */
124110 @ Override
125- public String findDefaultText (String aTextName , Locale locale ) {
111+ public String findDefaultText (String textKey , Locale locale ) {
126112 List <String > localList = getCurrentBundleNames ();
127113
128114 for (String bundleName : localList ) {
129115 ResourceBundle bundle = findResourceBundle (bundleName , locale );
130116 if (bundle != null ) {
131117 reloadBundles ();
132118 try {
133- return bundle .getString (aTextName );
119+ return bundle .getString (textKey );
134120 } catch (MissingResourceException e ) {
135121 // will be logged when not found in any bundle
136122 }
137123 }
138124 }
139125
140126 if (devMode ) {
141- LOG .warn ("Missing key [{}] in bundles [{}]!" , aTextName , localList );
127+ LOG .warn ("Missing key [{}] in bundles [{}]!" , textKey , localList );
142128 } else {
143- LOG .debug ("Missing key [{}] in bundles [{}]!" , aTextName , localList );
129+ LOG .debug ("Missing key [{}] in bundles [{}]!" , textKey , localList );
144130 }
145131
146132 return null ;
147133 }
148134
149- /**
150- * Returns a localized message for the specified key, aTextName, substituting variables from the
151- * array of params into the message. Neither the key nor the message is evaluated.
152- *
153- * @param aTextName the message key
154- * @param locale the locale the message should be for
155- * @param params an array of objects to be substituted into the message text
156- * @return A formatted message based on the specified key, or null if no localized message can be found for it
157- */
158135 @ Override
159- public String findDefaultText (String aTextName , Locale locale , Object [] params ) {
160- String defaultText = findDefaultText (aTextName , locale );
136+ public String findDefaultText (String textKey , Locale locale , Object [] params ) {
137+ String defaultText = findDefaultText (textKey , locale );
161138 if (defaultText != null ) {
162139 MessageFormat mf = buildMessageFormat (defaultText , locale );
163140 return formatWithNullDetection (mf , params );
164141 }
165142 return null ;
166143 }
167144
168-
169- /**
170- * <p>
171- * Finds a localized text message for the given key, aTextName, in the specified resource
172- * bundle.
173- * </p>
174- *
175- * <p>
176- * If a message is found, it will also be interpolated. Anything within <code>${...}</code>
177- * will be treated as an OGNL expression and evaluated as such.
178- * </p>
179- *
180- * <p>
181- * If a message is <b>not</b> found a WARN log will be logged.
182- * </p>
183- *
184- * @param bundle the bundle
185- * @param aTextName the key
186- * @param locale the locale
187- * @param defaultMessage the default message to use if no message was found in the bundle
188- * @param args arguments for the message formatter.
189- * @param valueStack the OGNL value stack.
190- * @return the localized text, or null if none can be found and no defaultMessage is provided
191- */
192145 @ Override
193- public String findText (ResourceBundle bundle , String aTextName , Locale locale , String defaultMessage , Object [] args ,
146+ public String findText (ResourceBundle bundle , String textKey , Locale locale , String defaultMessage , Object [] args ,
194147 ValueStack valueStack ) {
195148 try {
196149 reloadBundles (valueStack .getContext ());
197150
198- String message = TextParseUtil .translateVariables (bundle .getString (aTextName ), valueStack );
151+ String message = TextParseUtil .translateVariables (bundle .getString (textKey ), valueStack );
199152 MessageFormat mf = buildMessageFormat (message , locale );
200153
201154 return formatWithNullDetection (mf , args );
202155 } catch (MissingResourceException ex ) {
203156 if (devMode ) {
204- LOG .warn ("Missing key [{}] in bundle [{}]!" , aTextName , bundle );
157+ LOG .warn ("Missing key [{}] in bundle [{}]!" , textKey , bundle );
205158 } else {
206- LOG .debug ("Missing key [{}] in bundle [{}]!" , aTextName , bundle );
159+ LOG .debug ("Missing key [{}] in bundle [{}]!" , textKey , bundle );
207160 }
208161 }
209162
210- GetDefaultMessageReturnArg result = getDefaultMessage (aTextName , locale , valueStack , args , defaultMessage );
163+ GetDefaultMessageReturnArg result = getDefaultMessage (textKey , locale , valueStack , args , defaultMessage );
211164 if (unableToFindTextForKey (result )) {
212- LOG .warn ("Unable to find text for key '{}' in ResourceBundles for locale '{}'" , aTextName , locale );
165+ LOG .warn ("Unable to find text for key '{}' in ResourceBundles for locale '{}'" , textKey , locale );
213166 }
214167 return result != null ? result .message : null ;
215168 }
@@ -425,20 +378,10 @@ public void setSearchDefaultBundlesFirst(String searchDefaultBundlesFirst) {
425378 this .searchDefaultBundlesFirst = Boolean .parseBoolean (searchDefaultBundlesFirst );
426379 }
427380
428- /**
429- * Finds the given resource bundle by it's name.
430- * <p>
431- * Will use <code>Thread.currentThread().getContextClassLoader()</code> as the classloader.
432- * </p>
433- *
434- * @param aBundleName the name of the bundle (usually it's FQN classname).
435- * @param locale the locale.
436- * @return the bundle, <tt>null</tt> if not found.
437- */
438381 @ Override
439- public ResourceBundle findResourceBundle (String aBundleName , Locale locale ) {
382+ public ResourceBundle findResourceBundle (String bundleName , Locale locale ) {
440383 ClassLoader classLoader = getCurrentThreadContextClassLoader ();
441- String key = createMissesKey (String .valueOf (classLoader .hashCode ()), aBundleName , locale );
384+ String key = createMissesKey (String .valueOf (classLoader .hashCode ()), bundleName , locale );
442385
443386 if (missingBundles .contains (key )) {
444387 return null ;
@@ -449,7 +392,7 @@ public ResourceBundle findResourceBundle(String aBundleName, Locale locale) {
449392 if (bundlesMap .containsKey (key )) {
450393 bundle = bundlesMap .get (key );
451394 } else {
452- bundle = ResourceBundle .getBundle (aBundleName , locale , classLoader );
395+ bundle = ResourceBundle .getBundle (bundleName , locale , classLoader );
453396 bundlesMap .putIfAbsent (key , bundle );
454397 }
455398 } catch (MissingResourceException ex ) {
@@ -458,15 +401,15 @@ public ResourceBundle findResourceBundle(String aBundleName, Locale locale) {
458401 if (bundlesMap .containsKey (key )) {
459402 bundle = bundlesMap .get (key );
460403 } else {
461- bundle = ResourceBundle .getBundle (aBundleName , locale , delegatedClassLoaderMap .get (classLoader .hashCode ()));
404+ bundle = ResourceBundle .getBundle (bundleName , locale , delegatedClassLoaderMap .get (classLoader .hashCode ()));
462405 bundlesMap .putIfAbsent (key , bundle );
463406 }
464407 } catch (MissingResourceException e ) {
465- LOG .debug ("Missing resource bundle [{}]!" , aBundleName , e );
408+ LOG .debug ("Missing resource bundle [{}]!" , bundleName , e );
466409 missingBundles .add (key );
467410 }
468411 } else {
469- LOG .debug ("Missing resource bundle [{}]!" , aBundleName );
412+ LOG .debug ("Missing resource bundle [{}]!" , bundleName );
470413 missingBundles .add (key );
471414 }
472415 }
@@ -656,6 +599,36 @@ protected String findMessage(Class<?> clazz, String key, String indexedKey, Loca
656599 return null ;
657600 }
658601
602+ protected String extractIndexedName (String textKey ) {
603+ String indexedTextName = null ;
604+ // calculate indexedTextName (collection[*]) if applicable
605+ if (textKey .contains ("[" )) {
606+ int i = -1 ;
607+
608+ indexedTextName = textKey ;
609+
610+ while ((i = indexedTextName .indexOf ('[' , i + 1 )) != -1 ) {
611+ int j = indexedTextName .indexOf (']' , i );
612+ String a = indexedTextName .substring (0 , i );
613+ String b = indexedTextName .substring (j );
614+ indexedTextName = a + "[*" + b ;
615+ }
616+ }
617+ return indexedTextName ;
618+ }
619+
620+ protected void logMissingText (Class <?> startClazz , String textKey , Locale locale , GetDefaultMessageReturnArg result , String indexedTextName ) {
621+ // could we find the text, if not log a WARN
622+ if (unableToFindTextForKey (result ) && LOG .isDebugEnabled ()) {
623+ String warn = "Unable to find text for key '" + textKey + "' " ;
624+ if (indexedTextName != null ) {
625+ warn += " or indexed key '" + indexedTextName + "' " ;
626+ }
627+ warn += "in class '" + startClazz .getName () + "' and locale '" + locale + "'" ;
628+ LOG .debug (warn );
629+ }
630+ }
631+
659632 static class MessageFormatKey {
660633 String pattern ;
661634 Locale locale ;
0 commit comments