V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xinzhi
V2EX  ›  问与答

PHP:帮忙修改一段代码呗,修改“由XXX 发表于 14:18 1 天 之前”为“由XXX 发表于 1 天 之前”

  •  
  •   xinzhi · 2011-02-11 23:05:44 +08:00 · 5467 次点击
    这是一个创建于 4839 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一边帮亲戚刷机,一边帮朋友改WP, 两边忙不过来了。

    <span class="author"><?php printf(__('Posted by %1$s at %2$s', 'suf_theme'), '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>', sprintf(get_the_time(get_option('time_format')))); ?>&nbsp;<?php the_time_ago('d M, Y'); ?></span>

    // 直接去掉sprintf这一段,报错,连同逗号什么的,去掉,WP整站崩溃了。。。还是求助吧。

    最后送给对子大家看:
    RT @xie107: 【程序猿】上联:为系统而生,为框架而死,为debug奋斗一辈子 下联:吃符号的亏,上大小写的当,最后死在需求上! 横批:杯具程序员。

    谢谢帮忙!
    9 条回复    1970-01-01 08:00:00 +08:00
    sxysxy
        1
    sxysxy  
       2011-02-11 23:20:00 +08:00
    sprintf当然不能去掉了~~那是get_author_posts_url 函数里的一个参数吧, 你删了它就会报错了。
    darasion
        2
    darasion  
       2011-02-11 23:23:25 +08:00
    试试:

    <span class="author"><?php printf(__('Posted by %1$s at %2$s', 'suf_theme'), '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>', ''); ?>&nbsp;<?php the_time_ago('d M, Y'); ?></span>
    benzhe
        3
    benzhe  
       2011-02-11 23:27:31 +08:00
    楼上的说法是错的,详看这里:http://www.w3school.com.cn/php/func_string_printf.asp

    下面应该没问题:

    <span class="author">

    <?php printf(__('Posted by %1$s', 'suf_theme'), '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>'); ?>

    &nbsp;

    <?php the_time_ago('d M, Y'); ?></span>
    xinzhi
        4
    xinzhi  
    OP
       2011-02-12 00:21:07 +08:00
    @benzhe 您给的代码运行良好。问题好像出现在,WP没法应用国际化了,中文翻译没出来,直接是“Posted by XXX 1 天 之前 ”(后面的中文是JS处理的)
    benzhe
        5
    benzhe  
       2011-02-12 00:40:51 +08:00
    @xinzhi 不清楚wp替换语言的机制,不过要是想弄中文的话把代码里的“Posted by”直接修改成中文不就成了?

    像这样:
    <span class="author"><?php printf(__('由 %1$s 发表于 ', 'suf_theme'), '<a href="'.get_author_posts_url(get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a>'); ?> &nbsp; <?php the_time_ago('d M, Y'); ?></span>

    没有考虑中文编码问题,如果printf出问题就直接不用,get_the_author_meta这类函数直接返回字符串的
    darasion
        6
    darasion  
       2011-02-12 00:43:11 +08:00
    @xinzhi 记得好像带下划线的这个函数就是国际化函数。__(),里边这个:'Posted by %1$s at %2$s' 参数不能变。变了国际化就找不到相应的对应了。可以看做是数据库中的一个key。

    我想到俩办法,
    1、你可以把 sprintf(....xxxxx..x.x.x) 替换成一个空值。
    2、如果只用中文,就干脆自己用汉字直接写。不管英文什么的了。。
    darasion
        7
    darasion  
       2011-02-12 00:55:47 +08:00
    国际化是用 gettext 的。

    大概原理就是 用一个po文件做翻译,然后编译成mo文件。
    po文件里边很多这种东西:

    #: src/name.c:36
    msgid "My name is %s.\n"
    msgstr "我的名字是 %s \n"

    刚才wp代码里__()函数的参数 'Posted by %1$s at %2$s' ,就是po文件里边的一个msgid,翻译后的就是 msgstr。wp 通过 __() 函数读取编译好的mo文件,得到相应的翻译文字。


    好像大概是这样吧,没仔细看。。

    参考:
    http://en.wikipedia.org/wiki/Gettext
    http://www.gnu.org/software/gettext/
    xinzhi
        8
    xinzhi  
    OP
       2011-02-12 01:23:31 +08:00
    直接写中文吧,Jquery timeago插件里面也是用中文的。再次感谢大家!
    POPOEVER
        9
    POPOEVER  
       2011-02-12 12:08:48 +08:00
    估计在 po 文件里直接是整句翻译,一句里有几个参数,这边删掉一个参数,po 文件里对不上所以就出不来了,试试看到 po 文件里找找 Posted by 对应的翻译字串,看看一个字串里是不是有几个参数,把对应 get_the_time(); 的那个参数直接删掉看看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5513 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:36 · PVG 17:36 · LAX 02:36 · JFK 05:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.