Exemplo n.º 1
0
// Load the language packs from the configuration file
func LoadConfig(path string) error {
	bytes, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	if err := json.Unmarshal(bytes, &ShelterRESTLanguagePacks); err != nil {
		return err
	}

	// Check language formats. They should follow IANA language types

	if !model.IsValidLanguage(ShelterRESTLanguagePacks.Default) {
		return ErrInvalidLanguage
	}

	for _, language := range ShelterRESTLanguagePacks.Packs {
		if !model.IsValidLanguage(language.Name()) {
			return ErrInvalidLanguage
		}
	}

	// Load the default language pack
	ShelterRESTLanguagePack = ShelterRESTLanguagePacks.Select(ShelterRESTLanguagePacks.Default)
	if ShelterRESTLanguagePack == nil {
		return ErrDefaultLanguageNotFound
	}

	// TODO: Should we check if the configuration file languages are the same of the REST
	// messages file? Don't known what can we gain with this restriction, maybe system
	// language consistency.

	return nil
}
Exemplo n.º 2
0
// Convert a owner request object into a owner model object. It can return errors related
// to the e-mail format
func (o *OwnerRequest) toOwnerModel() (model.Owner, error) {
	var owner model.Owner

	email, err := mail.ParseAddress(o.Email)
	if err != nil {
		return owner, err
	}

	if !model.IsValidLanguage(o.Language) {
		return owner, ErrInvalidLanguage
	}

	owner = model.Owner{
		Email:    email,
		Language: model.NormalizeLanguage(o.Language),
	}

	return owner, nil
}