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

📄 messagetext.java

📁 基于JXTA开发平台的下载软件开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 24.07.2003
 * Copyright (C) 2003, 2004, 2005, 2006 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 */
package org.gudy.azureus2.core3.internat;

import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.util.Debug;
import org.gudy.azureus2.core3.util.FileUtil;
import org.gudy.azureus2.core3.util.SystemProperties;
import org.gudy.azureus2.core3.util.Constants;

import java.io.File;
import java.io.FilenameFilter;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author Arbeiten
 * 
 * @author CrazyAlchemist Added keyExistsForDefaultLocale
 */
public class MessageText {

  public static final Locale LOCALE_ENGLISH = new Locale("en", "");
  public static final Locale LOCALE_DEFAULT = new Locale("", ""); // == english
  private static Locale LOCALE_CURRENT = LOCALE_DEFAULT;
  private static final String BUNDLE_NAME = "org.gudy.azureus2.internat.MessagesBundle"; //$NON-NLS-1$
  private static Map pluginLocalizationPaths = new HashMap();
  private static ResourceBundle RESOURCE_BUNDLE;
  private static Set			platform_specific_keys	= new HashSet();

  private static int bundle_fail_count	= 0;
  
  static{
	  setResourceBundle( getResourceBundle(BUNDLE_NAME, LOCALE_DEFAULT, MessageText.class.getClassLoader()));
  }
  
  static ResourceBundle
  getResourceBundle(
	String		name,
	Locale		loc,
	ClassLoader	cl )
  {
	  try{
		  return( ResourceBundle.getBundle(name, loc, cl ));
		  
	  }catch( Throwable e ){
		  
		  bundle_fail_count++;
			  
		  if ( bundle_fail_count == 1 ){
			  
			  e.printStackTrace();
			  
			  Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
						"Failed to load resource bundle. One possible cause is "
								+ "that you have installed Azureus into a directory "
								+ "with a '!' in it. If so, please remove the '!'."));
		  }
		  
		  return(
			  new ResourceBundle()
			  {
				  public Locale
				  getLocale()
				  {
					return( LOCALE_DEFAULT );
				  }

				  protected Object 
				  handleGetObject(String key)
				  {
						return( null );
				  }
	
				  public Enumeration
				  getKeys()
				  {
					return( new Vector().elements());
				  }
			  });
	  } 
  }
  
  private static ResourceBundle DEFAULT_BUNDLE = RESOURCE_BUNDLE;
  
  private static void
  setResourceBundle(
	  ResourceBundle	bundle )
  {
	  RESOURCE_BUNDLE	= bundle;
	  
	  Enumeration	keys = RESOURCE_BUNDLE.getKeys();
	  
	  String	platform_suffix = getPlatformSuffix();
	  
	  platform_specific_keys.clear();
	  
	  while( keys.hasMoreElements()){
		  
		  String	key = (String)keys.nextElement();
		  
		  if ( key.endsWith( platform_suffix )){
			  			  
			  platform_specific_keys.add( key );
		  }
	  }
  }
  
 
  public static boolean keyExists(String key) {
    try {
      RESOURCE_BUNDLE.getString(key);
      return true;
    } catch (MissingResourceException e) {
      return false;
    }
  }

    public static boolean keyExistsForDefaultLocale(final String key) {
        try {
            DEFAULT_BUNDLE.getString(key);
            return true;
        } catch (MissingResourceException e) {
            return false;
        }
    }


  /**
   * @param key
   * @return
   */
  public static String 
  getString(
	String key, 
	String sDefault) 
  {
  	if (key == null)
  		return "";

	  String	target_key = key + getPlatformSuffix();
	  
	  if ( !platform_specific_keys.contains( target_key )){
		  
		  target_key	= key;
	  }
    
	  try {
      
		  return RESOURCE_BUNDLE.getString( target_key );
    
	  }catch (MissingResourceException e) {
		  
		  return getPlatformNeutralString(key, sDefault);
	  }
  }

  public static String 
  getString(
	String key) 
  {
  	if (key == null)
  		return "";

	  String	target_key = key + getPlatformSuffix();
	  
	  if ( !platform_specific_keys.contains( target_key )){
		  
		  target_key	= key;
	  }

	  try {
	 
		  return RESOURCE_BUNDLE.getString( target_key );
		  
	  } catch (MissingResourceException e) {
		  
	      return getPlatformNeutralString(key);
	  }
  }

  public static String getPlatformNeutralString(String key) {
    try {
      return RESOURCE_BUNDLE.getString(key);
    } catch (MissingResourceException e) {
      return '!' + key + '!';
    }
  }

  public static String getPlatformNeutralString(String key, String sDefault) {
    try {
      return RESOURCE_BUNDLE.getString(key);
    } catch (MissingResourceException e) {
      return sDefault;
    }
  }

  /**
   * Gets the localization key suffix for the running platform
   * @return The suffix
   * @see Constants
   */
  private static String getPlatformSuffix() {
    if(Constants.isOSX)
        return "._mac";
    else if(Constants.isLinux)
        return "._linux";
    else if(Constants.isSolaris)
        return "._solaris";
     else if(Constants.isWindows)
       return "._windows";
     else
       return "._unknown";
  }

  /**
   * Process a sequence of words, and translate the ones containing at least one '.', unless it's an ending dot.
   * @param sentence 
   * @return the formated String in the current Locale
   */
  public static String getStringForSentence(String sentence) {
    StringTokenizer st = new StringTokenizer(sentence , " ");
    StringBuffer result = new StringBuffer(sentence.length());
    String separator = "";
    while(st.hasMoreTokens())
    {
      result.append(separator);
      separator = " ";
      
      String word = st.nextToken();
      int length = word.length();
      int position = word.lastIndexOf(".");
      if(position == -1 || (position+1) == length) {
        result.append(word);
      } else {
        //We have a key :
        String translated = getString(word);
        if(translated.equals("!" + word + "!")) {
          result.append(word);
        }
        else {
          result.append(translated);
        }
      }         
    }    
    return result.toString();
  }

  /**
   * Expands a message text and replaces occurrences of %1 with first param, %2 with second...
   * @param key
   * @param params
   * @return
   */
  public static String 
  getString(
  		String		key,
		String[]	params )
  {
  	String	res = getString(key);
  	
  	for(int i=0;i<params.length;i++){
  		
  		String	from_str 	= "%" + (i+1);
  		String	to_str		= params[i];
  		
  		res = replaceStrings( res, from_str, to_str );
  	}
  	
  	return( res );
  }
  
  protected static String
  replaceStrings(
  	String	str,
	String	f_s,
	String	t_s )
  {
  	int	pos = 0;
  	

⌨️ 快捷键说明

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