Added folder for german translation

This commit is contained in:
digitalcraftsman
2015-06-23 20:04:41 +02:00
parent bdd5423884
commit e47666b0ab
270 changed files with 16240 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
// Example code for Chapter 4.2 from "Build Web Application with Golang"
// Purpose: Shows how to perform server-side validation of user input from a form.
// Also shows to use multiple template files with predefined template names.
// Run `go run main.go` and then access http://localhost:9090
package main
import (
"apps/ch.4.2/validator"
"html/template"
"log"
"net/http"
)
const (
PORT = "9090"
HOST_URL = "http://localhost:" + PORT
)
var t *template.Template
type Links struct {
BadLinks [][2]string
}
// invalid links to display for testing.
var links Links
func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, HOST_URL+"/profile", http.StatusTemporaryRedirect)
}
func profileHandler(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "profile", links)
}
func checkProfile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
p := validator.ProfilePage{&r.Form}
t.ExecuteTemplate(w, "submission", p.GetErrors())
}
// This function is called before main()
func init() {
// Note: we can reference the loaded templates by their defined name inside the template files.
t = template.Must(template.ParseFiles("profile.gtpl", "submission.gtpl"))
list := make([][2]string, 2)
list[0] = [2]string{HOST_URL + "/checkprofile", "No data"}
list[1] = [2]string{HOST_URL + "/checkprofile?age=1&gender=guy&shirtsize=big", "Invalid options"}
links = Links{list}
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/profile", profileHandler)
http.HandleFunc("/checkprofile", checkProfile)
err := http.ListenAndServe(":"+PORT, nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

View File

@@ -0,0 +1,89 @@
{{define "profile"}}<!DOCTYPE html>
<html>
<style>
.row{
display: table-row;
}
.cell{
display: table-cell;
}
.required{
color: red
}
</style>
<body>
<h2>Profile Setup:</h2>
<form action="/checkprofile">
<div class="row">
<div class="cell"><span class="required">*</span>User Name:</div>
<div class="cell"><input type="text" name="username" id="username" required/></div>
</div>
<div class="row">
<div class="cell"><span class="required">*</span>Age:</div>
<div class="cell"><input type="number" min="13" max="130" name="age" id="age" size="3" required/></div>
</div>
<div class="row">
<div class="cell"><span class="required">*</span>Email:</div>
<div class="cell"><input type="email" name="email" id="email" placeholder="john@example.com" required/></div>
</div>
<div class="row">
<div class="cell"><span class="required">*</span>Birth day:</div>
<div class="cell">
<input type="date" name="birthday" id="birthday" placeholder="MM/DD/YYYY" required/>
</div>
</div>
<div class="row">
<div class="cell">Gender:</div>
<div class="cell">
<label for="gender_male">
Male: <input type="radio" name="gender" value="m" id="gender_male"/>
</label>
<label for="gender_female">
Female: <input type="radio" name="gender" value="f" id="gender_female"/>
</label>
<label for="gender_na">
N/A: <input type="radio" name="gender" value="na" id="gender_na"/>
</label>
</div>
</div>
<div class="row">
<div class="cell">Siblings:</div>
<div class="cell">
<label for="sibling_male">
Brother: <input type="checkbox" name="sibling" value="m" id="sibling_male"/>
</label>
<label for="sibling_female">
Sister: <input type="checkbox" name="sibling" value="f" id="sibling_female"/>
</label>
</div>
</div>
<div class="row">
<div class="cell">Shirt Size:</div>
<div class="cell">
<select id="shirt_size" >
<option></option>
<option value="s">Small</option>
<option value="m">Medium</option>
<option value="l">Large</option>
<option value="xl">X-Large</option>
<option value="xxl">XX-Large</option>
</select>
</div>
</div>
<div class="row">
<div class="cell">Chinese Name:</div>
<div class="cell"><input type="text" name="chineseName" id="chineseName"/></div>
</div>
<br/>
<span class="required">*</span>Required
<br/>
<input type="submit" value="Submit" id="submitBtn"/>
</form>
<h2>Invalid submissions</h2>
<ol>{{range .BadLinks}}
<li><a href="{{index . 0}}">{{index . 1}}</a></li>
{{end}}
</ol>
</body>
</html>
{{end}}

View File

@@ -0,0 +1,16 @@
{{define "submission"}}<!DOCTYPE html>
<html>
<body>
{{if .Errors}}
<h2>Errors:</h2>
<ol>
{{range .Errors}}
<li>{{.}}</li>
{{end}}
</ol>
{{else}}
Profile successfully submitted.
{{end}}
</body>
</html>
{{end}}

View File

@@ -0,0 +1,175 @@
// This file contains all the validators to validate the profile page.
package validator
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type ProfilePage struct {
Form *url.Values
}
type Errors struct {
Errors []error
}
// Goes through the form object and validates each element.
// Attachs an error to the output if validation fails.
func (p *ProfilePage) GetErrors() Errors {
errs := make([]error, 0, 10)
if *p.Form == nil || len(*p.Form) < 1 {
errs = append(errs, errors.New("No data was received. Please submit from the profile page."))
}
for name, val := range *p.Form {
if fn, ok := stringValidator[name]; ok {
if err := fn(strings.Join(val, "")); err != nil {
errs = append(errs, err)
}
} else {
if fn, ok := stringsValidator[name]; ok {
if err := fn(val); err != nil {
errs = append(errs, err)
}
}
}
}
return Errors{errs}
}
const (
// Used for parsing the time
mmddyyyyForm = "01/02/2006" // we want the date sent in this format
yyyymmddForm = "2006-01-02" // However, HTML5 pages send the date in this format
)
var stringValidator map[string]func(string) error = map[string]func(string) error{
// parameter name : validator reference
"age": checkAge,
"birthday": checkDate,
"chineseName": checkChineseName,
"email": checkEmail,
"gender": checkGender,
"shirtsize": checkShirtSize,
"username": checkUsername,
}
var stringsValidator map[string]func([]string) error = map[string]func([]string) error{
// parameter name : validator reference
"sibling": checkSibling,
}
// Returns true if slices have a common element
func doSlicesIntersect(s1, s2 []string) bool {
if s1 == nil || s2 == nil {
return false
}
for _, str := range s1 {
if isElementInSlice(str, s2) {
return true
}
}
return false
}
func isElementInSlice(str string, sl []string) bool {
if sl == nil || str == "" {
return false
}
for _, v := range sl {
if v == str {
return true
}
}
return false
}
// Checks if all the characters are chinese characters. Won't check if empty.'
func checkChineseName(str string) error {
if str != "" {
if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{9fa5}]+$", strings.Trim(str, " ")); !m {
return errors.New("Please make sure that the chinese name only contains chinese characters.")
}
}
return nil
}
// Checks if a user name exist.
func checkUsername(str string) error {
if strings.Trim(str, " ") == "" {
return errors.New("Please enter a username.")
}
return nil
}
// Check if age is a number and between 13 and 130
func checkAge(str string) error {
age, err := strconv.Atoi(str)
if str == "" || err != nil {
return errors.New("Please enter a valid age.")
}
if age < 13 {
return errors.New("You must be at least 13 years of age to submit.")
}
if age > 130 {
return errors.New("You're too old to register, grandpa.")
}
return nil
}
func checkEmail(str string) error {
if m, err := regexp.MatchString(`^[^@]+@[^@]+$`, str); !m {
fmt.Println("err = ", err)
return errors.New("Please enter a valid email address.")
}
return nil
}
// Checks if a valid date was passed.
func checkDate(str string) error {
_, err := time.Parse(mmddyyyyForm, str)
if err != nil {
_, err = time.Parse(yyyymmddForm, str)
}
if str == "" || err != nil {
return errors.New("Please enter a valid Date.")
}
return nil
}
// Checks if the passed input is a known gender option
func checkGender(str string) error {
if str == "" {
return nil
}
siblings := []string{"m", "f", "na"}
if !isElementInSlice(str, siblings) {
return errors.New("Please select a valid gender.")
}
return nil
}
// Check if all the values are known options.
func checkSibling(strs []string) error {
if strs == nil || len(strs) < 1 {
return nil
}
siblings := []string{"m", "f"}
if siblings != nil && !doSlicesIntersect(siblings, strs) {
return errors.New("Please select a valid sibling")
}
return nil
}
// Checks if the shirt size is a known option.
func checkShirtSize(str string) error {
if str == "" {
return nil
}
shirts := []string{"s", "m", "l", "xl", "xxl"}
if !isElementInSlice(str, shirts) {
return errors.New("Please select a valid shirt size")
}
return nil
}