1 16 17 package org.springframework.core.io.support; 18 19 import java.util.Locale ; 20 21 import org.springframework.core.io.DefaultResourceLoader; 22 import org.springframework.core.io.Resource; 23 import org.springframework.core.io.ResourceLoader; 24 import org.springframework.util.Assert; 25 26 33 public class LocalizedResourceHelper { 34 35 36 public static final String DEFAULT_SEPARATOR = "_"; 37 38 39 private final ResourceLoader resourceLoader; 40 41 private String separator = DEFAULT_SEPARATOR; 42 43 44 48 public LocalizedResourceHelper() { 49 this.resourceLoader = new DefaultResourceLoader(); 50 } 51 52 56 public LocalizedResourceHelper(ResourceLoader resourceLoader) { 57 Assert.notNull(resourceLoader, "ResourceLoader must not be null"); 58 this.resourceLoader = resourceLoader; 59 } 60 61 65 public void setSeparator(String separator) { 66 this.separator = (separator != null ? separator : DEFAULT_SEPARATOR); 67 } 68 69 70 89 public Resource findLocalizedResource(String name, String extension, Locale locale) { 90 Assert.notNull(name, "Name must not be null"); 91 Assert.notNull(extension, "Extension must not be null"); 92 93 Resource resource = null; 94 95 if (locale != null) { 96 String lang = locale.getLanguage(); 97 String country = locale.getCountry(); 98 String variant = locale.getVariant(); 99 100 if (variant.length() > 0) { 102 String location = 103 name + this.separator + lang + this.separator + country + this.separator + variant + extension; 104 resource = this.resourceLoader.getResource(location); 105 } 106 107 if ((resource == null || !resource.exists()) && country.length() > 0) { 109 String location = name + this.separator + lang + this.separator + country + extension; 110 resource = this.resourceLoader.getResource(location); 111 } 112 113 if ((resource == null || !resource.exists()) && lang.length() > 0) { 115 String location = name + this.separator + lang + extension; 116 resource = this.resourceLoader.getResource(location); 117 } 118 } 119 120 if (resource == null || !resource.exists()) { 122 String location = name + extension; 123 resource = this.resourceLoader.getResource(location); 124 } 125 126 return resource; 127 } 128 129 } 130 | Popular Tags |