http://qs321.pair.com?node_id=1056586

klayman has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone. For past few days am struggling with problem and am hoping that someone can point me to the right direction. Project that am working on is written in mason and using older version of perl (5.10.1). Its online cms that is using simple translation module that needs to be extened. At the moment cms is pulling in templates that have few tags in them which are replaced before served to the client like {T_OPTION} or {T_SELECTION}. These can be replaced with strings like __("pick") or __('shirt'), where regular expression is matching these tags and replacing them with translations for specific language selected by client:

$c =~ s/__\(["'](.*?)["']\)/&gettr($1)/egis

Problem arise if we want to add specific tags to be replaced by cms into template translation sentences eg
__("Please choose your {T_OPTION}")
I've tried to take out the tags for translation so they can be used anywhere in the sentence and that the final sentence can be matched against the po file that contains all translated strings eg
__("Please choose your [_0]",{T_OPTION})
and sentense can have different order of words in different languages to make sense as well. [_0] will be replaced by whatever is added as {T_OPTION} where after tag is replaced whole translation text may look like __("Please choose your [_0]",__("shirt")).
Here you can see my problem. I need to somehow let regex match first part of the code as translation string and second (everything after '",') as parameters and then replace them in the original sentense. I have sub doing this. So I've tried to create regex that will match these cases >

s/__\(('.*?[^\\']'|".*?[^\\"]")(?:,(,?.*?[^'"]))?\)/&gettr($1, $2)/egis;

this accounts for escaped apostrophes and brackets as well and it works however this is slowing our cms down and renders it almost unusable. I was trying to use standard perl modules like Text::Ballanced and Regexp::Common however could not get them working as I wanted to. Could there be any simpler solution to this problem? (I cannot use different translation module as many templates would need to be rewritten to use it)