Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify global config path #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions opencc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ import (

var (
// Dir is the parent dir for config and dictionary
Dir = flag.String("dir", defaultDir(), "dict dir")
configDir = "config"
dictDir = "dictionary"
Dir = flag.String("dir", defaultDir(), "dict dir")
configPath = ""
configDir = "config"
dictDir = "dictionary"
)

// SetConfigPath set global config path
func SetConfigPath(path string) {
configPath = path
}

func defaultDir() string {
if runtime.GOOS == "windows" {
return `C:\gocc\`
Expand Down Expand Up @@ -112,7 +118,12 @@ func (cc *OpenCC) initDict() error {
if cc.Conversion == "" {
return fmt.Errorf("conversion is not set")
}
configFile := filepath.Join(*Dir, configDir, cc.Conversion+".json")
var configFile string
if configPath != "" {
configFile = filepath.Join(configPath, configDir, cc.Conversion+".json")
} else {
configFile = filepath.Join(*Dir, configDir, cc.Conversion+".json")
}
body, err := ioutil.ReadFile(configFile)
if err != nil {
return err
Expand Down Expand Up @@ -192,7 +203,13 @@ func (cc *OpenCC) addDictChain(d map[string]interface{}) (*Group, error) {
if !has {
return nil, fmt.Errorf("no file field found")
}
daDict, err := da.BuildFromFile(filepath.Join(*Dir, dictDir, file.(string)))
var daFile string
if configPath != "" {
daFile = filepath.Join(configPath, dictDir, file.(string))
} else {
daFile = filepath.Join(*Dir, dictDir, file.(string))
}
daDict, err := da.BuildFromFile(daFile)
if err != nil {
return nil, err
}
Expand Down