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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This tweaks the locale support in a couple of ways. First, we now locate locales in the root `assets/i18n` directory, resolved via `GetBundlePath`. This hardens the plugin against changes the server might make it how it unpacks the plugin, and simplifies the distribution of i18n resources for users. Secondly, we now register all locales found in the `assets/i18n` directory. This means that adding support for new locales simply requires dropping in the file without any need to modify the code or even recompile.
- Loading branch information
1 parent
e8cb152
commit 8507681
Showing
7 changed files
with
136 additions
and
23 deletions.
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
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) { | ||
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