Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep order in po files #58

Open
xavivars opened this issue Dec 20, 2021 · 4 comments
Open

Keep order in po files #58

xavivars opened this issue Dec 20, 2021 · 4 comments

Comments

@xavivars
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants