SC@SDUSC
matching:
该包中只有一个LocaleMatcher类,它是一个示例配置,此配置显示默认值
<map:matcher name="i18n" src="org.apache.cocoon.matching.LocaleMatcher">
<locale-attribute>locale</locale-attribute>
<negotiate>false</negotiate>
<use-locale>true</use-locale>
<use-locales>false</use-locales>
<use-blank-locale>true</use-blank-locale>
<default-locale language="en" country="US"/>
<store-in-request>false<store-in-request>
<create-session>false<create-session>
<store-in-session>false<store-in-session>
<store-in-cookie>false<store-in-cookie>
</map:matcher>
- locale-attribute指定要用作语言环境的请求参数/会话属性/cookie 的名称(默认为
locale
) - negotiate指定匹配器是否应检查资源是否存在。如果设置为 true,匹配器将查找语言环境,直到找到匹配的资源。如果即使使用默认语言环境或空白语言环境也找不到资源,匹配器将不匹配。
- use-locale指定是否使用用户代理提供的主要语言环境(或服务器默认设置,不是代理传递的语言环境)
- use-locales指定是否应该依次测试用户代理提供的每个区域设置(仅在
negotiate
设置为时才有意义true
) - default-locale指定当没有匹配任何先前的语言环境时要使用的默认语言环境。
- use-blank-locale指定是否应该在其文件名或文件路径中没有区域设置的情况下查找文件(例如,在查找 index.en.html 之后,尝试 index.html)如果没有匹配任何先前的区域设置。
- store-in-request指定是否应将找到的语言环境存储为请求属性。
- create-session指定在将找到的语言环境存储为会话属性时是否应创建会话。
- store-in-session指定是否应将找到的语言环境存储为会话属性。
- store-in-cookie指定是否应将找到的语言环境存储为 cookie。
区域标识,语言环境将按以下顺序进行测试:
- 区域设置作为请求参数提供
- 区域设置作为会话属性提供
- 区域设置作为 cookie 提供
- 使用站点地图参数提供的区域设置
(<map:match> 节点中的 <map:parameter name="locale" value="{1}"/> 样式参数) - 用户代理提供的区域设置,或服务器默认设置,如果
use-locale
设置为true
- 用户代理提供的区域设置,如果
use-locales
设置为true
. - 默认语言环境,如果在匹配器的配置中指定
- 没有定义语言环境的资源(空白语言环境)
找到匹配的语言环境后,匹配器中包含的站点地图元素将可以使用以下站点地图变量:
- {source}:匹配的源的 URI
- {locale}:与该资源匹配的语言环境
- {matched-locale}:匹配资源的语言环境部分
- {language}:匹配资源的语言
- {country}:匹配资源的国家
- {variant}:匹配资源的变体
在该类中,提供了match方法,首先,判断资源是否有效,在该方法中调用了I18nUtils的findLocale和storeLocale方法,返回map。
isValidResource方法,可以判断资源是否有效,并对该方法进行了重载,根据locale变量是否含有变体,国家,语言来选择语言国家变体环境进行检测。
private boolean isValidResource(String pattern, Locale locale, Locale testLocale, Map map) {
String url;
String testLocaleStr = testLocale.toString();
if ("".equals(testLocaleStr)) {
int starPos = pattern.indexOf("*");
if (starPos < pattern.length() - 1 && starPos > 1 &&
pattern.charAt(starPos - 1) == pattern.charAt(starPos + 1)) {
url = pattern.substring(0, starPos - 1) + pattern.substring(starPos + 1);
} else {
url = StringUtils.replace(pattern, "*", "");
}
} else {
url = StringUtils.replace(pattern, "*", testLocaleStr);
}
boolean result = true;
if (testResourceExists) {
Source source = null;
try {
source = resolver.resolveURI(url);
result = source.exists();
} catch (IOException e) {
result = false;
} finally {
if (source != null) {
resolver.release(source);
}
}
}
if (result) {
map.put("source", url);
map.put("matched-locale", testLocaleStr);
if (locale != null) {
map.put("locale", locale.toString());
map.put("language", locale.getLanguage());
map.put("country", locale.getCountry());
map.put("variant", locale.getVariant());
}
}
return result;
}
首先判断在*之前之后是否有相同字符,如果有,则删除至只剩一个。如果检测资源存在,则保存URL添加到Map中,最终释放来源。