Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Apr 30, 2020
1 parent 7a92a54 commit c44f67a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ os:
- linux
- osx
go:
- 1.13.x
- 1.14.x
go_import_path: github.com/kataras/i18n
env:
global:
- GO111MODULE=on
install:
- go get ./...
script:
- go test -vet=off -v -cover ./...
- go test -v -cover ./...
after_script:
# examples
- cd ./_examples
- go get ./...
- go test -vet=off -v -cover ./...
- go test -v -cover ./...
- cd ../
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Gerasimos Maropoulos <kataras2006@hotmail.com>
Copyright (c) 2019-2020 Gerasimos Maropoulos <kataras2006@hotmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Source code and other details for the project are available at GitHub:
Current Version
0.0.2
0.0.3
Installation
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kataras/i18n

go 1.13
go 1.14

require (
github.com/BurntSushi/toml v0.3.1
Expand Down
15 changes: 12 additions & 3 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package i18n

import (
"context"
"fmt"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -242,6 +241,13 @@ func parsePath(m *Matcher, path string) int {
return -1
}

func reverseStrings(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}

func parseLanguage(path string) (language.Tag, bool) {
if idx := strings.LastIndexByte(path, '.'); idx > 0 {
path = path[0:idx]
Expand All @@ -253,6 +259,8 @@ func parseLanguage(path string) (language.Tag, bool) {
return r == '_' || r == os.PathSeparator || r == '/' || r == '.'
})

names = reverseStrings(names)

for _, s := range names {
t, err := language.Parse(s)
if err != nil {
Expand Down Expand Up @@ -304,7 +312,7 @@ func (i *I18n) Tr(lang, format string, args ...interface{}) string {
return msg
}

return fmt.Sprintf(format, args...)
return ""
}

const acceptLanguageHeaderKey = "Accept-Language"
Expand Down Expand Up @@ -381,6 +389,7 @@ func GetMessage(r *http.Request, format string, args ...interface{}) string {
}

// GetMessage returns the localized text message for this "r" request based on the key "format".
// It returns an empty string if locale or format not found.
func (i *I18n) GetMessage(r *http.Request, format string, args ...interface{}) string {
loc := i.GetLocale(r)
if loc != nil {
Expand All @@ -392,7 +401,7 @@ func (i *I18n) GetMessage(r *http.Request, format string, args ...interface{}) s
return msg
}

return fmt.Sprintf(format, args...)
return ""
}

// Router is package-level function which calls the `Default.Router` method.
Expand Down
99 changes: 98 additions & 1 deletion loader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package i18n

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -72,7 +76,6 @@ func testLoadAndTrHelper(t *testing.T, i18N *I18n) {
if expected := "value"; got != expected {
t.Fatalf("expected %s but got %s", expected, got)
}

}

func TestLoadEmptyTags(t *testing.T) {
Expand All @@ -84,3 +87,97 @@ func TestLoadEmptyTags(t *testing.T) {
i18N.SetDefault("en-US")
testLoadAndTrHelper(t, i18N)
}

// https://github.com/kataras/i18n/issues/1
func TestLoadAbsDirWithPotentialLangCode(t *testing.T) {
dir := filepath.Join(os.TempDir(), "/opt/etc/rcacs/locales")
if err := createIfNotExists(dir, 0755); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})

if err := copyDir("./testfiles", dir); err != nil {
t.Fatalf("failed to copy testfiles to the temp dir: %s: %v", dir, err)
}

i18N, err := New(Glob(dir + "/*/*"))
if err != nil {
t.Fatal(err)
}

i18N.SetDefault("en-US")
testLoadAndTrHelper(t, i18N)
}

func copyDir(src, dest string) error {
entries, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
sourcePath := filepath.Join(src, entry.Name())
destPath := filepath.Join(dest, entry.Name())

fileInfo, err := os.Stat(sourcePath)
if err != nil {
return err
}

switch fileInfo.Mode() & os.ModeType {
case os.ModeDir:
if err := createIfNotExists(destPath, 0755); err != nil {
return err
}
if err := copyDir(sourcePath, destPath); err != nil {
return err
}
default:
if err := copyFile(sourcePath, destPath); err != nil {
return err
}
}

isSymlink := entry.Mode()&os.ModeSymlink != 0
if !isSymlink {
if err := os.Chmod(destPath, entry.Mode()); err != nil {
return err
}
}
}

return nil
}

func copyFile(srcFile, dstFile string) error {
out, err := os.Create(dstFile)
if err != nil {
return err
}

defer out.Close()

in, err := os.Open(srcFile)
defer in.Close()
if err != nil {
return err
}

_, err = io.Copy(out, in)
if err != nil {
return err
}

return nil
}

func createIfNotExists(dir string, perm os.FileMode) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, perm); err != nil {
return err
}
}

return nil
}

0 comments on commit c44f67a

Please sign in to comment.