1

I need to filter out of a PO file all entries that are used only in the wordpress admin (so I can translate only user facing strings). In other words, if all references for any given entry contain the string "../admin" (i.e., begin with "#: ../admin") I don't want them in the output file.

Tools can be grep, sed, windows based, anything. Any ideas?

(Ideas on other ways to accomplish this are welcome, but I am thinking of filtering out the user facing strings for translation then placing only those on the PO file. The native language is English and I can leave all admin facing content in English.)

Filter this out:


#: ../admin/add_new_packet.php:64
#: ../admin_processing/add_new_packet_processing.php:9
msgid "Cancel"
msgstr ""

Leave this in:


#: ../header-employer.php:81
#: ../admin/add_new_packet.php:64
msgid "Logout"
msgstr ""

Here is the format for PO Files for those who are not familiar.

Gaia
  • 2,872
  • 1
  • 41
  • 59
  • 1
    comments on how to improve the question are also welcome. – Gaia Jul 16 '12 at 21:22
  • I'd suggest to add some sample input and desired output (and describe what you have tried so far, if anything). – Lev Levitsky Jul 16 '12 at 21:23
  • PO files have a standard format. Desired output is the same format without the entries that have references indicating they are used exclusively on the admin side. If you are not familiar with the format have a look at http://www.gnu.org/software/gettext/manual/html_node/PO-Files.html – Gaia Jul 16 '12 at 21:45
  • A link to format description is not the same as showing sample input. Providing sample input and output helps others to write, test and illustrate answers, even if your question is clear enough without it. I recommend to include examples wherever it's possible. _(I removed the downvote as you've shown `some` effort to improve the question. Still, I think you can/should improve it further)_ – Lev Levitsky Jul 16 '12 at 22:12

1 Answers1

1

One solution using awk. Content of script.awk:

BEGIN {
    ## Separate records by one or more blank lines.
    RS = ""

    ## Each line will be one field. Both for input and output.
    FS = OFS = "\n"
}

## For every paragraph...
{
    ## Flag: If 0, print current record of PO file. If 1, don't
    ## print it.
    admin = 0 

    ## Traverse all lines of each record. If matches '#: ../admin'
    ## set flag and end processing.
    for (i = 1; i <= NF; i++ ) { 
        if ( $i ~ /^#: *\.\.\/admin\/?/ ) { 
            admin = 1 
            break
        }   
    }   

    ## Print only if flag hasn't been set.
    if ( ! admin ) { 
        print $0 "\n"
    }   
}

You didn't provide neither input nor output as Lev Levitsky suggested in comments, so it's not easy to guess how a script should behave to get a solution and test it to check that works.

Birei
  • 35,723
  • 2
  • 77
  • 82