-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust comment format and encapsulate the language-ctx method
- Loading branch information
Showing
11 changed files
with
166 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2021 Terminus, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctxhelper | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/erda-project/erda-infra/providers/i18n" | ||
) | ||
|
||
const languageKey = "language" | ||
const zh = "zh-CN" | ||
const en = "en-US" | ||
|
||
func GetAuditLanguage(ctx context.Context) (i18n.LanguageCodes, bool) { | ||
langCodes, ok := ctx.Value(languageKey).(i18n.LanguageCodes) | ||
if !ok { | ||
return nil, false | ||
} | ||
if langCodes == nil { | ||
return i18n.LanguageCodes{ | ||
&i18n.LanguageCode{ | ||
Code: zh, | ||
Quality: 1, | ||
}, | ||
}, true | ||
} | ||
return langCodes, true | ||
} | ||
|
||
func PutAuditLanguage(ctx context.Context, language i18n.LanguageCodes) context.Context { | ||
return context.WithValue(ctx, languageKey, language) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2021 Terminus, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctxhelper | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/erda-project/erda-infra/providers/i18n" | ||
) | ||
|
||
func TestAuditContext(t *testing.T) { | ||
|
||
type resp struct { | ||
language i18n.LanguageCodes | ||
flag bool | ||
} | ||
testCase := []struct { | ||
name string | ||
lang any | ||
want resp | ||
}{ | ||
{ | ||
name: "invalid language", | ||
lang: 1, | ||
want: resp{ | ||
language: nil, | ||
flag: false, | ||
}, | ||
}, | ||
{ | ||
name: "language is nil", | ||
lang: []*i18n.LanguageCode{}, | ||
want: resp{ | ||
language: nil, | ||
flag: false, | ||
}, | ||
}, | ||
{ | ||
name: "success get language", | ||
lang: i18n.LanguageCodes{ | ||
&i18n.LanguageCode{ | ||
Code: en, | ||
Quality: 1, | ||
}, | ||
}, | ||
want: resp{ | ||
language: i18n.LanguageCodes{ | ||
&i18n.LanguageCode{ | ||
Code: en, | ||
Quality: 1, | ||
}, | ||
}, | ||
flag: true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCase { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var lang i18n.LanguageCodes | ||
var flag bool | ||
if tt.lang == 1 { | ||
ctx := context.WithValue(context.Background(), languageKey, tt.lang) | ||
lang, flag = GetAuditLanguage(ctx) | ||
|
||
} else if tt.name == "language is nil" { | ||
lang, flag = GetAuditLanguage(context.Background()) | ||
} else { | ||
ctx := PutAuditLanguage(context.Background(), tt.lang.(i18n.LanguageCodes)) | ||
lang, flag = GetAuditLanguage(ctx) | ||
} | ||
assert.Equal(t, tt.want.language, lang) | ||
assert.Equal(t, tt.want.flag, flag) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters