Repository: gobuffalo/buffalo (language: go)

Issue:
Missing i18n import with --api flag
### Description
Using the latest 0.16.8 generating an api with --api result in a project that cannot be compiled once you add a resource to the project

### Steps to Reproduce the Problem

  1. buffalo new coke --api
  2. buffalo g resource profile name
  3. buffalo dev

result:
generatedApps/seizetst02/models
generatedApps/seizetst02/actions
#generatedApps/seizetst02/actions
actions/profiles.go:133:30: undefined: T
actions/profiles.go:189:30: undefined: T
actions/profiles.go:223:30: undefined: T
buffalo: 2020/05/04 10:14:25 === Error! ===
buffalo: 2020/05/04 10:14:25 === exit status 2

###  Solution
In app.go add the following missing line:
import i18n "github.com/gobuffalo/mw-i18n"
var T *i18n.Translator //missing

Note: thosse lines are generated in regular Html app (non --api)


Discussion / hints:
I am wondering if the bug is because we don t want to have i18n for APIs ...then the problem is having i18n in the ressources
OR
**What I think is that we are missing it in API
So it should be added in API's tpl here :**

https://github.com/gobuffalo/buffalo/blob/b30b19999a85328c743266373b38df2ad8a5d3fb/genny/newapp/api/templates/actions/app.go.tmpl#L14

**SAME as in WEB**
https://github.com/gobuffalo/buffalo/blob/b30b19999a85328c743266373b38df2ad8a5d3fb/genny/newapp/web/templates/actions/app.go.tmpl#L16
https://github.com/gobuffalo/buffalo/blob/b30b19999a85328c743266373b38df2ad8a5d3fb/genny/newapp/web/templates/actions/app.go.tmpl#L24

Fix patch:
diff --git a/app.go b/app.go
index 8ef8245e9..a255bd216 100644
--- a/app.go
+++ b/app.go
@@ -23,6 +23,10 @@ type App struct {
 	root          *App
 	children      []*App
 	filepaths     []string
+
+	// Routenamer for the app. This field provides the ability to override the
+	// base route namer for something more specific to the app.
+	RouteNamer RouteNamer
 }
 
 // Muxer returns the underlying mux router to allow
@@ -48,6 +52,8 @@ func New(opts Options) *App {
 		moot:     &sync.RWMutex{},
 		routes:   RouteList{},
 		children: []*App{},
+
+		RouteNamer: baseRouteNamer{},
 	}
 
 	dem := a.defaultErrorMiddleware
diff --git a/route_mappings.go b/route_mappings.go
index d77adc099..e5c9429a7 100644
--- a/route_mappings.go
+++ b/route_mappings.go
@@ -11,7 +11,6 @@ import (
 	"strings"
 
 	"github.com/gobuffalo/envy"
-	"github.com/gobuffalo/flect"
 	"github.com/gobuffalo/flect/name"
 	"github.com/gorilla/handlers"
 )
@@ -263,7 +262,7 @@ func (a *App) addRoute(method string, url string, h Handler) *RouteInfo {
 
 	url = path.Join(a.Prefix, url)
 	url = a.normalizePath(url)
-	name := a.buildRouteName(url)
+	name := a.RouteNamer.NameRoute(url)
 
 	hs := funcKey(h)
 	r := &RouteInfo{
@@ -291,61 +290,6 @@ func (a *App) addRoute(method string, url string, h Handler) *RouteInfo {
 	return r
 }
 
-//buildRouteName builds a route based on the path passed.
-func (a *App) buildRouteName(p string) string {
-	if p == "/" || p == "" {
-		return "root"
-	}
-
-	resultParts := []string{}
-	parts := strings.Split(p, "/")
-
-	for index, part := range parts {
-
-		originalPart := parts[index]
-
-		var previousPart string
-		if index > 0 {
-			previousPart = parts[index-1]
-		}
-
-		var nextPart string
-		if len(parts) > index+1 {
-			nextPart = parts[index+1]
-		}
-
-		isIdentifierPart := strings.Contains(part, "{") && (strings.Contains(part, flect.Singularize(previousPart)))
-		isSimplifiedID := part == `{id}`
-
-		if isIdentifierPart || isSimplifiedID || part == "" {
-			continue
-		}
-
-		if strings.Contains(nextPart, "{") {
-			part = flect.Singularize(part)
-		}
-
-		if originalPart == "new" || originalPart == "edit" {
-			resultParts = append([]string{part}, resultParts...)
-			continue
-		}
-
-		if strings.Contains(previousPart, "}") {
-			resultParts = append(resultParts, part)
-			continue
-		}
-
-		resultParts = append(resultParts, part)
-	}
-
-	if len(resultParts) == 0 {
-		return "unnamed"
-	}
-
-	underscore := strings.TrimSpace(strings.Join(resultParts, "_"))
-	return name.VarCase(underscore)
-}
-
 func stripAsset(path string, h http.Handler, a *App) http.Handler {
 	if path == "" {
 		return h
diff --git a/routenamer.go b/routenamer.go
new file mode 100644
index 000000000..b6d28058d
--- /dev/null
+++ b/routenamer.go
@@ -0,0 +1,74 @@
+package buffalo
+
+import (
+	"strings"
+
+	"github.com/gobuffalo/flect"
+	"github.com/gobuffalo/flect/name"
+)
+
+// RouteNamer is in charge of naming a route from the
+// path assigned, this name typically will be used if no
+// name is assined with .Name(...).
+type RouteNamer interface {
+	// NameRoute receives the path and returns the name
+	// for the route.
+	NameRoute(string) string
+}
+
+// BaseRouteNamer is the default route namer used by apps.
+type baseRouteNamer struct{}
+
+func (drn baseRouteNamer) NameRoute(p string) string {
+	if p == "/" || p == "" {
+		return "root"
+	}
+
+	resultParts := []string{}
+	parts := strings.Split(p, "/")
+
+	for index, part := range parts {
+
+		originalPart := parts[index]
+
+		var previousPart string
+		if index > 0 {
+			previousPart = parts[index-1]
+		}
+
+		var nextPart string
+		if len(parts) > index+1 {
+			nextPart = parts[index+1]
+		}
+
+		isIdentifierPart := strings.Contains(part, "{") && (strings.Contains(part, flect.Singularize(previousPart)))
+		isSimplifiedID := part == `{id}`
+
+		if isIdentifierPart || isSimplifiedID || part == "" {
+			continue
+		}
+
+		if strings.Contains(nextPart, "{") {
+			part = flect.Singularize(part)
+		}
+
+		if originalPart == "new" || originalPart == "edit" {
+			resultParts = append([]string{part}, resultParts...)
+			continue
+		}
+
+		if strings.Contains(previousPart, "}") {
+			resultParts = append(resultParts, part)
+			continue
+		}
+
+		resultParts = append(resultParts, part)
+	}
+
+	if len(resultParts) == 0 {
+		return "unnamed"
+	}
+
+	underscore := strings.TrimSpace(strings.Join(resultParts, "_"))
+	return name.VarCase(underscore)
+}


Test patch:
diff --git a/router_test.go b/router_test.go
index 11fca3f7a..b41a33893 100644
--- a/router_test.go
+++ b/router_test.go
@@ -740,7 +740,7 @@ func Test_buildRouteName(t *testing.T) {
 	a := New(Options{})
 
 	for input, result := range cases {
-		fResult := a.buildRouteName(input)
+		fResult := a.RouteNamer.NameRoute(input)
 		r.Equal(result, fResult, input)
 	}
 
@@ -751,7 +751,7 @@ func Test_buildRouteName(t *testing.T) {
 	}
 
 	for input, result := range cases {
-		fResult := a.buildRouteName(input)
+		fResult := a.RouteNamer.NameRoute(input)
 		r.Equal(result, fResult, input)
 	}
 }