Skip to content

Commit

Permalink
Use a custom gettext FS to workaround the default plural formula
Browse files Browse the repository at this point in the history
By default gettext-go uses the `nplurals=1; plural=0;` plural formula,
but this doesn't make sense for programs with default strings being in
English.

Use a default gettext.FileSystem backed by an OS one returning empty
data if there is no po / mo file. In this case the language will be
taken into account, while this is not the case using the
`nilTranslator`.
  • Loading branch information
cbosdo committed Apr 9, 2024
1 parent e738482 commit 57a73c4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion mgradm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

"github.com/chai2010/gettext-go"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd"
l10n_utils "github.com/uyuni-project/uyuni-tools/shared/l10n/utils"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// Run runs the `mgradm` root command.
func Run() error {
gettext.BindLocale(gettext.New("mgradm", utils.LocaleRoot))
gettext.BindLocale(gettext.New("mgradm", utils.LocaleRoot, l10n_utils.New(utils.LocaleRoot)))
run, err := cmd.NewUyuniadmCommand()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion mgrctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

"github.com/chai2010/gettext-go"
"github.com/uyuni-project/uyuni-tools/mgrctl/cmd"
l10n_utils "github.com/uyuni-project/uyuni-tools/shared/l10n/utils"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// Run runs the `mgrctl` root command.
func Run() error {
gettext.BindLocale(gettext.New("mgrctl", utils.LocaleRoot))
gettext.BindLocale(gettext.New("mgrctl", utils.LocaleRoot, l10n_utils.New(utils.LocaleRoot)))
run, err := cmd.NewUyunictlCommand()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion mgrpxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

"github.com/chai2010/gettext-go"
"github.com/uyuni-project/uyuni-tools/mgrpxy/cmd"
l10n_utils "github.com/uyuni-project/uyuni-tools/shared/l10n/utils"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// Run runs the `mgrpxy` root command.
func Run() error {
gettext.BindLocale(gettext.New("mgrpxy", utils.LocaleRoot))
gettext.BindLocale(gettext.New("mgrpxy", utils.LocaleRoot, l10n_utils.New(utils.LocaleRoot)))
run, err := cmd.NewUyuniproxyCommand()
if err != nil {
return err
Expand Down
50 changes: 50 additions & 0 deletions shared/l10n/utils/defaultfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package l10n

import "github.com/chai2010/gettext-go"

// DefaultFS providing a empty data if no data is found.
type DefaultFS struct {
osFs gettext.FileSystem
gettext.FileSystem
}

// New creates a new DefaultFS delegating to an OS FileSystem.
func New(path string) *DefaultFS {
return &DefaultFS{
osFs: gettext.OS(path),
}
}

// LocaleList gets the list of locales from the underlying os FileSystem.
func (f *DefaultFS) LocaleList() []string {
return f.osFs.LocaleList()
}

// LoadMessagesFile loads a messages or returns the content of an empty json file.
func (f *DefaultFS) LoadMessagesFile(domain, lang, ext string) ([]byte, error) {
osFile, err := f.osFs.LoadMessagesFile(domain, lang, ext)
// Return an empty file by default
if err != nil {
return []byte("[]"), nil
}
return osFile, nil
}

// LoadResourceFile loads the resource file or returns empty data.
func (f *DefaultFS) LoadResourceFile(domain, lang, ext string) ([]byte, error) {
osFile, err := f.osFs.LoadResourceFile(domain, lang, ext)
// Return an empty file by default
if err != nil {
return []byte{}, nil
}
return osFile, nil
}

// String returns a name of the FileSystem.
func (f *DefaultFS) String() string {
return "DefaultFS"
}
2 changes: 1 addition & 1 deletion shared/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func checkValueSize(value string, min int, max int) bool {
}

if len(value) < min {
fmt.Printf(NL("Has to be more than %d character long", "Has to be more that %d characters long", min), min)
fmt.Printf(NL("Has to be more than %d character long", "Has to be more than %d characters long", min), min)
return false
}
if len(value) > max {
Expand Down
10 changes: 10 additions & 0 deletions shared/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"testing"

expect "github.com/Netflix/go-expect"
"github.com/chai2010/gettext-go"
l10n_utils "github.com/uyuni-project/uyuni-tools/shared/l10n/utils"
)

type askTestData struct {
Expand All @@ -24,6 +26,10 @@ type askTestData struct {
}

func TestAskIfMissing(t *testing.T) {
// Set english locale to not depend on the system one
gettext.BindLocale(gettext.New("", "", l10n_utils.New("")))
gettext.SetLanguage("en")

c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
if err != nil {
t.Errorf("Failed to create fake console")
Expand Down Expand Up @@ -83,6 +89,10 @@ func TestAskIfMissing(t *testing.T) {
}

func TestAskPasswordIfMissing(t *testing.T) {
// Set english locale to not depend on the system one
gettext.BindLocale(gettext.New("", "", l10n_utils.New("")))
gettext.SetLanguage("en")

c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
if err != nil {
t.Errorf("Failed to create fake console")
Expand Down

0 comments on commit 57a73c4

Please sign in to comment.