-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom gettext FS to workaround the default plural formula
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
Showing
6 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters