Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental approach for localisation of report #189

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
GaugeReportsDirEnvName = "gauge_reports_dir" // directory where reports are generated by plugins
OverwriteReportsEnvProperty = "overwrite_reports"
UseNestedSpecs = "use_nested_specs"
ReportLocalisation = "report_localisation"
)

func GetCurrentExecutableDir() (string, string) {
Expand Down Expand Up @@ -64,6 +65,14 @@ func GetProjectRoot() string {
return projectRoot
}

func GetLocale() string {
locale := os.Getenv(ReportLocalisation)
if locale == "" {
return "en"
}
return locale
}

func AddDefaultPropertiesToProject() {
defaultPropertiesFile := getDefaultPropertiesFile()

Expand All @@ -77,11 +86,16 @@ func AddDefaultPropertiesToProject() {
Name: OverwriteReportsEnvProperty,
DefaultValue: "true"})

reportLocalisationProperty := &(common.Property{
Comment: "The localisation used for the report. Defaults to 'en'",
Name: ReportLocalisation,
DefaultValue: "en"})

if !common.FileExists(defaultPropertiesFile) {
fmt.Printf("Failed to setup html report plugin in project. Default properties file does not exist at %s. \n", defaultPropertiesFile)
return
}
if err := common.AppendProperties(defaultPropertiesFile, reportsDirProperty, overwriteReportProperty); err != nil {
if err := common.AppendProperties(defaultPropertiesFile, reportsDirProperty, overwriteReportProperty, reportLocalisationProperty); err != nil {
fmt.Printf("Failed to setup html report plugin in project: %s \n", err)
return
}
Expand Down
11 changes: 11 additions & 0 deletions generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ func readTemplates(themePath string) {
return r
}

localFile := env.GetLocale() + ".properties"
config, err := ReadPropertiesFile(filepath.Join(getAbsThemePath(themePath), "localisation", localFile))
if err != nil {
log.Fatalf(err.Error())
}

var localise = func(s string) string {
return config[s]
}

var funcs = template.FuncMap{
"parseMarkdown": parseMarkdown,
"sanitize": sanitizeHTML,
Expand All @@ -309,6 +319,7 @@ func readTemplates(themePath string) {
"stringToUpper": strings.ToUpper,
"stringToTitle": strings.ToTitle,
"sum": sum,
"localised": localise,
}

f, err := ioutil.ReadFile(filepath.Join(getAbsThemePath(themePath), "views", "partials.tmpl"))
Expand Down
45 changes: 45 additions & 0 deletions generator/properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package generator

import (
"bufio"
"log"
"os"
"strings"
)

type AppConfigProperties map[string]string

func ReadPropertiesFile(filename string) (AppConfigProperties, error) {
config := AppConfigProperties{}

if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(line[equal+1:])
}
config[key] = value
}
}
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil, err
}

return config, nil
}
1 change: 1 addition & 0 deletions themes/default/localisation/de.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated_on = Erstellt am
1 change: 1 addition & 0 deletions themes/default/localisation/en.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated_on = Generated On
2 changes: 1 addition & 1 deletion themes/default/views/partials.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<span>{{.ExecutionTime}}</span>
</li>
<li>
<label>Generated On </label>
<label>{{(localised "generated_on")}}</label>
<span>{{.Timestamp}}</span>
</li>
</ul>
Expand Down