<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DALLASCAO.COM &#187; java</title>
	<atom:link href="http://dallascao.com/cn/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://dallascao.com/cn</link>
	<description>英汉翻译 Dallas Cao 的网站</description>
	<lastBuildDate>Mon, 23 Jan 2012 11:42:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>环境感知的语言版本链接</title>
		<link>http://dallascao.com/cn/the-context-sensitive-language-versions-link/</link>
		<comments>http://dallascao.com/cn/the-context-sensitive-language-versions-link/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 16:30:04 +0000</pubDate>
		<dc:creator>Dallas Cao</dc:creator>
				<category><![CDATA[代码狂]]></category>
		<category><![CDATA[全球互联网]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[代码]]></category>
		<category><![CDATA[字符串替换]]></category>
		<category><![CDATA[网站管理]]></category>

		<guid isPermaLink="false">http://www.dallascao.com/cn/?p=123</guid>
		<description><![CDATA[大多数双语网站上都有一个固定的指向另一语言版本的链接。不管你在网站一个语言版本的什么位置，点击语言版本链接，打开的总是另一语言版本的首页。而我希望我的访问者打开的是他正在查看的页面的另一语言版本。 我使用 javawscript 的 replace 方法实现了这个功能。[......]<p class='read-more'><a href='http://dallascao.com/cn/the-context-sensitive-language-versions-link/'></a></p>]]></description>
			<content:encoded><![CDATA[<p>大多数双语网站上都有一个固定的指向另一语言版本的链接。不管你在网站一个语言版本的什么位置，点击语言版本链接，打开的总是另一语言版本的首页。而我希望我的访问者打开的是他正在查看的页面的另一语言版本。<br />
<span id="more-123"></span><br />
例如：如果用户访问了：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/cn/the-context-sensitive-language-versions-link/</pre></div></div>

<p>点击底部的 “English” 链接，用户打开的是：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://dallascao.com/en/the-context-sensitive-language-versions-link/</pre></div></div>

<p>也就是这里涉及到是将当前页面 URL 中的 “/cn/” 换成 “/en/”。</p>
<p>以下 php 代码无法给出正确的结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url_old</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url_new</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cn&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$url_old</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>   
&lt;a href=<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url_new</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>English&lt;/a&gt;</pre></div></div>

<p>不管您在访问哪个页面， $url_old 返回的总是首页链接 “/en/index.php”。这是因为我使用了“固定链接”功能，URL 被重写了。在服务器端，URL 重写发生于 PHP 代码执行之后，因此 php 字符串 “$url_old” 无法得到后来被重写的实际 URL。以下的 javascript 代码就可以，这是因为java script 是在客户端运行的，可以准确得到客户端使用的 URL:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
var s<span style="color: #339933;">=</span>window.<span style="color: #006633;">location</span>.<span style="color: #006633;">href</span>
r<span style="color: #339933;">=</span>s.<span style="color: #006633;">replace</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">/</span>cn<span style="color: #339933;">/</span>, <span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
document.<span style="color: #006633;">write</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a title='English verion of this page' href=&quot;</span><span style="color: #339933;">+</span>r<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&gt;English&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>更多信息：</p>
<p><a target=_blank href="http://www.w3schools.com/jsref/jsref_replace.asp">java replace () 方法</a></p>
<div id="crp_related"><h3>相关文章：</h3><ul><li><a href="http://dallascao.com/cn/use-cookies-to-remember/" rel="bookmark" class="crp_title">使用 cookies 记住用户上次访问的语言版本（php 代码）</a></li><li><a href="http://dallascao.com/cn/use-of-if-fields/" rel="bookmark" class="crp_title">使用 Miscrosoft Word 的 If 域</a></li><li><a href="http://dallascao.com/cn/use-regular-expressions-in-htaccess-for-redirections/" rel="bookmark" class="crp_title">在 .htaccess 中使用正则表达式来定义转向</a></li><li><a href="http://dallascao.com/cn/chinese-friendfeedcom-receives-favorable-feedbacks-from-fans/" rel="bookmark" class="crp_title">中文版 friendfeed.com 受到爱好者的好评</a></li><li><a href="http://dallascao.com/cn/google-dictionary-for-word-gd4word/" rel="bookmark" class="crp_title">Google Dictionary for Word</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://dallascao.com/cn/the-context-sensitive-language-versions-link/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk

Served from: dallascao.com @ 2012-02-05 15:33:19 -->
