camper/pkg/form/input.go
jordi fita mas ff6e9497b5 Replace contact page with location
Customer does not want a contact page, but a page where they can write
the direction on how to reach the campground, with a Google map embed
instead of using Leaflet, because Google Maps shows the reviews right
in the map.

That means i had to replace the GPS locations with XML fields for the
customer to write.  In all four languages.

This time i tried a translation approach inspired by PrestaShop: instead
of opening a new page for each language, i have all languages in the
same page and use AlpineJS to show just a single language.  It is far
easier to write the translations, even though you do not have the source
text visible, specially in this section that there is no place for me
to put the language links.
2023-12-21 21:17:04 +01:00

72 lines
1.2 KiB
Go

/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package form
import (
"database/sql/driver"
"dev.tandem.ws/tandem/camper/pkg/locale"
"net/http"
"strconv"
"strings"
)
type Input struct {
Name string
Val string
Error error
}
func (input *Input) setError(err error) {
input.Error = err
}
func (input *Input) FillValue(r *http.Request) {
input.Val = strings.TrimSpace(r.FormValue(input.Name))
}
func (input *Input) Value() (driver.Value, error) {
return input.Val, nil
}
func (input *Input) Int() int {
i, err := strconv.Atoi(input.Val)
if err != nil {
panic(err)
}
return i
}
func (input *Input) L10nInput() *L10nInput {
return &L10nInput{
Input: Input{
Name: input.Name,
},
Source: input.Val,
}
}
type L10nInput struct {
Input
Source string
}
type I18nInput map[string]*Input
func NewI18nInput(locales locale.Locales, name string) I18nInput {
input := make(I18nInput)
for lang := range locales {
input[lang.String()] = &Input{
Name: name + "." + lang.String(),
}
}
return input
}
func (input I18nInput) FillValue(r *http.Request) {
for _, inner := range input {
inner.FillValue(r)
}
}