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

Support localized README #20508

Merged
merged 7 commits into from
Jul 31, 2022
Merged
Changes from 1 commit
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
31 changes: 26 additions & 5 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,34 @@ func renderDirectory(ctx *context.Context, treeLink string) {
renderReadmeFile(ctx, readmeFile, readmeTreelink)
}

// Note: This will always return lower-case strings
func localizedExtensions(ext string, languageCode string) (localizedExts []string) {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if len(languageCode) < 1 {
return []string{ext}
}

lowerLangCode := "." + strings.ToLower(languageCode)

if strings.Contains(lowerLangCode, "-") {
underscoreLangCode := strings.ReplaceAll(lowerLangCode, "-", "_")
indexOfDash := strings.Index(lowerLangCode, "-")
// e.g. [.zh-cn.md, .zh_cn.md, .zh.md, .md]
return []string{lowerLangCode + ext, underscoreLangCode + ext, lowerLangCode[:indexOfDash] + ext, ext}
}

// e.g. [.en.md, .md]
return []string{lowerLangCode + ext, ext}
}

func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string) (*namedBlob, string) {
// 3 for the extensions in exts[] in order
// 3 kinds of extensions in exts[] in order
// the last one is for a readme that doesn't
// strictly match an extension
var readmeFiles [4]*namedBlob
var docsEntries [3]*git.TreeEntry
exts := []string{".md", ".txt", ""} // sorted by priority
exts := append(localizedExtensions(".md", ctx.Language()), ".txt", "") // sorted by priority
extCount := len(exts)
readmeFiles := make([]*namedBlob, extCount+1)
docsEntries := make([]*git.TreeEntry, extCount)

for _, entry := range entries {
if entry.IsDir() {
lowerName := strings.ToLower(entry.Name())
Expand Down Expand Up @@ -225,7 +246,7 @@ func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string)
}
}
if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
readmeFiles[3] = &namedBlob{
readmeFiles[extCount] = &namedBlob{
name,
isSymlink,
entry.Blob(),
Expand Down