-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
vlib,builder,v3: add i18n and fix shared builds #27721
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
Open
medvednikov
wants to merge
2
commits into
master
Choose a base branch
from
pr/i18n-shared-library-builds
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,104 @@ | ||
| // Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved. | ||
| // Use of this source code is governed by an MIT license | ||
| // that can be found in the LICENSE file. | ||
| module i18n | ||
|
|
||
| import os | ||
|
|
||
| pub const default_translations_dir = 'translations' | ||
|
|
||
| const default_tr_map = load_tr_map() | ||
|
|
||
| // load_tr_map loads all .tr files from the default translations directory. | ||
| pub fn load_tr_map() map[string]map[string]string { | ||
| return load_tr_map_from_dir(default_translations_dir) | ||
| } | ||
|
|
||
| // load_tr_map_from_dir loads all .tr files from dir into a lang -> key -> text map. | ||
| pub fn load_tr_map_from_dir(dir string) map[string]map[string]string { | ||
| files := os.walk_ext(dir, '.tr') | ||
| mut res := map[string]map[string]string{} | ||
| for tr_path in files { | ||
| lang := fetch_lang_from_tr_path(tr_path) | ||
| if lang.len == 0 { | ||
| continue | ||
| } | ||
| text := os.read_file(tr_path) or { | ||
| eprintln('translation file "${tr_path}" failed to load') | ||
| return {} | ||
| } | ||
| for key, val in parse_tr_text(text) { | ||
| res[lang][key] = val | ||
| } | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| fn parse_tr_text(text string) map[string]string { | ||
| mut res := map[string]string{} | ||
| normalized := text.replace('\r\n', '\n') | ||
| for section in normalized.split('-----\n') { | ||
| nl_pos := section.index('\n') or { continue } | ||
| key := section[..nl_pos].trim_space() | ||
| if key.len == 0 { | ||
| continue | ||
| } | ||
| res[key] = section[nl_pos + 1..].trim_right('\n') | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| fn fetch_lang_from_tr_path(path string) string { | ||
| return os.file_name(path).all_before_last('.tr') | ||
| } | ||
|
|
||
| // tr returns the translation for key from the default translations directory. | ||
| pub fn tr(lang string, key string) string { | ||
| return tr_from_map(default_tr_map, lang, key) | ||
| } | ||
|
|
||
| // tr_from_map returns the translation for key from translations. | ||
| pub fn tr_from_map(translations map[string]map[string]string, lang string, key string) string { | ||
| res := translations[lang][key] | ||
| if res == '' { | ||
| eprintln('NO TRANSLATION FOR KEY "${key}"') | ||
| return key | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| // tr_plural returns the pluralized translation for key from the default translations directory. | ||
| pub fn tr_plural(lang string, key string, amount int) string { | ||
| return tr_plural_from_map(default_tr_map, lang, key, amount) | ||
| } | ||
|
|
||
| // tr_plural_from_map returns the pluralized translation for key from translations. | ||
| pub fn tr_plural_from_map(translations map[string]map[string]string, lang string, key string, amount int) string { | ||
| s := translations[lang][key] | ||
| if s == '' { | ||
| eprintln('NO TRANSLATION FOR KEY "${key}"') | ||
| return key | ||
| } | ||
| if !s.contains('|') { | ||
| return s | ||
| } | ||
| // goods | ||
| // товар|а|ов | ||
| vals := s.split('|') | ||
| if vals.len != 3 { | ||
| return s | ||
| } | ||
| amount_str := amount.str() | ||
| ending := if amount % 10 == 1 && !amount_str.ends_with('11') { | ||
| '' | ||
| } else if amount % 10 == 2 && !amount_str.ends_with('12') { | ||
| vals[1] | ||
| } else if amount % 10 == 3 && !amount_str.ends_with('13') { | ||
| vals[1] | ||
| } else if amount % 10 == 4 && !amount_str.ends_with('14') { | ||
| vals[1] | ||
| } else { | ||
| vals[2] | ||
| } | ||
| return vals[0] + ending | ||
| } | ||
This file contains hidden or 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,30 @@ | ||
| module i18n | ||
|
|
||
| import os | ||
|
|
||
| fn test_load_tr_map_from_dir() { | ||
| translations := load_tr_map_from_dir(os.join_path(os.dir(@FILE), 'testdata', 'translations')) | ||
|
|
||
| assert 'en' in translations | ||
| assert 'pt-br' in translations | ||
| assert translations['en']['msg_hello'] == 'Hello' | ||
| assert translations['pt-br']['msg_hello'] == 'Ola' | ||
| } | ||
|
|
||
| fn test_tr_from_map_returns_key_for_missing_translation() { | ||
| translations := load_tr_map_from_dir(os.join_path(os.dir(@FILE), 'testdata', 'translations')) | ||
|
|
||
| assert tr_from_map(translations, 'en', 'missing_key') == 'missing_key' | ||
| } | ||
|
|
||
| fn test_tr_plural_from_map() { | ||
| translations := { | ||
| 'ru': { | ||
| 'goods': 'товар|а|ов' | ||
| } | ||
| } | ||
|
|
||
| assert tr_plural_from_map(translations, 'ru', 'goods', 1) == 'товар' | ||
| assert tr_plural_from_map(translations, 'ru', 'goods', 2) == 'товара' | ||
| assert tr_plural_from_map(translations, 'ru', 'goods', 5) == 'товаров' | ||
| } |
This file contains hidden or 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,2 @@ | ||
| msg_hello | ||
| Hello |
This file contains hidden or 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,2 @@ | ||
| msg_hello | ||
| Ola |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two new public functions are exported without V doc comments, while the repo guide requires a doc comment immediately before each new or modified public function and says it should start with the function name (
AGENTS.mdlines 286-289). This leaves the newi18npublic API undocumented in generated module docs; please addtr_plural...comments for both helpers.Useful? React with 👍 / 👎.