软件开发中经常有字符串不想硬编码,解决方案因语言和框架而异,但往往都含有“异曲同工之妙”。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
即可。