2017-05-07 02:32:32 +02:00
package languages
import (
2017-05-09 01:44:41 +02:00
"fmt"
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
"github.com/nicksnyder/go-i18n/i18n"
2017-05-10 21:45:39 +02:00
"github.com/ewhal/nyaa/service/user"
2017-05-07 23:05:41 +02:00
"html/template"
"net/http"
)
2017-05-07 02:32:32 +02:00
2017-05-09 20:24:37 +02:00
// When go-i18n finds a language with >0 translations, it uses it as the Tfunc
// However, if said language has a missing translation, it won't fallback to the "main" language
func TfuncWithFallback ( language string , languages ... string ) ( i18n . TranslateFunc , error ) {
// Use the last language on the args as the fallback one.
fallbackLanguage := language
if languages != nil {
fallbackLanguage = languages [ len ( languages ) - 1 ]
}
T , err1 := i18n . Tfunc ( language , languages ... )
fallbackT , err2 := i18n . Tfunc ( fallbackLanguage )
if err1 != nil && err2 != nil {
// fallbackT is still a valid function even with the error, it returns translationID.
return fallbackT , err2 ;
}
return func ( translationID string , args ... interface { } ) string {
if translated := T ( translationID , args ... ) ; translated != translationID {
return translated
}
return fallbackT ( translationID , args ... )
} , nil
}
2017-05-10 21:45:39 +02:00
func GetAvailableLanguages ( ) ( languages map [ string ] string ) {
languages = make ( map [ string ] string )
var T i18n . TranslateFunc
for _ , languageTag := range i18n . LanguageTags ( ) {
T , _ = i18n . Tfunc ( languageTag )
/ * Translation files should have an ID with the translated language name .
If they don ' t , just use the languageTag * /
if languageName := T ( "language_name" ) ; languageName != "language_name" {
languages [ languageTag ] = languageName ;
} else {
languages [ languageTag ] = languageTag
}
}
return
}
2017-05-09 17:47:06 +02:00
func SetTranslation ( tmpl * template . Template , language string , languages ... string ) i18n . TranslateFunc {
2017-05-09 20:24:37 +02:00
T , _ := TfuncWithFallback ( language , languages ... )
2017-05-07 23:05:41 +02:00
tmpl . Funcs ( map [ string ] interface { } {
2017-05-09 01:44:41 +02:00
"T" : func ( str string , args ... interface { } ) template . HTML {
return template . HTML ( fmt . Sprintf ( T ( str ) , args ... ) )
2017-05-07 23:05:41 +02:00
} ,
} )
2017-05-09 17:47:06 +02:00
return T
2017-05-07 23:05:41 +02:00
}
2017-05-09 17:47:06 +02:00
func SetTranslationFromRequest ( tmpl * template . Template , r * http . Request , defaultLanguage string ) i18n . TranslateFunc {
2017-05-10 21:45:39 +02:00
userLanguage := ""
user , _ , err := userService . RetrieveCurrentUser ( r )
if err == nil {
userLanguage = user . Language ;
}
2017-05-07 23:05:41 +02:00
cookie , err := r . Cookie ( "lang" )
cookieLanguage := ""
if err == nil {
cookieLanguage = cookie . Value
}
2017-05-10 21:45:39 +02:00
2017-05-07 23:05:41 +02:00
// go-i18n supports the format of the Accept-Language header, thankfully.
headerLanguage := r . Header . Get ( "Accept-Language" )
2017-05-10 21:45:39 +02:00
return SetTranslation ( tmpl , userLanguage , cookieLanguage , headerLanguage , defaultLanguage )
2017-05-07 23:05:41 +02:00
}