开发者

preg_replace and gettext problem

开发者 https://www.devze.com 2023-02-12 16:04 出处:网络
My project has templates with HTML. In order to ease the localization, I have added a preffix and suffix in all things I want to be translated of the HTML templates. Since I can\'t use _() inside HTML

My project has templates with HTML. In order to ease the localization, I have added a preffix and suffix in all things I want to be translated of the HTML templates. Since I can't use _() inside HTML, I do a replace before showing the template:

preg_replace("%\<t\>([a-zA-Z0-9\.\\s)]*)\</t\>%is",_("$1"),$template);

I replaced _() with my tr() translating function and apparently it gets hit, just cant find the translation i guess.

When I use print _('same text'); it shows up correctly.

preg_replace("%\<t\>([a-zA-Z0-9\.\\s)]*)开发者_StackOverflow中文版\</t\>%is",_("hello"),$template);

So i'm guessing there's some issue locating the translations while using preg_replace like that.

I'm not looking for alternatives to storing the data, I'm looking for a solution to this problem if you have any.


Use preg_replace_callback or the /e (eval - and it has a reason why it sounds like evil!) modifier of preg_replace.


your calling the gettext function before the preg_replace has fired, you need to allow the PCRE library to call the function for you, if your using PHP5+ then try this:

preg_replace_callback("%\<t\>([a-zA-Z0-9\.\\s)]*)\</t\>%is",function($matches){
    return $matches[1];
},$template);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号