Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
75a13761
Commit
75a13761
authored
Oct 07, 2018
by
Mikalai Radchuk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds support for `LC_MESSAGES` and `LC_ALL`
Implements the following locale lookup priority order: `LC_MESSAGES`, `LC_ALL`, `LANG`.
parent
709ac9ce
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
6 deletions
+108
-6
i18n.go
pkg/kubectl/util/i18n/i18n.go
+11
-2
i18n_test.go
pkg/kubectl/util/i18n/i18n_test.go
+97
-4
No files found.
pkg/kubectl/util/i18n/i18n.go
View file @
75a13761
...
@@ -50,9 +50,18 @@ var knownTranslations = map[string][]string{
...
@@ -50,9 +50,18 @@ var knownTranslations = map[string][]string{
}
}
func
loadSystemLanguage
()
string
{
func
loadSystemLanguage
()
string
{
langStr
:=
os
.
Getenv
(
"LANG"
)
// Implements the following locale priority order: LC_ALL, LC_MESSAGES, LANG
// Similarly to: https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
langStr
:=
os
.
Getenv
(
"LC_ALL"
)
if
langStr
==
""
{
if
langStr
==
""
{
glog
.
V
(
3
)
.
Infof
(
"Couldn't find the LANG environment variable, defaulting to en_US"
)
langStr
=
os
.
Getenv
(
"LC_MESSAGES"
)
}
if
langStr
==
""
{
langStr
=
os
.
Getenv
(
"LANG"
)
}
if
langStr
==
""
{
glog
.
V
(
3
)
.
Infof
(
"Couldn't find the LC_ALL, LC_MESSAGES or LANG environment variables, defaulting to en_US"
)
return
"default"
return
"default"
}
}
pieces
:=
strings
.
Split
(
langStr
,
"."
)
pieces
:=
strings
.
Split
(
langStr
,
"."
)
...
...
pkg/kubectl/util/i18n/i18n_test.go
View file @
75a13761
...
@@ -21,6 +21,8 @@ import (
...
@@ -21,6 +21,8 @@ import (
"testing"
"testing"
)
)
var
knownTestLocale
=
"en_US.UTF-8"
func
TestTranslation
(
t
*
testing
.
T
)
{
func
TestTranslation
(
t
*
testing
.
T
)
{
err
:=
LoadTranslations
(
"test"
,
func
()
string
{
return
"default"
})
err
:=
LoadTranslations
(
"test"
,
func
()
string
{
return
"default"
})
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -50,15 +52,106 @@ func TestTranslationPlural(t *testing.T) {
...
@@ -50,15 +52,106 @@ func TestTranslationPlural(t *testing.T) {
}
}
}
}
func
TestTranslationEnUSEnv
(
t
*
testing
.
T
)
{
func
TestTranslationUsingEnvVar
(
t
*
testing
.
T
)
{
os
.
Setenv
(
"LANG"
,
"en_US.UTF-8"
)
// We must backup and restore env vars before setting test values in tests
// othervise we are risking to break other tests/test cases
// which rely on the same env vars
envVarsToBackup
:=
[]
string
{
"LC_MESSAGES"
,
"LANG"
,
"LC_ALL"
}
expectedStrEnUSLocale
:=
"baz"
expectedStrFallback
:=
"foo"
testCases
:=
[]
struct
{
name
string
setenvFn
func
()
expectedStr
string
}{
{
name
:
"Only LC_ALL is set"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_ALL"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"Only LC_MESSAGES is set"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_MESSAGES"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"Only LANG"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LANG"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"LC_MESSAGES overrides LANG"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LANG"
,
"be_BY.UTF-8"
)
// Unknown locale
os
.
Setenv
(
"LC_MESSAGES"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"LC_ALL overrides LANG"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LANG"
,
"be_BY.UTF-8"
)
// Unknown locale
os
.
Setenv
(
"LC_ALL"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"LC_ALL overrides LC_MESSAGES"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_MESSAGES"
,
"be_BY.UTF-8"
)
// Unknown locale
os
.
Setenv
(
"LC_ALL"
,
knownTestLocale
)
},
expectedStr
:
expectedStrEnUSLocale
,
},
{
name
:
"Unknown locale in LANG"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LANG"
,
"be_BY.UTF-8"
)
},
expectedStr
:
expectedStrFallback
,
},
{
name
:
"Unknown locale in LC_MESSAGES"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_MESSAGES"
,
"be_BY.UTF-8"
)
},
expectedStr
:
expectedStrFallback
,
},
{
name
:
"Unknown locale in LC_ALL"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_ALL"
,
"be_BY.UTF-8"
)
},
expectedStr
:
expectedStrFallback
,
},
{
name
:
"Invalid env var"
,
setenvFn
:
func
()
{
os
.
Setenv
(
"LC_MESSAGES"
,
"fake.locale.UTF-8"
)
},
expectedStr
:
expectedStrFallback
,
},
{
name
:
"No env vars"
,
setenvFn
:
func
()
{},
expectedStr
:
expectedStrFallback
,
},
}
for
_
,
test
:=
range
testCases
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
for
_
,
envVar
:=
range
envVarsToBackup
{
if
envVarValue
:=
os
.
Getenv
(
envVar
);
envVarValue
!=
""
{
os
.
Unsetenv
(
envVar
)
// Restore env var at the end
defer
func
()
{
os
.
Setenv
(
envVar
,
envVarValue
)
}()
}
}
test
.
setenvFn
()
err
:=
LoadTranslations
(
"test"
,
nil
)
err
:=
LoadTranslations
(
"test"
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
}
result
:=
T
(
"test_string"
)
result
:=
T
(
"test_string"
)
if
result
!=
"baz"
{
if
result
!=
test
.
expectedStr
{
t
.
Errorf
(
"expected: %s, saw: %s"
,
"baz"
,
result
)
t
.
Errorf
(
"expected: %s, saw: %s"
,
test
.
expectedStr
,
result
)
}
})
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment