KEEP K.I.S.S.

tk's blog

编写国际化软件的一些提示

tk posted @ Dec 28, 2013 12:10:13 AM in C with tags i18n gettext , 2950 阅读

笔记,摘自 Internationalization Tips,基于国际化工具 GNU gettext

1. 不要分割整句

例如

String s = i18n("Press OK to delete ") + n + i18n(" files");

这样把一句话拆成几部分,在抽取字符串资源后,翻译人员在看到 " files" 这样的字符串时会莫名其妙,翻译到其他语言里很有可能意思不伦不类,表达不清。而且 像 " files" 这样短的词会整个程序中出现多次,并不能保证在每个语境下都是同一个 意思。

这种情况下应该使用占位符

printf(i18n("Press OK to delete %d files"), n);

这里还提到了一个C语言里的技巧:编译时于预处理器会将连续多行的字符串字面量 自动拼接,合并成一个,这样在写很长的字符串时,可以拆成多行来写。

2. 避免同时使用多个占位符

例如

printf("Press OK to %s %d file(s)", action, number);

像这样一条句子里使用了两个占位符,由于后面的参数位置写死在代码里,在翻译时,翻 译字符串中的占位符也必须依照参数的顺序,在某些语言习惯里,可能造成翻译困难或者 不够地道。比如

printf("You should say %s when %s comes.", hello, somebody);

当翻译成中文时

printf("当%s来的时候,你得说%s", somebody, hello);

后面的参数需要调整顺序。

由于C语言的 printf 族函数并不提供类似 C#、Python 那种 {0} 带序号的占位符用法,所以使用多个占位符不是明智之举。虽然标准库不支持,但是有一 些第三方库可以做到,例如 win32 下的 FormatMessage 和 java 的 MessageFormat ,还可以考虑 Qt。

其实个人觉得 Ruby 中的变量名占位符更好,翻译时候翻译人员可以根据有意义的变量名 来翻译,而不是数字。

当然,和作者再三强调的一样,最好的方法就是避免使用多个占位符。

3. 多复数处理

在某些语言中,单数和复数表达可能不一样。

例如

printf(i18n("You have won %d point(s)!"), n);

n == 1 时,输出 "1 points" 就欠妥当,最好修改成

if (n == 1)
    printf(i18n("You have won one point!"));
else
    printf(i18n("You have won %d points!"), n);

注意,这还没完,并不是所有语言就仅仅区分 n == 1n != 1 这两种情况, 有些语言中,当 n == 101 时,也应当使用单数。

用硬编码来处理所有这些情行则会把代码弄得臃肿不堪,gettext 则提供了解决方法:ngettext

4. 翻译歧义

在编码时要注意多义词的处理。比如英文单词 right ,表示方向时是 的意思, 在表示对错时是 的意思。这个时候就需要给字符串加上前缀来以示区别。

作者给了个菜单项的例子,使用 | 来作为前缀

Menu|File
Menu|Printer
Menu|File|Open
Menu|File|New
Menu|Printer|Select
Menu|Printer|Open

然后使用一个新函数来查找翻译字符串

char * sgettext (const char *msgid)
{
    char *msgval = gettext (msgid);
    if (msgval == msgid)
        msgval = strrchr (msgid, '|') + 1;
    return msgval;
}

这个方法有几个缺点

*   包含了 `|` 字符的字符串不能这样来处理
*   翻译人员必须清楚只有 `|` 字符之后的字符串才是翻译的内容
*   开发者也许只用 `sgettext` 函数,并没有注意到字符串是否包含有 `|`

从这点上看,个人认为 MSVS 那套字符串也是有优势的。

  • 无匹配
Avatar_small
依云 说:
Dec 28, 2013 12:41:21 PM

GNU C 的 printf 支持调整格式化占位符的顺序的

Avatar_small
依云 说:
Dec 28, 2013 12:41:55 PM

gettext 有处理复数问题的 ngettext 啊。

Johny 说:
Nov 14, 2022 04:21:10 PM

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks... All video downloader

Johny 说:
Nov 22, 2022 11:15:06 PM

I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. Satta king

Johny 说:
Nov 27, 2022 04:26:00 PM

I haven't any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. [url=https ://deepweb.blog/]deep web[/url]

johny 说:
Dec 22, 2022 03:33:19 PM

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. Plagiarism Checker

ขาย PLC 说:
Jan 09, 2023 06:31:41 AM

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. ขาย PLC

testosterone injecti 说:
Mar 10, 2023 09:59:44 AM

Thanks for the informative and helpful post, obviously in your blog everything is good.. testosterone injections for sale

SEO 说:
May 13, 2023 05:37:37 PM

I invite you to the page where you can read with interesting information on similar topics. 에볼루션카지노

SEO 说:
May 13, 2023 10:54:37 PM

For true fans of this thread I will address is a free online! 마사지사이트

SEO 说:
Jun 03, 2023 02:34:26 AM

Welcome to the party of my life here you will learn everything about me. 출장안마

SEO 说:
Jun 03, 2023 02:51:49 PM

I invite you to the page where you can read with interesting information on similar topics. 평택출장마사지

SEO 说:
Jun 04, 2023 09:30:18 PM I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. 대구의밤
SEO 说:
Jun 06, 2023 01:03:23 AM

The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. 파워볼

Damen Dashiki Hemd 说:
Aug 05, 2023 04:47:58 AM

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it. Damen Dashiki Hemd

Rockwall Roofers 说:
Aug 05, 2023 05:14:02 AM

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it. Rockwall Roofers

new york stripper 说:
Aug 07, 2023 01:59:07 AM

Very nice blog and articles. I am realy very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post. new york stripper

asd 说:
Aug 15, 2023 05:13:57 AM

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. sinarplay

singapore scrapping 说:
Aug 16, 2023 11:52:25 PM

Writing with style and getting good compliments on the article is quite hard, to be honest.But you've done it so calmly and with so cool feeling and you've nailed the job. This article is possessed with style and I am giving good compliment. Best! singapore scrapping your car

Matt Davies Stockton 说:
Aug 23, 2023 09:41:25 AM

I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon… Matt Davies Stockton

Johny 说:
Aug 31, 2023 02:34:28 PM

This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post. last minute naar ornos

Johny 说:
Aug 31, 2023 03:35:17 PM

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. last minute naar sitges

Johny 说:
Sep 09, 2023 11:57:13 AM

I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. daftar pentaslot

sophia 说:
Sep 23, 2023 03:24:29 PM Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. Fortune Mouse
Johny 说:
Oct 22, 2023 12:01:20 PM

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks ufabetทางเข้าเล่น

Johny 说:
Oct 22, 2023 01:06:49 PM

This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. ufabetทางเข้าเล่น

SEO Expert 说:
Nov 09, 2023 04:30:03 PM

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. plagiarism checker unlimited words

SEO Expert 说:
Nov 09, 2023 06:15:14 PM

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. banglaword

SEO Expert 说:
Nov 09, 2023 07:19:49 PM

I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! h5.fire kirin

seo service london 说:
Feb 21, 2024 09:01:11 PM

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter