This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Support dynamic locales #141
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,42 +4,45 @@ | |
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/mattermost/mattermost-server/model" | ||
"github.com/mattermost/mattermost-server/utils/fileutils" | ||
"github.com/nicksnyder/go-i18n/i18n" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var locales = make(map[string]string) | ||
|
||
func (p *Plugin) TranslationsPreInit() error { | ||
|
||
i18nDirectory, found := fileutils.FindDir(*p.ServerConfig.PluginSettings.Directory + "/" + manifest.Id + "/server/dist/i18n/") | ||
if !found { | ||
return fmt.Errorf("unable to find i18n directory") | ||
bundlePath, err := p.API.GetBundlePath() | ||
if err != nil { | ||
return errors.Wrap(err, "unable to find i18n directory") | ||
} | ||
|
||
files, _ := ioutil.ReadDir(i18nDirectory) | ||
i18nDirectory := path.Join(bundlePath, "assets", "i18n") | ||
files, err := ioutil.ReadDir(i18nDirectory) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to read i18n directory") | ||
} | ||
for _, f := range files { | ||
if filepath.Ext(f.Name()) == ".json" { | ||
filename := f.Name() | ||
locales[strings.Split(filename, ".")[0]] = filepath.Join(i18nDirectory, filename) | ||
|
||
filename := f.Name() | ||
if filepath.Ext(filename) == ".json" { | ||
if err := i18n.LoadTranslationFile(filepath.Join(i18nDirectory, filename)); err != nil { | ||
return err | ||
p.API.LogError("Failed to load translation file", "filename", filename, "err", err.Error()) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better continuing here and logging, nice. |
||
} | ||
|
||
p.API.LogDebug("Loaded translation file", "filename", filename) | ||
p.locales[strings.TrimSuffix(filename, filepath.Ext(filename))] = filepath.Join(i18nDirectory, filename) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetUserTranslations(locale string) i18n.TranslateFunc { | ||
if _, ok := locales[locale]; !ok { | ||
func (p *Plugin) GetUserTranslations(locale string) i18n.TranslateFunc { | ||
if _, ok := p.locales[locale]; !ok { | ||
locale = model.DEFAULT_LOCALE | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/mattermost/mattermost-server/plugin/plugintest" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTranslationsPreInit(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for adding test coverage on this. I learned some things from your style. |
||
tmpDir, err := ioutil.TempDir("", "TestTranslationsPreInit") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmpDir) | ||
|
||
assetsPath := filepath.Join(tmpDir, "assets") | ||
err = os.Mkdir(assetsPath, 0777) | ||
require.NoError(t, err) | ||
|
||
i18nPath := filepath.Join(tmpDir, "assets", "i18n") | ||
|
||
t.Run("failure to get bundle path", func(t *testing.T) { | ||
api := &plugintest.API{} | ||
api.On("GetBundlePath", mock.Anything).Return(tmpDir, nil) | ||
|
||
defer api.AssertExpectations(t) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
err := p.TranslationsPreInit() | ||
require.EqualError(t, err, fmt.Sprintf("unable to read i18n directory: open %s: no such file or directory", i18nPath)) | ||
}) | ||
|
||
t.Run("failure to read i18n directory", func(t *testing.T) { | ||
api := &plugintest.API{} | ||
api.On("GetBundlePath", mock.Anything).Return(tmpDir, nil) | ||
|
||
defer api.AssertExpectations(t) | ||
|
||
file, err := os.Create(i18nPath) | ||
require.NoError(t, err) | ||
file.Close() | ||
defer os.Remove(file.Name()) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
err = p.TranslationsPreInit() | ||
require.EqualError(t, err, fmt.Sprintf("unable to read i18n directory: readdirent: invalid argument")) | ||
}) | ||
|
||
t.Run("no i18n files", func(t *testing.T) { | ||
api := &plugintest.API{} | ||
api.On("GetBundlePath", mock.Anything).Return(tmpDir, nil) | ||
|
||
defer api.AssertExpectations(t) | ||
|
||
err := os.Mkdir(i18nPath, 0777) | ||
require.NoError(t, err) | ||
defer os.Remove(i18nPath) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
err = p.TranslationsPreInit() | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("various i18n files", func(t *testing.T) { | ||
api := &plugintest.API{} | ||
api.On("GetBundlePath", mock.Anything).Return(tmpDir, nil) | ||
api.On("LogError", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Maybe().Run(func(args mock.Arguments) { | ||
t.Helper() | ||
t.Log(args...) | ||
}) | ||
api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe().Run(func(args mock.Arguments) { | ||
t.Helper() | ||
t.Log(args...) | ||
}) | ||
|
||
defer api.AssertExpectations(t) | ||
|
||
err := os.Mkdir(i18nPath, 0777) | ||
require.NoError(t, err) | ||
|
||
err = ioutil.WriteFile(filepath.Join(i18nPath, "not-i18n.txt"), []byte{}, 0777) | ||
require.NoError(t, err) | ||
|
||
err = ioutil.WriteFile(filepath.Join(i18nPath, "invalid.json"), []byte{}, 0777) | ||
require.NoError(t, err) | ||
|
||
err = ioutil.WriteFile(filepath.Join(i18nPath, "en.json"), []byte(`[{"id":"id","translation":"translation"}]`), 0777) | ||
require.NoError(t, err) | ||
|
||
err = ioutil.WriteFile(filepath.Join(i18nPath, "es.json"), []byte(`[{"id":"id","translation":"translation2"}]`), 0777) | ||
require.NoError(t, err) | ||
|
||
p := NewPlugin() | ||
p.API = api | ||
err = p.TranslationsPreInit() | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, map[string]string{ | ||
"en": filepath.Join(i18nPath, "en.json"), | ||
"es": filepath.Join(i18nPath, "es.json"), | ||
}, p.locales) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome!