MM-23706 search executable dir before working dir when looking for i18n (#14241)
* MM-23706 search executable dir before working dir when looking for i18n
Этот коммит содержится в:
@@ -17,7 +17,7 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bool) string {
|
||||
func findPath(path string, baseSearchPaths []string, workingDirFirst bool, filter func(os.FileInfo) bool) string {
|
||||
if filepath.IsAbs(path) {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path
|
||||
@@ -27,9 +27,12 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
|
||||
}
|
||||
|
||||
searchPaths := []string{}
|
||||
searchPaths = append(searchPaths, baseSearchPaths...)
|
||||
if workingDirFirst {
|
||||
searchPaths = append(searchPaths, baseSearchPaths...)
|
||||
}
|
||||
|
||||
// Additionally attempt to search relative to the location of the running binary.
|
||||
// Attempt to search relative to the location of the running binary either before
|
||||
// or after searching relative to the working directory, depending on `workingDirFirst`.
|
||||
var binaryDir string
|
||||
if exe, err := os.Executable(); err == nil {
|
||||
if exe, err = filepath.EvalSymlinks(exe); err == nil {
|
||||
@@ -47,6 +50,10 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
|
||||
}
|
||||
}
|
||||
|
||||
if !workingDirFirst {
|
||||
searchPaths = append(searchPaths, baseSearchPaths...)
|
||||
}
|
||||
|
||||
for _, parent := range searchPaths {
|
||||
found, err := filepath.Abs(filepath.Join(parent, path))
|
||||
if err != nil {
|
||||
@@ -65,6 +72,10 @@ func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bo
|
||||
return ""
|
||||
}
|
||||
|
||||
func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bool) string {
|
||||
return findPath(path, baseSearchPaths, true, filter)
|
||||
}
|
||||
|
||||
// FindFile looks for the given file in nearby ancestors relative to the current working
|
||||
// directory as well as the directory of the executable.
|
||||
func FindFile(path string) string {
|
||||
@@ -85,3 +96,15 @@ func FindDir(dir string) (string, bool) {
|
||||
|
||||
return found, true
|
||||
}
|
||||
|
||||
// FindDirRelBinary looks for the given directory in nearby ancestors relative to the
|
||||
// directory of the executable, then relative to the working directory, falling back to `./` if not found.
|
||||
func FindDirRelBinary(dir string) (string, bool) {
|
||||
found := findPath(dir, commonBaseSearchPaths, false, func(fileInfo os.FileInfo) bool {
|
||||
return fileInfo.IsDir()
|
||||
})
|
||||
if found == "" {
|
||||
return "./", false
|
||||
}
|
||||
return found, true
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func InitTranslations(localizationSettings model.LocalizationSettings) error {
|
||||
}
|
||||
|
||||
func InitTranslationsWithDir(dir string) error {
|
||||
i18nDirectory, found := fileutils.FindDir(dir)
|
||||
i18nDirectory, found := fileutils.FindDirRelBinary(dir)
|
||||
if !found {
|
||||
return fmt.Errorf("Unable to find i18n directory")
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user