Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Add internationalization (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyxPractice authored Jul 28, 2022
1 parent 6b9ffd5 commit 4097d79
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 35 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ require (
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/imroc/req/v3 v3.14.1
github.com/nicksnyder/go-i18n/v2 v2.2.0
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/cubiest/jibberjabber v1.0.1
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
Expand Down Expand Up @@ -47,7 +51,6 @@ require (
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.11 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
Expand All @@ -33,6 +35,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cubiest/jibberjabber v1.0.1 h1:JxKC9EZcdw8azEbyWaNj62ppPMkbFJBf2ayPY1vBeDI=
github.com/cubiest/jibberjabber v1.0.1/go.mod h1:Ovt9ZAmzAgwQ8cWgvZ1se9oaGYzjHrlAXKM3NOzlQOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -98,9 +102,11 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucas-clemente/quic-go v0.28.0 h1:9eXVRgIkMQQyiyorz/dAaOYIx3TFzXsIFkNFz4cxuJM=
Expand Down Expand Up @@ -147,6 +153,8 @@ github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1E
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/nicksnyder/go-i18n/v2 v2.2.0 h1:MNXbyPvd141JJqlU6gJKrczThxJy+kdCNivxZpBQFkw=
github.com/nicksnyder/go-i18n/v2 v2.2.0/go.mod h1:4OtLfzqyAxsscyCb//3gfqSvBc81gImX91LrZzczN1o=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down Expand Up @@ -354,6 +362,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
Expand Down
14 changes: 12 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package config

import (
"fmt"

"github.com/victorbersy/docker-hub-cli/internal/config/locales"
"github.com/victorbersy/docker-hub-cli/internal/ui/styles"
)

type ViewType string

const (
Expand Down Expand Up @@ -33,6 +40,9 @@ type Config struct {
}

func GetDefaultConfig() Config {
localizer := locales.NewLocales()
explore_title := localizer.L("tab_explore_title")
my_repos_title := localizer.L("tab_my_repos_title")
return Config{
Defaults: Defaults{
Preview: PreviewConfig{
Expand All @@ -43,11 +53,11 @@ func GetDefaultConfig() Config {
},
Views: []ViewConfig{
{
Title: " Explore",
Title: fmt.Sprint(styles.DefaultGlyphs.TabExplore, " ", explore_title),
Type: ExploreView,
},
{
Title: " My Repositories",
Title: fmt.Sprint(styles.DefaultGlyphs.TabMyRepos, " ", my_repos_title),
Type: MyReposView,
},
},
Expand Down
81 changes: 81 additions & 0 deletions internal/config/locales/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
startup_reading:
description:
other: "Reading config..."
startup_no_views_defined:
description:
other: "No views defined..."

tab_explore_title:
description: "Top screen tab name"
other: "Explore"
tab_my_repos_title:
description: "Top screen tab name"
other: "My repositories"

explore_repositories_item_type_label:
description: "Label name used to identify the resource, like to count them"
other: "Repositories"
explore_repositories_not_found:
description: "Displayed when none are found"
other: "No repositories found."
explore_repositories_fetching:
description: "Displayed when fetching repositories"
other: "Fetching repositories..."

explore_sidebar_architectures:
description: "Explore sidebar architectures title"
other: "Architectures"
explore_sidebar_how_to_pull_instructions:
description: "Instructions on how to pull the selected image"
other: "How to pull \"{{.Name}}\"?"

my_repositories_item_type_label:
description: "Label name used to identify the resource, like to count them"
other: "Repositories"
my_repositories_not_found:
description: "Displayed when none are found"
other: "No repositories found."
my_repositories_not_found_tip:
description: "Displayed when none are found"
other: "Have you set DOCKER_USERNAME and DOCKER_BEARER?"
my_repositories_fetching:
description: "Displayed when fetching repositories"
other: "Fetching repositories..."

my_repos_sidebar_visibility:
description: ""
other: "Visibility"
my_repos_sidebar_visibility_private:
description: ""
other: "Private"
my_repos_sidebar_visibility_public:
description: ""
other: "Public"
my_repos_sidebar_stats:
description: ""
other: "Stats"
my_repos_sidebar_timestamps:
description: ""
other: "Timestamps"
my_repos_sidebar_updated_at:
description: ""
other: "Last Update"
my_repos_sidebar_created_at:
description: ""
other: "Created At"

column_header_name:
description: "Common table column header"
other: "Name"
column_header_organization:
description: "Common table column header"
other: "Organization"
column_header_updated_at:
description: "Common table column header"
other: "Updated At"
column_header_created_at:
description: "Common table column header"
other: "Created At"
column_header_description:
description: "Common table column header"
other: "Description"
81 changes: 81 additions & 0 deletions internal/config/locales/fr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
startup_reading:
description:
other: "Récupération de la configuration..."
startup_no_views_defined:
description:
other: "Aucune vues trouvées..."

tab_explore_title:
description: "Top screen tab name"
other: "Explorer"
tab_my_repos_title:
description: "Top screen tab name"
other: "Mes dépôts"

explore_repositories_item_type_label:
description: "Label name used to identify the resource, like to count them"
other: "Dépôts"
explore_repositories_not_found:
description: "Displayed when none are found"
other: "Aucun dépôt trouvé."
explore_repositories_fetching:
description: "Displayed when fetching repositories"
other: "Récupération des dépôts..."

explore_sidebar_architectures:
description: "Explore sidebar architectures title"
other: "Architectures"
explore_sidebar_how_to_pull_instructions:
description: "Instructions on how to pull the selected image"
other: "Comment obtenir \"{{.Name}}\" ?"

my_repositories_item_type_label:
description: "Label name used to identify the resource, like to count them"
other: "Dépôts"
my_repositories_not_found:
description: "Displayed when none are found"
other: "Aucun de vos dépôts n'a été trouvé."
my_repositories_not_found_tip:
description: "Displayed when none are found"
other: "Avez-vous configuré DOCKER_USERNAME et DOCKER_BEARER ?"
my_repositories_fetching:
description: "Displayed when fetching repositories"
other: "Récupération des dépôts..."

my_repos_sidebar_visibility:
description: ""
other: "Visibilité"
my_repos_sidebar_visibility_private:
description: ""
other: "Privé"
my_repos_sidebar_visibility_public:
description: ""
other: "Publique"
my_repos_sidebar_stats:
description: ""
other: "Stats"
my_repos_sidebar_timestamps:
description: ""
other: "Dates"
my_repos_sidebar_updated_at:
description: ""
other: "Dernière mise à jour"
my_repos_sidebar_created_at:
description: ""
other: "Créé le"

column_header_name:
description: "Column name used in explore view"
other: "Nom"
column_header_organization:
description: "Column name used in explore view"
other: "Société"
column_header_updated_at:
description: "Column name used in explore view"
other: "Dernière mise à jour"
column_header_created_at:
description: "Column name used in explore view"
other: "Créé le"
column_header_description:
description: "Column name used in explore view"
other: "Description"
40 changes: 40 additions & 0 deletions internal/config/locales/locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package locales

import (
"github.com/cubiest/jibberjabber"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)

var default_language = language.English

type Locales struct {
Localizer *i18n.Localizer
}

func NewLocales() Locales {
return Locales{
Localizer: getLocalizer(),
}
}

func getLocalizer() *i18n.Localizer {
bundle := i18n.NewBundle(default_language)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
bundle.MustLoadMessageFile("./internal/config/locales/en.yaml")
bundle.MustLoadMessageFile("./internal/config/locales/fr.yaml")
return i18n.NewLocalizer(bundle, getLanguage().String(), default_language.String())
}

func getLanguage() language.Tag {
userLanguage, err := jibberjabber.DetectLanguage()
if err != nil {
return default_language
}
return language.Make(userLanguage)
}

func (locales Locales) L(msgId string) string {
return locales.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: msgId})
}
17 changes: 14 additions & 3 deletions internal/ui/components/sidebar/explore/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/nicksnyder/go-i18n/v2/i18n"
data_search "github.com/victorbersy/docker-hub-cli/internal/data/search"
repository_search "github.com/victorbersy/docker-hub-cli/internal/ui/components/repository/search"
"github.com/victorbersy/docker-hub-cli/internal/ui/components/sidebar"
"github.com/victorbersy/docker-hub-cli/internal/ui/context"
)

type Model struct {
repo *repository_search.Repository
width int
ctx *context.ProgramContext
}

func NewModel(data *data_search.Repository, width int) Model {
func NewModel(data *data_search.Repository, width int, ctx *context.ProgramContext) Model {
var r *repository_search.Repository
if data == nil {
r = nil
Expand All @@ -24,6 +27,7 @@ func NewModel(data *data_search.Repository, width int) Model {
return Model{
repo: r,
width: width,
ctx: ctx,
}
}

Expand Down Expand Up @@ -71,13 +75,14 @@ func (m *Model) renderDescription() string {
}

func (m *Model) renderArchs() string {
architecture_title := m.ctx.Localizer.L("explore_sidebar_architectures")
archs := []string{}
for _, arch := range m.repo.Data.Architectures {
archs = append(archs, archLabel.Render(arch.Name))
}
return lipgloss.JoinVertical(
lipgloss.Top,
archsTitle.Render("Archs"),
archsTitle.Render(architecture_title),
lipgloss.JoinHorizontal(
lipgloss.Top,
archs...,
Expand All @@ -86,10 +91,16 @@ func (m *Model) renderArchs() string {
}

func (m *Model) renderPullCmd() string {
howToPullInstructions := &i18n.Message{ID: "explore_sidebar_how_to_pull_instructions"}
howToPullInstructionsTemplate := map[string]interface{}{"Name": m.repo.Data.Slug, "Count": 1}
how_to_txt := m.ctx.Localizer.Localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: howToPullInstructions,
TemplateData: howToPullInstructionsTemplate,
})
cmd := fmt.Sprintf("$ docker pull %s", m.repo.Data.Slug)
return lipgloss.JoinVertical(
lipgloss.Top,
dockerPullCmdTitle.Render(fmt.Sprintf("How to pull %s?", m.repo.Data.Name)),
dockerPullCmdTitle.Render(how_to_txt),
dockerPullCmdBox.Copy().Render(cmd),
)
}
Loading

0 comments on commit 4097d79

Please sign in to comment.