﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-小明思考-随笔分类-G11N/ICU</title><link>http://www.cppblog.com/sandy/category/1985.html</link><description>高性能服务器端计算</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 12:43:30 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 12:43:30 GMT</pubDate><ttl>60</ttl><item><title>C++源文件编码问题</title><link>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Thu, 05 Apr 2007 07:30:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/21320.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2007/04/05/21320.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/21320.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/21320.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: C++源文件编码问题&nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2007/04/05/21320.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/21320.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2007-04-05 15:30 <a href="http://www.cppblog.com/sandy/archive/2007/04/05/21320.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[Tips]如何计算utf8下的字节数</title><link>http://www.cppblog.com/sandy/archive/2006/11/08/14841.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 08 Nov 2006 05:32:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/11/08/14841.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/14841.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/11/08/14841.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/14841.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/14841.html</trackback:ping><description><![CDATA[项目需要，写了一个帮助L10N的工程师计算utf8的小工具(html page)。如下<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">&lt;!</span><span style="COLOR: #ff00ff">DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">HTML</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">HEAD</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">TITLE</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"> An utf8 count tool  </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">TITLE</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">meta </span><span style="COLOR: #ff0000">http-equiv</span><span style="COLOR: #0000ff">="Content-Type"</span><span style="COLOR: #ff0000"> content</span><span style="COLOR: #0000ff">="text/html; charset=utf16"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">script</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><br />    </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">function</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> utf16to8(str) {<br />        </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">var</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> out, i, len, c;<br /><br />        out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">""</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">;<br />        len </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> str.length;<br />        </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">for</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(i </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">; i </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&lt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> len; i</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">++</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) {<br />        c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> str.charCodeAt(i);<br />        </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">if</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x0001</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> (c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&lt;=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x007F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">)) {<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> str.charAt(i);<br />        } </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">else</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">if</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> (c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x07FF</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) {<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> String.fromCharCode(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0xE0</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">|</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">12</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x0F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">));<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> String.fromCharCode(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x80</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">|</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">  </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">6</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x3F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">));<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> String.fromCharCode(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x80</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">|</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">  </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x3F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">));<br />        } </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">else</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> {<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> String.fromCharCode(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0xC0</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">|</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">  </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">6</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x1F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">));<br />            out </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">+=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> String.fromCharCode(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x80</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">|</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> ((c </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&gt;&gt;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">  </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">) </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">&amp;</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0x3F</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">));<br />        }<br />        }<br />        </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">return</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> out;<br />    }<br />    </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">function</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> count()<br />    {<br />        </span><span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">var</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> temp </span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> f1.value;<br />        temp</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">temp.replace(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">\\r</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">g,</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">\r</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);<br />        temp</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">temp.replace(</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">\\n</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">g,</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">\n</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">);<br />        result.innerHTML</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span><span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">utf16to8(temp).length;<br />    }<br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">script</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">HEAD</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">BODY</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">TEXTAREA </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="f1"</span><span style="COLOR: #ff0000"> NAME</span><span style="COLOR: #0000ff">="f1"</span><span style="COLOR: #ff0000"> ROWS</span><span style="COLOR: #0000ff">="10"</span><span style="COLOR: #ff0000"> COLS</span><span style="COLOR: #0000ff">="50"</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">TEXTAREA</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">br</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">INPUT </span><span style="COLOR: #ff0000">TYPE</span><span style="COLOR: #0000ff">="button"</span><span style="COLOR: #ff0000"> value</span><span style="COLOR: #0000ff">="count length"</span><span style="COLOR: #ff0000"> onclick</span><span style="COLOR: #0000ff">="count()"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">div </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="result"</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">div</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">BODY</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">HTML</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /></span></div><img src ="http://www.cppblog.com/sandy/aggbug/14841.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-11-08 13:32 <a href="http://www.cppblog.com/sandy/archive/2006/11/08/14841.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>G11N Developement (2) --- Linux Best Practice</title><link>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 26 Jun 2006 08:57:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/9020.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/26/9020.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/9020.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/9020.html</trackback:ping><description><![CDATA[在linux/Unix平台上面做G11N的开发，大抵都会用到gettext库/工具集( <a href="ftp://ftp.gnu.org/gnu/gettext/" target="_top">ftp://ftp.gnu.org/gnu/gettext/</a>&nbsp;) 和libiconv(<a href="http://www.gnu.org/software/libiconv/">http://www.gnu.org/software/libiconv/</a>) ,前者是用于实现Resource bundle,而后者用于各种编码转化。<br><br>(注:这里没有考虑cross-platform)<br><br><br>1. 实现方法<br><br>a . 使用po文件作为资源文件 (建议编码是utf-8)<br>&nbsp;&nbsp;&nbsp; 一方面因为UTF-8兼容ANSI,另外一方面考虑到G11N的程序大部分使用utf-8输出，比如输出到database,web UI, file等等。<br><br>b. 使用getext从resource file ( mo文件)读到相应的L10N resource文件<br>&nbsp;&nbsp;&nbsp;注意这里需要先setlocale来设置locale<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">locale.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">libintl.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;_(string)&nbsp;gettext&nbsp;(string)</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(setlocale(LC_ALL,&nbsp;&#8220;&#8221;)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;setlocale()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(bindtextdomain(package,&nbsp;&#8220;</span><span style="color: #000000;">/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">share</span><span style="color: #000000;">/</span><span style="color: #000000;">locale&#8221;)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;bindtextdomain()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;&#8211;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(textdomain(package)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;textdomain()&nbsp;error.\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;&#8211;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;_(&#8220;Hello,&nbsp;world</span><span style="color: #000000;">!</span><span style="color: #000000;">&#8221;));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br></span></div>
<br><br>c.如果要输出到控制台(console),因为不是console都支持unicode output，所以推荐的方法是首先是把utf-8转化为本地编码，然后使用printf输出。不推荐使用wprintf进行输出，一方面是因为wchar_t的大小随编译器不同，不好控制。另外一方面，很多wprintf的实现也都是先把wchar_t[]转为本地编码，然后输出。转化编码使用libconv<br><br>d.如果要输出到web pages,database,file，编码推荐使用utf-8.<br><br>e.如果要输出本地化的日期和时间，使用API: strftime<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">time.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)&nbsp;<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;tm&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptm;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;buffer[</span><span style="color: #000000;">100</span><span style="color: #000000;">];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;memset(buffer,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(time(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">t)&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;time()&nbsp;error:&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">m\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;((ptm&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;localtime(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">t))&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;NULL)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&#8220;localtime()&nbsp;error:&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">m\n&#8221;);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;strftime(buffer,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer),&nbsp;&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">x&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">X&#8221;,&nbsp;ptm);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;buffer);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br></span></div>
<br><br>f. 如果要输出本地化的数字和货币，使用API: strfmon<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: #eeeeee;"><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">monetary.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">argv)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;buffer[</span><span style="color: #000000;">100</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;strfmon(buffer,&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(buffer),&nbsp;&#8220;</span><span style="color: #000000;">%=*</span><span style="color: #000000;">i</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;12345.67);</span><span style="color: #000000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;</span><span style="color: #000000;">%</span><span style="color: #000000;">s\n&#8221;,&nbsp;buffer);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br></span></div>
<br><br>2.目录结构<br>/product<br>&nbsp;&nbsp;/i18n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/zh_TW<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;your.mo<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ja_JP&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;your.mo<br><br><img src ="http://www.cppblog.com/sandy/aggbug/9020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-26 16:57 <a href="http://www.cppblog.com/sandy/archive/2006/06/26/9020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入 printf / wprintf / console下的unicode output</title><link>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Wed, 21 Jun 2006 03:53:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/8779.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/21/8779.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/8779.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/8779.html</trackback:ping><description><![CDATA[1. printf 只能提供ANSI/MB 的输出，不支持输出unicode stream.<br />例如:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t test[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>是不会正确输出的<br /><br /><br />2.wprintf 同样不会提供unicode output,<br />   但是他会把wchar_t的string转为locale的SB/MB字符编码，然后输出<br />例如：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>会输出??1234之类的字符串，或者不输出任何结果<br />因为wprintf没有办法把L"测试Test"转为默认的ANSI,需要设置locale<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">setlocale(LC_ALL,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">chs</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试Test</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />wprintf(L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,test);</span></div>会有正确的输出<br />等同于printf("%ls",test);<br /><br />综上:  <font color="#ff3300"><strong>CRT I/O functions do not provide Unicode output.<br /><br /></strong><font color="#000000">3. Window console自从NT4就是一个真正的unicode console<br />不过输出unicode string,只有使用Windows API, WriteConsoleW<br />例如：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">wchar_t test[] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">测试1234</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br />DWORD ws;<br />WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),test,wcslen(test),</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ws,NULL);</span></div>可以正确的输出而不需要设置locale,因为是真正的unicode的输出，跟codepage无关<br /><br />4. 如何实现跨平台的console output<br />    不要使用wchar_t和wprintf,因为这些都依赖于编译器.<br />     ICU是IBM的一个成熟的跨平台支持unicode的libary,推荐使用<br /><br />以下是ICU的uprintf实现<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> uprintf(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> UnicodeString </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">str) {<br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">buf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />    int32_t len </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str.length();<br />    int32_t bufLen </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> len </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">;<br />    int32_t actualLen;<br />    buf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[bufLen </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br />    actualLen </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> str.extract(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, len, buf</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">, bufLen</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">); </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Default codepage conversion</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    buf[actualLen] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />    printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, buf);<br />    delete buf;<br />}</span></div>它也是先把Unicode string转化为本地的codepage,然后printf，虽然也不是unicode output,但是跨平台，大多数情况会工作得很好。<br /></font></font><img src ="http://www.cppblog.com/sandy/aggbug/8779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-21 11:53 <a href="http://www.cppblog.com/sandy/archive/2006/06/21/8779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>G11N Development(1) --- Unicode Basic &amp;&amp; wxxx function</title><link>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html</link><dc:creator>小明</dc:creator><author>小明</author><pubDate>Mon, 19 Jun 2006 11:45:00 GMT</pubDate><guid>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html</guid><wfw:comment>http://www.cppblog.com/sandy/comments/8722.html</wfw:comment><comments>http://www.cppblog.com/sandy/archive/2006/06/19/8722.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sandy/comments/commentRss/8722.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sandy/services/trackbacks/8722.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Globalization development &nbsp;&nbsp;<a href='http://www.cppblog.com/sandy/archive/2006/06/19/8722.html'>阅读全文</a><img src ="http://www.cppblog.com/sandy/aggbug/8722.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sandy/" target="_blank">小明</a> 2006-06-19 19:45 <a href="http://www.cppblog.com/sandy/archive/2006/06/19/8722.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>