2016-05-14 04:35:16 +00:00
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tpl
import (
"github.com/nicksnyder/go-i18n/i18n/bundle"
2016-08-09 09:41:56 +00:00
"github.com/spf13/hugo/helpers"
2016-05-14 04:35:16 +00:00
jww "github.com/spf13/jwalterweatherman"
2016-08-09 09:41:56 +00:00
"github.com/spf13/viper"
)
var (
2016-11-05 16:27:40 +00:00
// Logi18nWarnings set to true to print warnings about missing language strings
2016-08-09 09:41:56 +00:00
Logi18nWarnings bool
i18nWarningLogger = helpers . NewDistinctFeedbackLogger ( )
2016-10-24 11:45:30 +00:00
currentLanguage * helpers . Language
2016-05-14 04:35:16 +00:00
)
2016-07-26 12:44:37 +00:00
type translate struct {
translateFuncs map [ string ] bundle . TranslateFunc
2016-05-14 04:35:16 +00:00
2016-07-26 12:44:37 +00:00
current bundle . TranslateFunc
}
2016-09-08 14:04:04 +00:00
var translator * translate
2016-07-26 12:44:37 +00:00
// SetTranslateLang sets the translations language to use during template processing.
// This construction is unfortunate, but the template system is currently global.
2016-10-24 11:45:30 +00:00
func SetTranslateLang ( language * helpers . Language ) error {
currentLanguage = language
if f , ok := translator . translateFuncs [ language . Lang ] ; ok {
2016-09-08 14:04:04 +00:00
translator . current = f
2016-08-09 09:41:56 +00:00
} else {
2016-10-24 11:45:30 +00:00
jww . WARN . Printf ( "Translation func for language %v not found, use default." , language . Lang )
2016-10-24 18:56:00 +00:00
translator . current = translator . translateFuncs [ viper . GetString ( "defaultContentLanguage" ) ]
2016-07-26 12:44:37 +00:00
}
2016-07-26 17:04:10 +00:00
return nil
2016-07-26 12:44:37 +00:00
}
2016-11-05 16:27:40 +00:00
// SetI18nTfuncs sets the language bundle to be used for i18n.
2016-07-26 12:44:37 +00:00
func SetI18nTfuncs ( bndl * bundle . Bundle ) {
2016-09-08 14:04:04 +00:00
translator = & translate { translateFuncs : make ( map [ string ] bundle . TranslateFunc ) }
2016-10-24 18:56:00 +00:00
defaultContentLanguage := viper . GetString ( "defaultContentLanguage" )
2016-08-09 09:41:56 +00:00
var (
defaultT bundle . TranslateFunc
err error
)
defaultT , err = bndl . Tfunc ( defaultContentLanguage )
if err != nil {
jww . WARN . Printf ( "No translation bundle found for default language %q" , defaultContentLanguage )
}
2016-11-06 23:10:32 +00:00
enableMissingTranslationPlaceholders := viper . GetBool ( "enableMissingTranslationPlaceholders" )
2016-07-26 12:44:37 +00:00
for _ , lang := range bndl . LanguageTags ( ) {
2016-08-09 09:41:56 +00:00
currentLang := lang
2016-09-08 14:04:04 +00:00
translator . translateFuncs [ currentLang ] = func ( translationID string , args ... interface { } ) string {
2016-09-18 20:18:13 +00:00
tFunc , err := bndl . Tfunc ( currentLang )
if err != nil {
jww . WARN . Printf ( "could not load translations for language %q (%s), will use default content language.\n" , lang , err )
} else if translated := tFunc ( translationID , args ... ) ; translated != translationID {
2016-08-09 09:41:56 +00:00
return translated
}
if Logi18nWarnings {
i18nWarningLogger . Printf ( "i18n|MISSING_TRANSLATION|%s|%s" , currentLang , translationID )
}
2016-11-06 23:10:32 +00:00
if enableMissingTranslationPlaceholders {
2016-11-18 21:38:41 +00:00
return "[i18n] " + translationID
2016-09-18 20:18:13 +00:00
}
2016-08-09 09:41:56 +00:00
if defaultT != nil {
2016-09-09 19:21:16 +00:00
if translated := defaultT ( translationID , args ... ) ; translated != translationID {
return translated
}
2016-08-09 09:41:56 +00:00
}
2016-09-18 20:18:13 +00:00
return ""
2016-08-09 09:41:56 +00:00
}
2016-05-14 04:35:16 +00:00
}
}
2016-11-05 16:27:40 +00:00
func i18nTranslate ( id string , args ... interface { } ) ( string , error ) {
2016-09-08 14:04:04 +00:00
if translator == nil || translator . current == nil {
helpers . DistinctErrorLog . Printf ( "i18n not initialized, check that you have language file (in i18n) that matches the site language or the default language." )
return "" , nil
2016-05-14 04:35:16 +00:00
}
2016-09-08 14:04:04 +00:00
return translator . current ( id , args ... ) , nil
2016-05-14 04:35:16 +00:00
}