Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Complete Reuse</strong></p> <p>To reuse the JDK logic, you could create a property file within the classpath for each of the known locales (such as test_fr_CA.properties, test_fr.properties, test_en_US.properties, test_en.properties, test.properties). Don't forget the root locale (test.properties) if you want to be able to match to it. Then simply create a resource bundle for the locale in question and inspect it to see the actual locale used.</p> <pre><code>ResourceBundle rb = ResourceBundle.getBundle("test", Locale.FRENCH); System.out.println("Locale used is:"+rb.getLocale().toString()); </code></pre> <p>The files can be created dynamically and cleaned up after the test.</p> <p><strong>High Level Code Replication, Low Level Reuse</strong></p> <p>You could replicate the high level code in <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ResourceBundle.java#ResourceBundle.getBundleImpl%28java.lang.String%2Cjava.util.Locale%2Cjava.lang.ClassLoader%2Cjava.util.ResourceBundle.Control%29" rel="nofollow">java.util.ResourceBundle.getBundleImpl(...)</a>. This is basically going through looking for a match (using your own matching logic like equal toString() representations) in a candidate list of locales reusing <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ResourceBundle.java#ResourceBundle.Control.getCandidateLocales%28java.lang.String,java.util.Locale%29" rel="nofollow">java.util.ResourceBundle.Control.getCandidateLocales(...)</a> on the locale in question. If there is no match you get the next fallback locale for the locale in question by reusing <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ResourceBundle.java#ResourceBundle.Control.getFallbackLocale%28java.lang.String,java.util.Locale%29" rel="nofollow">java.util.ResourceBundle.Control.getFallbackLocale(...)</a> For each fallback locale you try to match a locale in it's candidate list repeating the fallback in a loop until there are no fallback locales left. Note that the root locale will be the last candidate in each candidate list but it should be skipped unless you have exhausted all fallback locales.</p> <p>This method does not require creating files. You use a non-existent baseName in the getCandidateLocales(...) and get FallbackLocale(...) calls and compare each candidate locale to your list of known locales looking for a match.</p> <p>A simple example of this would be like the following:</p> <pre><code> ResourceBundle.Control rbControl = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES); Locale localeInQuestion = Locale.CHINA; List&lt;Locale&gt; knownLocales = Arrays.asList(new Locale[] {Locale.CANADA_FRENCH, Locale.FRENCH, Locale.US, Locale.UK, Locale.ENGLISH, Locale.ROOT}); String nonExistentBaseName = "bogus"; Locale matchingLocale = null; Boolean reachedRootLocaleMatch = false; outerloop: for (Locale targetLocale = localeInQuestion; targetLocale != null; targetLocale = rbControl.getFallbackLocale(nonExistentBaseName, targetLocale)) { List&lt;Locale&gt; candidateLocales = rbControl.getCandidateLocales(nonExistentBaseName, targetLocale); for (Iterator iterator = candidateLocales.iterator(); iterator.hasNext();) { Locale currentCandidateLocale = (Locale) iterator.next(); if (knownLocales.contains(currentCandidateLocale)) { if (currentCandidateLocale.equals(Locale.ROOT)) { reachedRootLocaleMatch = true; } else { matchingLocale = currentCandidateLocale; break outerloop; } } } } if (matchingLocale == null &amp;&amp; reachedRootLocaleMatch) { matchingLocale = Locale.ROOT; } if (matchingLocale != null) { System.out.println("The matching locale is: "+matchingLocale.toString()); } else { System.out.println("There was no matching locale"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload