2018-03-23 20:17:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package gotext
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2023-03-16 09:53:22 +00:00
|
|
|
"io/fs"
|
2020-09-10 20:02:24 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
2018-09-07 21:14:38 +00:00
|
|
|
|
2018-08-14 14:01:14 +00:00
|
|
|
// Translator interface is used by Locale and Po objects.Translator
|
|
|
|
// It contains all methods needed to parse translation sources and obtain corresponding translations.
|
2018-03-23 20:17:05 +00:00
|
|
|
type Translator interface {
|
|
|
|
ParseFile(f string)
|
|
|
|
Parse(buf []byte)
|
|
|
|
Get(str string, vars ...interface{}) string
|
|
|
|
GetN(str, plural string, n int, vars ...interface{}) string
|
|
|
|
GetC(str, ctx string, vars ...interface{}) string
|
|
|
|
GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
|
2018-09-07 21:14:38 +00:00
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
GetDomain() *Domain
|
2018-09-07 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TranslatorEncoding is used as intermediary storage to encode Translator objects to Gob.
|
|
|
|
type TranslatorEncoding struct {
|
|
|
|
// Headers storage
|
2020-09-26 18:16:19 +00:00
|
|
|
Headers HeaderMap
|
2018-09-07 21:14:38 +00:00
|
|
|
|
|
|
|
// Language header
|
|
|
|
Language string
|
|
|
|
|
|
|
|
// Plural-Forms header
|
|
|
|
PluralForms string
|
|
|
|
|
|
|
|
// Parsed Plural-Forms header values
|
|
|
|
Nplurals int
|
|
|
|
Plural string
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
Translations map[string]*Translation
|
|
|
|
Contexts map[string]map[string]*Translation
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
// GetTranslator is used to recover a Translator object after unmarshalling the TranslatorEncoding object.
|
|
|
|
// Internally uses a Po object as it should be switchable with Mo objects without problem.
|
|
|
|
// External Translator implementations should be able to serialize into a TranslatorEncoding object in order to
|
|
|
|
// deserialize into a Po-compatible object.
|
2018-09-07 21:14:38 +00:00
|
|
|
func (te *TranslatorEncoding) GetTranslator() Translator {
|
2020-09-10 20:02:24 +00:00
|
|
|
po := NewPo()
|
|
|
|
po.domain = NewDomain()
|
|
|
|
po.domain.Headers = te.Headers
|
|
|
|
po.domain.Language = te.Language
|
|
|
|
po.domain.PluralForms = te.PluralForms
|
|
|
|
po.domain.nplurals = te.Nplurals
|
|
|
|
po.domain.plural = te.Plural
|
|
|
|
po.domain.translations = te.Translations
|
2023-04-04 14:28:57 +00:00
|
|
|
po.domain.contextTranslations = te.Contexts
|
2018-09-07 21:14:38 +00:00
|
|
|
|
|
|
|
return po
|
2018-03-23 20:17:05 +00:00
|
|
|
}
|
2020-09-10 20:02:24 +00:00
|
|
|
|
2024-07-22 06:30:26 +00:00
|
|
|
// getFileData reads a file and returns the byte slice after doing some basic sanity checking
|
2023-03-16 09:53:22 +00:00
|
|
|
func getFileData(f string, filesystem fs.FS) ([]byte, error) {
|
|
|
|
if filesystem != nil {
|
|
|
|
return fs.ReadFile(filesystem, f)
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:02:24 +00:00
|
|
|
// Check if file exists
|
|
|
|
info, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that isn't a directory
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil, errors.New("cannot parse a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadFile(f)
|
|
|
|
}
|