gotext icon indicating copy to clipboard operation
gotext copied to clipboard

Keep order in po files

Open xavivars opened this issue 2 years ago • 4 comments

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?

xavivars avatar Dec 20 '21 18:12 xavivars

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.

leonelquinteros avatar Dec 20 '21 18:12 leonelquinteros

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...

xavivars avatar Dec 21 '21 08:12 xavivars

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

leonelquinteros avatar Dec 21 '21 13:12 leonelquinteros

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!

xavivars avatar Dec 21 '21 14:12 xavivars