leonelquinteros / gotext

Go (Golang) GNU gettext utilities package
Other
432 stars 58 forks source link

Keep order in po files #58

Open xavivars opened 2 years ago

xavivars commented 2 years ago

I'm using the library to apply some changes to some existing PO files (from some open source projects), but due to how the library keeps the PO file internally, it's pretty hard to be able to use it:

When "writing back" the po file to text, it reorders all translations, which makes it impractical to later submit an MR to the upstream project.

This simple code (with input and output files attached) demonstrates the problem.

func main() {
    inPo := filepath.Join(projectpath.Root, "./in.po")
    outPo := filepath.Join(projectpath.Root, "./out.po")

    po := gotext.NewPo()
    po.ParseFile(inPo)

    data, _ := po.MarshalText()

    os.WriteFile(outPo, data, 0644)
}

in.po.txt out.po.txt

Is there any way to "reconstruct" the pofile to how it was before parsing it?

leonelquinteros commented 2 years ago

Hola @xavivars,

Yes this happens because of implementation details made to ensure consistent ordering of Po files. This also means that after your first MR, al consequent should be straightforward.
I don't think there is a way currently to handle your use case and can't think of an easy approach for it.

I'd say, if you're working with Po files as an editor, you may better leverage GNU gettext tools available for that.

Sorry I can't help further on this one.

xavivars commented 2 years ago

Yeah, that's the problem... I don't really work with the files as an editor, but instead I'm trying to automate some of those editings...

leonelquinteros commented 2 years ago

I'm afraid you'll need to write your own file exporter for this use case. Maybe as part of your script, instead of calling MarshalText() you parse the source file again, and replace line by line with the translations from the edited Po object. It shouldn't be too hard, you may also rely on some code from this package to get there faster: https://github.com/leonelquinteros/gotext/blob/master/po.go#L129-L207

xavivars commented 2 years ago

I was hoping to avoid string manipulation at all (reason why I started using gotext to start with), but I'll think about it.

Thanks!