eclipse插件(RCP)字符串常量可配置化(国际化)

软件开发中经常有字符串不想硬编码,解决方案因语言和框架而异,但往往都含有“异曲同工之妙”。Eclipse插件(RCP)的解决方案:NLS

NLS

NLS 是Eclipse 3.1 开始提供的一个类,由IBM提供,其信息如下

/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.util;

import ……
/**
 * Common superclass for all message bundle classes.  Provides convenience
 * methods for manipulating messages.
 * <p>
 * The <code>#bind</code> methods perform string substitution and should be considered a
 * convenience and <em>not</em> a full substitute replacement for <code>MessageFormat#format</code>
 * method calls.
 * </p>
 * <p>
 * Text appearing within curly braces in the given message, will be interpreted
 * as a numeric index to the corresponding substitution object in the given array. Calling
 * the <code>#bind</code> methods with text that does not map to an integer will result in an
 * {@link IllegalArgumentException}.
 * </p>
 * <p>
 * Text appearing within single quotes is treated as a literal. A single quote is escaped by
 * a preceeding single quote.
 * </p>
 * <p>
 * Clients who wish to use the full substitution power of the <code>MessageFormat</code> class should
 * call that class directly and not use these <code>#bind</code> methods.
 * </p>
 * <p>
 * Clients may subclass this type.
 * </p>
 *
 * @since 3.1
 */
public abstract class NLS {

	private static final Object[] EMPTY_ARGS = new Object[0];
	private static final String EXTENSION = ".properties"; //$NON-NLS-1$
	private static String[] nlSuffixes;
	private static final String PROP_WARNINGS = "osgi.nls.warnings"; //$NON-NLS-1$
	private static final String IGNORE = "ignore"; //$NON-NLS-1$
	private static final boolean ignoreWarnings = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
		@Override
		public Boolean run() {
			return IGNORE.equals(System.getProperty(PROP_WARNINGS));
		}
	});

	/*
	 * NOTE do not change the name of this field; it is set by the Framework using reflection
	 */
	private static FrameworkLog frameworkLog;

	static final int SEVERITY_ERROR = 0x04;
	static final int SEVERITY_WARNING = 0x02;
	/*
	 * This object is assigned to the value of a field map to indicate
	 * that a translated message has already been assigned to that field.
	 */
	static final Object ASSIGNED = new Object();

	/**
	 * Creates a new NLS instance.
	 */
	protected NLS() {
		super();
	}
	…… 
}

使用

  • 创建XXX.properties配置文件,用于定义那些不想硬编码的字符串,类propertis文件。
User_name=张三
  • 创建XXX.java java类,用于将XXX.properties中配置的属性映射到java类,方便编码时使用。
public class UserConfig extends NLS{
	// 指定要映射的 properties 文件的路径,后缀省略
	public static final String BUNDLE_NAME = "com.abc.def.XXX";

	// 要映射的字符串,名称需要与 .propertis 中的保持一致。
	public static String User_name;
}

注意:该类必须继承NLS ,且在里边指定要解析的.properties文件的位置,其固定语法为public static final String BUNDLE_NAME = "com.abc.def.XXX";可以看出,类名和配置文件名可以不一样。

示例

这里展示一下官方在CNF(Common Navigator Framework)源码中的一个使用案例。
在这里插入图片描述
使用时直接
WorkbenchNavigatorMessages.PortingActionProvider_ImportResourcesMenu_label即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值