locale.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "encoding/json"
  4. "math"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. "imuslab.com/arozos/mod/utils"
  11. )
  12. // localeFileSchema is the top-level structure shared by all locale JSON files.
  13. type localeFileSchema struct {
  14. Author string `json:"author"`
  15. Keys map[string]localeKeyEntry `json:"keys"`
  16. }
  17. type localeKeyEntry struct {
  18. Name string `json:"name"`
  19. }
  20. // InfoHandleGetLocaleInfo returns locale coverage statistics.
  21. // It reads every JSON file under ./web/SystemAO/locale/ (one level of sub-dirs),
  22. // uses file_explorer.json as the canonical language baseline, and reports:
  23. // - coverage % per language (how many files have that language)
  24. // - a deduplicated contributors list
  25. func InfoHandleGetLocaleInfo(w http.ResponseWriter, r *http.Request) {
  26. type LanguageEntry struct {
  27. Code string `json:"code"`
  28. Name string `json:"name"`
  29. FileCount int `json:"fileCount"`
  30. TotalFiles int `json:"totalFiles"`
  31. Coverage float64 `json:"coverage"`
  32. }
  33. type Response struct {
  34. Languages []LanguageEntry `json:"languages"`
  35. Contributors []string `json:"contributors"`
  36. TotalFiles int `json:"totalFiles"`
  37. }
  38. localeRoot := "./web/SystemAO/locale"
  39. // ── 1. Collect all JSON file paths (root + one level of sub-dirs) ──────────
  40. var jsonFiles []string
  41. entries, err := os.ReadDir(localeRoot)
  42. if err != nil {
  43. utils.SendErrorResponse(w, "Failed to read locale directory: "+err.Error())
  44. return
  45. }
  46. for _, e := range entries {
  47. if e.IsDir() {
  48. subEntries, err2 := os.ReadDir(filepath.Join(localeRoot, e.Name()))
  49. if err2 != nil {
  50. continue
  51. }
  52. for _, se := range subEntries {
  53. if !se.IsDir() && filepath.Ext(se.Name()) == ".json" {
  54. jsonFiles = append(jsonFiles, filepath.Join(localeRoot, e.Name(), se.Name()))
  55. }
  56. }
  57. } else if filepath.Ext(e.Name()) == ".json" {
  58. jsonFiles = append(jsonFiles, filepath.Join(localeRoot, e.Name()))
  59. }
  60. }
  61. // ── 2. Parse file_explorer.json to get the baseline language set ────────────
  62. baseData, err := os.ReadFile(filepath.Join(localeRoot, "file_explorer.json"))
  63. if err != nil {
  64. utils.SendErrorResponse(w, "Failed to read file_explorer.json: "+err.Error())
  65. return
  66. }
  67. var base localeFileSchema
  68. if err = json.Unmarshal(baseData, &base); err != nil {
  69. utils.SendErrorResponse(w, "Failed to parse file_explorer.json: "+err.Error())
  70. return
  71. }
  72. // Build a map: langCode -> display name (from file_explorer.json)
  73. langNames := make(map[string]string, len(base.Keys))
  74. for code, entry := range base.Keys {
  75. langNames[code] = entry.Name
  76. }
  77. // ── 3. Walk every JSON file, tally language presence and collect authors ────
  78. langFileCounts := make(map[string]int, len(langNames))
  79. contributorsSet := make(map[string]bool)
  80. for _, path := range jsonFiles {
  81. data, err := os.ReadFile(path)
  82. if err != nil {
  83. continue
  84. }
  85. var lf localeFileSchema
  86. if err = json.Unmarshal(data, &lf); err != nil {
  87. continue
  88. }
  89. for _, part := range strings.Split(lf.Author, ",") {
  90. part = strings.TrimSpace(part)
  91. if part != "" {
  92. contributorsSet[part] = true
  93. }
  94. }
  95. for code := range lf.Keys {
  96. if _, isBaseLang := langNames[code]; isBaseLang {
  97. langFileCounts[code]++
  98. }
  99. }
  100. }
  101. totalFiles := len(jsonFiles)
  102. // ── 4. Build sorted language list ──────────────────────────────────────────
  103. languages := make([]LanguageEntry, 0, len(langNames))
  104. for code, name := range langNames {
  105. count := langFileCounts[code]
  106. coverage := 0.0
  107. if totalFiles > 0 {
  108. coverage = math.Round(float64(count)/float64(totalFiles)*1000) / 10
  109. }
  110. languages = append(languages, LanguageEntry{
  111. Code: code,
  112. Name: name,
  113. FileCount: count,
  114. TotalFiles: totalFiles,
  115. Coverage: coverage,
  116. })
  117. }
  118. sort.Slice(languages, func(i, j int) bool {
  119. if languages[i].Coverage != languages[j].Coverage {
  120. return languages[i].Coverage > languages[j].Coverage
  121. }
  122. return languages[i].Code < languages[j].Code
  123. })
  124. // ── 5. Build sorted contributors list ──────────────────────────────────────
  125. contributors := make([]string, 0, len(contributorsSet))
  126. for c := range contributorsSet {
  127. contributors = append(contributors, c)
  128. }
  129. sort.Strings(contributors)
  130. resp := Response{
  131. Languages: languages,
  132. Contributors: contributors,
  133. TotalFiles: totalFiles,
  134. }
  135. js, _ := json.Marshal(resp)
  136. utils.SendJSONResponse(w, string(js))
  137. }