TranslationBundle extends ListResourceBundle { static Map<String,String> loadContent( String lang ) { Map<String,String> result = new LinkedHashMap<> (); try { InputStream in = TranslationBundle.class.getResourceAsStream( "/" + lang + ".json" ); ByteArrayOutputStream os = new ByteArrayOutputStream(); Utils.copyStreamSafely( in, os ); String content = os.toString( "UTF-8" ); Matcher m = Pattern.compile( ROW_PATTERN ).matcher( content ); while( m.find()) { String s = m.group( 2 ); try { ErrorCode ec = ErrorCode.valueOf( m.group( 1 )); for( int i=0; i<ec.getI18nProperties().length; i++ ) { s = s.replace( "{" + i + "}", ec.getI18nProperties()[ i ]); } } catch( IllegalArgumentException e ) { } result.put( m.group( 1 ), s ); } } catch( Exception e ) { final Logger logger = Logger.getLogger( TranslationBundle.class.getName()); Utils.logException( logger, e ); } return result; } TranslationBundle( String lang ); String getLang(); static String resolve( String lang ); static final String DETAILS_SEPARATOR; static final String EN; static final String FR; }
@Test public void testLoadContent_invalidLang() { Map<String,String> map = TranslationBundle.loadContent( "fish" ); Assert.assertEquals( 0, map.size()); }