Skip to content
Browse files

Fixed minor issues found in pull request

  • Loading branch information...
1 parent 0788753 commit ae0235fb059ae5491712f465bc30f58f115b8902 @tmbrggmn tmbrggmn committed Jan 20, 2013
Showing with 21 additions and 25 deletions.
  1. +2 −2 controller.go
  2. +11 −15 i18n.go
  3. +6 −6 i18n_test.go
  4. +1 −1 mvc.go
  5. +1 −1 samples/i18n/app/controllers/app.go
View
4 controller.go
@@ -29,7 +29,7 @@ type Controller struct {
Args map[string]interface{} // Per-request scratch space.
RenderArgs map[string]interface{} // Args passed to the template.
Validation *Validation // Data validation helpers
- Txn *sql.Tx // Nil by default, but may be used by the app / plugins
+ Txn *sql.Tx // Nil by default, but may be used by the app / plugins
}
func NewController(req *Request, resp *Response, ct *ControllerType) *Controller {
@@ -284,5 +284,5 @@ func (c *Controller) Redirect(val interface{}, args ...interface{}) Result {
//
// The current language is set by the i18n plugin.
func (c *Controller) Message(message string, args ...interface{}) (value string) {
- return Message(c.Request.CurrentLocale, message, args...)
+ return Message(c.Request.Locale, message, args...)
}
View
26 i18n.go
@@ -11,13 +11,13 @@ import (
)
const (
- CurrentLocaleRenderArg string = "currentLocale" // The key for the current locale render arg value
+ CurrentLocaleRenderArg = "currentLocale" // The key for the current locale render arg value
- messageFilesDirectory string = "messages"
- messageFilePattern string = `^\w+.[a-zA-Z]{2}$`
- unknownValueFormat string = "??? %s ???"
- defaultLanguageOption string = "i18n.default_language"
- localeCookieConfigKey string = "i18n.cookie"
+ messageFilesDirectory = "messages"
+ messageFilePattern = `^\w+.[a-zA-Z]{2}$`
+ unknownValueFormat = "??? %s ???"
+ defaultLanguageOption = "i18n.default_language"
+ localeCookieConfigKey = "i18n.cookie"
)
var (
@@ -39,7 +39,7 @@ func MessageLanguages() []string {
// Perform a message look-up for the given locale and message using the given arguments.
//
// When either an unknown locale or message is detected, a specially formatted string is returned.
-func Message(locale, message string, args ...interface{}) (value string) {
+func Message(locale, message string, args ...interface{}) string {
language, region := parseLocale(locale)
TRACE.Printf("Resolving message '%s' for language '%s' and region '%s'", message, language, region)
@@ -69,7 +69,7 @@ func Message(locale, message string, args ...interface{}) (value string) {
return fmt.Sprintf(unknownValueFormat, message)
}
- if args != nil && len(args) > 0 {
+ if len(args) > 0 {
TRACE.Printf("Arguments detected, formatting '%s' with %v", value, args)
value = fmt.Sprintf(value, args...)
}
@@ -86,13 +86,9 @@ func parseLocale(locale string) (language, region string) {
return locale, ""
}
+// Load the messages when Revel boots.
func init() {
- InitHooks = append(InitHooks, loadMessagesOnInitialize)
-}
-
-// Wrap loadMessages() in a function so it can be hooked into InitHooks.
-func loadMessagesOnInitialize() {
- loadMessages(path.Join(BasePath, messageFilesDirectory))
+ InitHooks = append(InitHooks, func() { loadMessages(path.Join(BasePath, messageFilesDirectory)) })
}
// Recursively read and cache all available messages from all message files on the given path.
@@ -165,7 +161,7 @@ func (p I18nPlugin) BeforeRequest(c *Controller) {
// Set the current locale controller argument (CurrentLocaleControllerArg) with the given locale.
func setCurrentLocaleControllerArguments(c *Controller, locale string) {
- c.Request.CurrentLocale = locale
+ c.Request.Locale = locale
c.RenderArgs[CurrentLocaleRenderArg] = locale
}
View
12 i18n_test.go
@@ -116,18 +116,18 @@ func TestBeforeRequest(t *testing.T) {
plugin := I18nPlugin{}
controller := NewController(buildEmptyRequest(), nil, &ControllerType{reflect.TypeOf(Controller{}), nil})
- if plugin.BeforeRequest(controller); controller.Request.CurrentLocale != "" {
- t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "", controller.Request.CurrentLocale)
+ if plugin.BeforeRequest(controller); controller.Request.Locale != "" {
+ t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "", controller.Request.Locale)
}
controller = NewController(buildRequestWithCookie("REVEL_LANG", "en-US"), nil, &ControllerType{reflect.TypeOf(Controller{}), nil})
- if plugin.BeforeRequest(controller); controller.Request.CurrentLocale != "en-US" {
- t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-US", controller.Request.CurrentLocale)
+ if plugin.BeforeRequest(controller); controller.Request.Locale != "en-US" {
+ t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-US", controller.Request.Locale)
}
controller = NewController(buildRequestWithAcceptLanguages("en-GB", "en-US"), nil, &ControllerType{reflect.TypeOf(Controller{}), nil})
- if plugin.BeforeRequest(controller); controller.Request.CurrentLocale != "en-GB" {
- t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-GB", controller.Request.CurrentLocale)
+ if plugin.BeforeRequest(controller); controller.Request.Locale != "en-GB" {
+ t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-GB", controller.Request.Locale)
}
}
View
2 mvc.go
@@ -15,7 +15,7 @@ type Request struct {
ContentType string
Format string // "html", "xml", "json", or "text"
AcceptLanguages AcceptLanguages
- CurrentLocale string
+ Locale string
}
type Response struct {
View
2 samples/i18n/app/controllers/app.go
@@ -11,7 +11,7 @@ func (c Application) Index() rev.Result {
c.RenderArgs["acceptLanguageHeader"] = c.Request.Header.Get("Accept-Language")
c.RenderArgs["acceptLanguageHeaderParsed"] = c.Request.AcceptLanguages.String()
c.RenderArgs["acceptLanguageHeaderMostQualified"] = c.Request.AcceptLanguages[0]
- c.RenderArgs["controllerCurrentLocale"] = c.Request.CurrentLocale
+ c.RenderArgs["controllerCurrentLocale"] = c.Request.Locale
// Controller-resolves messages
c.RenderArgs["controllerGreeting"] = c.Message("greeting")

0 comments on commit ae0235f

Please sign in to comment.
Something went wrong with that request. Please try again.