2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-09-23 00:04:55 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package locale // import "miniflux.app/v2/internal/locale"
|
2018-09-23 00:04:55 +02:00
|
|
|
|
|
|
|
import (
|
2021-02-17 07:58:44 +01:00
|
|
|
"embed"
|
2018-09-23 00:04:55 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type translationDict map[string]interface{}
|
|
|
|
type catalog map[string]translationDict
|
|
|
|
|
|
|
|
var defaultCatalog catalog
|
|
|
|
|
2021-02-17 07:58:44 +01:00
|
|
|
//go:embed translations/*.json
|
|
|
|
var translationFiles embed.FS
|
|
|
|
|
|
|
|
// LoadCatalogMessages loads and parses all translations encoded in JSON.
|
|
|
|
func LoadCatalogMessages() error {
|
|
|
|
var err error
|
2024-02-28 16:08:20 +01:00
|
|
|
defaultCatalog = make(catalog, len(AvailableLanguages()))
|
2018-09-23 00:04:55 +02:00
|
|
|
|
2021-02-17 07:58:44 +01:00
|
|
|
for language := range AvailableLanguages() {
|
|
|
|
defaultCatalog[language], err = loadTranslationFile(language)
|
2018-09-23 00:04:55 +02:00
|
|
|
if err != nil {
|
2021-02-17 07:58:44 +01:00
|
|
|
return err
|
2018-09-23 00:04:55 +02:00
|
|
|
}
|
2021-02-17 07:58:44 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-23 00:04:55 +02:00
|
|
|
|
2021-02-17 07:58:44 +01:00
|
|
|
func loadTranslationFile(language string) (translationDict, error) {
|
|
|
|
translationFileData, err := translationFiles.ReadFile(fmt.Sprintf("translations/%s.json", language))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-23 00:04:55 +02:00
|
|
|
}
|
2021-02-17 07:58:44 +01:00
|
|
|
|
|
|
|
translationMessages, err := parseTranslationMessages(translationFileData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return translationMessages, nil
|
2018-09-23 00:04:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 07:58:44 +01:00
|
|
|
func parseTranslationMessages(data []byte) (translationDict, error) {
|
|
|
|
var translationMessages translationDict
|
|
|
|
if err := json.Unmarshal(data, &translationMessages); err != nil {
|
|
|
|
return nil, fmt.Errorf(`invalid translation file: %w`, err)
|
2018-09-23 00:04:55 +02:00
|
|
|
}
|
2021-02-17 07:58:44 +01:00
|
|
|
return translationMessages, nil
|
2018-09-23 00:04:55 +02:00
|
|
|
}
|