veb: fix key value and translation file name (#23203)

This commit is contained in:
André Luiz 2024-12-19 12:46:46 -03:00 committed by GitHub
parent 05cbbfbabc
commit 939d243b5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 3 deletions

View File

@ -22,7 +22,7 @@ struct TrData {
// m['en']['house'] == 'House'
fn load_tr_map() map[string]map[string]string {
// Find all translation files to figure out how many languages we have and to load the translation map
files := os.walk_ext('translations/', '.tr')
files := os.walk_ext('translations', '.tr')
mut res := map[string]map[string]string{}
for tr_path in files {
lang := fetch_lang_from_tr_path(tr_path)
@ -35,7 +35,7 @@ fn load_tr_map() map[string]map[string]string {
// println('val="${val}"')
nl_pos := s.index('\n') or { continue }
key := s[..nl_pos]
val := s[nl_pos..]
val := s[nl_pos + 1..]
// v := vals[i + 1]
// println('key="${key}" => val="${v}"')
res[lang][key] = val
@ -46,7 +46,7 @@ fn load_tr_map() map[string]map[string]string {
}
fn fetch_lang_from_tr_path(path string) string {
return path.find_between('/', '.')
return path.find_between(os.path_separator, '.')
}
// Used by %key in templates

18
vlib/veb/tr_test.v Normal file
View File

@ -0,0 +1,18 @@
module veb
import os
fn test_load_files_translations() {
os.chdir(os.dir(@FILE))!
translations := load_tr_map()
assert 'pt-br' in translations
assert 'en' in translations
assert 'msg_hello' in translations['pt-br']
assert 'msg_hello' in translations['en']
assert translations['pt-br']['msg_hello'] == 'Olá'
assert translations['en']['msg_hello'] == 'Hello'
}

View File

@ -0,0 +1,2 @@
msg_hello
Hello

View File

@ -0,0 +1,2 @@
msg_hello
Olá