Skip to content

Commit

Permalink
Merge pull request #61 from zquestz/language_hooks
Browse files Browse the repository at this point in the history
Added hooks for devs to get region and language
  • Loading branch information
zquestz committed Jan 29, 2016
2 parents b7351ad + 94a9ec8 commit b05f351
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
7 changes: 6 additions & 1 deletion providers/amazon/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ type Provider struct{}

// BuildURI generates a search URL for Amazon.
func (p *Provider) BuildURI(q string) string {
return fmt.Sprintf("https://www.amazon.com/s/?keywords=%s", url.QueryEscape(q))
switch providers.Region() {
case "GB":
return fmt.Sprintf("https://www.amazon.co.uk/s/?keywords=%s", url.QueryEscape(q))
default:
return fmt.Sprintf("https://www.amazon.com/s/?keywords=%s", url.QueryEscape(q))
}
}
48 changes: 48 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package providers

import (
"fmt"
"os"
"regexp"
"sort"
"strings"

"github.com/zquestz/s/launcher"
"golang.org/x/text/language"
)

const (
defaultLanguage = "en"
defaultRegion = "US"
)

// Provider interface provides a way to build the URI
Expand Down Expand Up @@ -95,3 +102,44 @@ func ProviderNames() []string {
sort.Strings(names)
return names
}

// Region returns the users region code.
// Eg. "US", "GB", etc
func Region() string {
l := locale()

tag, err := language.Parse(l)
if err != nil {
return defaultRegion
}

region, _ := tag.Region()

return region.String()
}

// Language returns the users language code.
// Eg. "en", "es", etc
func Language() string {
l := locale()

tag, err := language.Parse(l)
if err != nil {
return defaultLanguage
}

base, _ := tag.Base()

return base.String()
}

func locale() string {
lang := os.Getenv("LANG")
if lang == "" {
return ""
}

locale := strings.Split(lang, ".")[0]

return locale
}

0 comments on commit b05f351

Please sign in to comment.