⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i18nbundleimpl.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
            iconfile = lookup( key );
        }
        catch ( MissingResourceException ex )
        {
            if ( m_parentBundle != null )
                return m_parentBundle.getIcon( key );
            throw ex;
        }
        try
        {
            ImageIcon icon = (ImageIcon) iconCache.get( iconfile );
            if ( icon == null )
            {
                icon = new ImageIcon( loadResource( iconfile ), key );
                iconCache.put( iconfile, icon );
            } // end of if ()
            return icon;
        }
        catch ( Exception ex )
        {
            String message = "Icon " + iconfile + " can't be created: " + ex.getMessage();
            getLogger().error( message );
            throw new MissingResourceException( message, className, key );
        }
    }

    public Locale getLocale()
    {
        if ( locale == null )
            throw new IllegalStateException( "Call setLocale first!" );
        return locale;
    }

    public String getLang()
    {
        if ( locale == null )
            throw new IllegalStateException( "Call setLocale first!" );
        return locale.getLanguage();
    }

    /** this method imitates the orginal
     * <code>ResourceBundle.getBundle(String className,Locale
     * locale)</code> which causes problems when the locale is changed
     * to the base locale (english). For a full description see
     * ResourceBundle.getBundle(String className) in the java-api.*/
    protected ResourceBundle loadResourceBundle( String className, Locale locale )
    {
        String tries[] = new String[7];
        StringBuffer buf = new StringBuffer();
        tries[6] = className;
        buf.append( className );
        if ( locale.getLanguage().length() > 0 )
        {
            buf.append( '_' );
            buf.append( locale.getLanguage() );
            tries[2] = buf.toString();
        }
        if ( locale.getCountry().length() > 0 )
        {
            buf.append( '_' );
            buf.append( locale.getCountry() );
            tries[1] = buf.toString();
        }
        if ( locale.getVariant().length() > 0 )
        {
            buf.append( '_' );
            buf.append( locale.getVariant() );
            tries[0] = buf.toString();
        }
        buf.delete( className.length(), buf.length() - 1 );
        Locale defaultLocale = Locale.getDefault();
        if ( defaultLocale.getLanguage().length() > 0 )
        {
            buf.append( defaultLocale.getLanguage() );
            tries[5] = buf.toString();
        }
        if ( defaultLocale.getCountry().length() > 0 )
        {
            buf.append( '_' );
            buf.append( defaultLocale.getCountry() );
            tries[4] = buf.toString();
        }
        if ( defaultLocale.getVariant().length() > 0 )
        {
            buf.append( '_' );
            buf.append( defaultLocale.getVariant() );
            tries[3] = buf.toString();
        }

        ResourceBundle bundle = null;
        for ( int i = 0; i < tries.length; i++ )
        {
            if ( tries[i] == null )
                continue;
            bundle = loadBundle( tries[i] );
            if ( bundle != null )
            {
                loadParent( tries, i, bundle );
                return bundle;
            }
        }
        throw new MissingResourceException( "'" + className + "' not found. The resource-file is missing.", className,
                                            "" );
    }

    private String getPropertyFileNameFromClassName( String classname )
    {
        StringBuffer result = new StringBuffer( classname );
        for ( int i = 0; i < result.length(); i++ )
        {
            if ( result.charAt( i ) == '.' )
                result.setCharAt( i, '/' );
        }
        result.insert( 0, '/' );
        result.append( ".properties" );
        return result.toString();
    }

    private ResourceBundle loadBundle( String name )
    {
        try
        {
            getLogger().debug( "Trying to load bundle " + name );
            String pathName = getPropertyFileNameFromClassName( name );
            InputStream io = this.getClass().getResourceAsStream( pathName );
            if ( io != null )
            {
                return new PropertyResourceBundle( io );
            }
            ResourceBundle bundle = (ResourceBundle) Class.forName( name ).newInstance();
            getLogger().debug( "Bundle found " + name );
            return bundle;
        }
        catch ( Exception ex )
        {
            return null;
        }
        catch ( ClassFormatError ex )
        {
            return null;
        }
    }

    private void loadParent( String[] tries, int i, ResourceBundle bundle )
    {
        ResourceBundle parent = null;
        if ( i == 0 || i == 3 )
        {
            parent = loadBundle( tries[i++] );
            if ( parent != null )
                setParent( bundle, parent );
            bundle = parent;
        }
        if ( i == 1 || i == 4 )
        {
            parent = loadBundle( tries[i++] );
            if ( parent != null )
                setParent( bundle, parent );
            bundle = parent;
        }
        if ( i == 2 || i == 5 )
        {
            parent = loadBundle( tries[6] );
            if ( parent != null )
                setParent( bundle, parent );
        }
    }

    private void setParent( ResourceBundle bundle, ResourceBundle parent )
    {
        try
        {
            Method method = bundle.getClass().getMethod( "setParent", new Class[]
                { ResourceBundle.class } );
            method.invoke( bundle, new Object[]
                { parent } );
        }
        catch ( Exception ex )
        {
        }
    }

    private String lookup( String key ) throws MissingResourceException
    {
        if ( dict == null )
        {
            return resourceBundle.getString( key );
        }
        else
        {
            String result = dict.lookup( key, getLang() );
            if ( result != null )
                return result;
            String message = "Can't find resourcestring " + key + " in class " + className;
            throw new MissingResourceException( message, className, key );
        } // end of else
    }

    public String getString( String key ) throws MissingResourceException
    {
        String result = (String) stringCache.get( key );
        if ( result != null )
            return result;
        result = getUncachedString( key );
        stringCache.put( key, result );
        return result;
    }

    private String getUncachedString( String key ) throws MissingResourceException
    {
        String result;
        try
        {
            result = lookup( key );
        }
        catch ( MissingResourceException ex )
        {
            if ( m_parentBundle != null )
                return m_parentBundle.getString( key );
            throw ex;
        }

        if ( getLogger() != null && getLogger().isDebugEnabled() )
            getLogger().debug( "string requested: " + result );

        return filterXHTML( result );
    }

    public I18nBundle getParentBundle()
    {
        return m_parentBundle;
    }

    /* replaces XHTML with HTML because swing can't display proper XHTML*/
    String filterXHTML( String text )
    {
        if ( text.indexOf( "<br/>" ) >= 0 )
        {
            return applyXHTMLFilter( text );
        }
        else
        {
            return text;
        } // end of else
    }

    public static String replaceAll( String text, String token, String with )
    {
        StringBuffer buf = new StringBuffer();
        int i = 0;
        int lastpos = 0;
        while ( ( i = text.indexOf( token, lastpos ) ) >= 0 )
        {
            if ( i > 0 )
                buf.append( text.substring( lastpos, i ) );
            buf.append( with );
            i = ( lastpos = i + token.length() );
        } // end of if ()
        buf.append( text.substring( lastpos, text.length() ) );
        return buf.toString();
    }

    private String applyXHTMLFilter( String text )
    {
        return replaceAll( text, "<br/>", "<br></br>" );
    }

    public void setLocale( Locale locale )
    {
        this.locale = locale;
        stringCache.clear();
        iconCache.clear();
        getLogger().debug( "Locale changed to " + locale );
        if ( dict == null )
        {
            resourceBundle = loadResourceBundle( className, locale );
            //resourceBundle = ResourceBundle.getBundle(className, locale);
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -