github.com/jmooring/hugo@v0.47.1/i18n/translationProvider.go (about) 1 // Copyright 2017 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package i18n 15 16 import ( 17 "errors" 18 "fmt" 19 20 "github.com/gohugoio/hugo/helpers" 21 22 "github.com/gohugoio/hugo/deps" 23 "github.com/gohugoio/hugo/source" 24 "github.com/nicksnyder/go-i18n/i18n/bundle" 25 "github.com/nicksnyder/go-i18n/i18n/language" 26 ) 27 28 // TranslationProvider provides translation handling, i.e. loading 29 // of bundles etc. 30 type TranslationProvider struct { 31 t Translator 32 } 33 34 // NewTranslationProvider creates a new translation provider. 35 func NewTranslationProvider() *TranslationProvider { 36 return &TranslationProvider{} 37 } 38 39 // Update updates the i18n func in the provided Deps. 40 func (tp *TranslationProvider) Update(d *deps.Deps) error { 41 sp := source.NewSourceSpec(d.PathSpec, d.BaseFs.SourceFilesystems.I18n.Fs) 42 src := sp.NewFilesystem("") 43 44 i18nBundle := bundle.New() 45 46 en := language.GetPluralSpec("en") 47 if en == nil { 48 return errors.New("The English language has vanished like an old oak table!") 49 } 50 var newLangs []string 51 52 for _, r := range src.Files() { 53 currentSpec := language.GetPluralSpec(r.BaseFileName()) 54 if currentSpec == nil { 55 // This may is a language code not supported by go-i18n, it may be 56 // Klingon or ... not even a fake language. Make sure it works. 57 newLangs = append(newLangs, r.BaseFileName()) 58 } 59 } 60 61 if len(newLangs) > 0 { 62 language.RegisterPluralSpec(newLangs, en) 63 } 64 65 // The source files are ordered so the most important comes first. Since this is a 66 // last key win situation, we have to reverse the iteration order. 67 files := src.Files() 68 for i := len(files) - 1; i >= 0; i-- { 69 if err := addTranslationFile(i18nBundle, files[i]); err != nil { 70 return err 71 } 72 } 73 74 tp.t = NewTranslator(i18nBundle, d.Cfg, d.Log) 75 76 d.Translate = tp.t.Func(d.Language.Lang) 77 78 return nil 79 80 } 81 82 func addTranslationFile(bundle *bundle.Bundle, r source.ReadableFile) error { 83 f, err := r.Open() 84 if err != nil { 85 return fmt.Errorf("Failed to open translations file %q: %s", r.LogicalName(), err) 86 } 87 defer f.Close() 88 err = bundle.ParseTranslationFileBytes(r.LogicalName(), helpers.ReaderToBytes(f)) 89 if err != nil { 90 return fmt.Errorf("Failed to load translations in file %q: %s", r.LogicalName(), err) 91 } 92 return nil 93 } 94 95 // Clone sets the language func for the new language. 96 func (tp *TranslationProvider) Clone(d *deps.Deps) error { 97 d.Translate = tp.t.Func(d.Language.Lang) 98 99 return nil 100 }