leonelquinteros / gotext

Go (Golang) GNU gettext utilities package
Other
437 stars 57 forks source link

Plurals export into po file #88

Open kuzminT opened 1 year ago

kuzminT commented 1 year ago

Please describe your issue

We're trying to use this package to parse translations from PO files or export them into PO. When we use parsing, it's clear and works well. Now I'm trying to create a new PO and have managed to do it with a string format, but there are some troubles with plurals. I couldn't find any example of how to work with them. Instead of getting one key and several translation variants for it, I have several separate keys and translations. Also, most of the properties for working with objects are private and I can't use them. So I'm doing something like this:

po := gotext.NewPo()
domain := po.GetDomain()
for i := range translations {
   domain.SetN(key.Name, "some value", i, translations[i].Value)
}

And as result in the file I see this strings:

msgid "Just one user online"
msgid_plural "Just one user online"
msgstr[0] "Il y a %d utilisateurs en ligne"

msgid "Just one user online"
msgid_plural "Just one user online"
msgstr[1] "Un seul utilisateur en ligne"

But it is not what I wanted, certainly. I could just concatenate some strings by myself, but we wanted to use this library and not work with bare hands.

Is this a bug, an improvement, a proposal or something else? Describe it.

Perhaps it's not a bug, just a lack of documentation. It would be great if the package were supplemented with similar examples.

donseba commented 10 months ago

I was going through the specific function. IF your key.Name is the same, as it should, for single and plural, then it should work.

// Set the (N)th plural form for the given string
func (do *Domain) SetN(id, plural string, n int, str string) {
    // Get plural form _before_ lock down
    pluralForm := do.pluralForm(n)

    do.trMutex.Lock()
    do.pluralMutex.Lock()
    defer do.trMutex.Unlock()
    defer do.pluralMutex.Unlock()

    if trans, ok := do.translations[id]; ok {
        trans.SetN(pluralForm, str)
    } else {
        trans = NewTranslation()
        trans.ID = id
        trans.PluralID = plural
        trans.SetN(pluralForm, str)
        do.translations[str] = trans
    }
}

See line if trans, ok := do.translations[id]; ok {

donseba commented 9 months ago

@kuzminT , I have found the issue .. in the snippet above you see do.translations[str] = trans , this should be do.translations[id] = trans

I have forked this repo since it is not active at the moment https://github.com/donseba/gotext