Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

In reply to regex for translation by klayman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found