-
Notifications
You must be signed in to change notification settings - Fork 61
(GH-538) Add schema_i18n! helper macro
#1482
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
Merged
SteveL-MSFT
merged 5 commits into
PowerShell:main
from
michaeltlombardi:gh-538/main/schema_i18n_helper
Apr 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78e8561
(GH-538) Add helper for schema i18n
michaeltlombardi 6b00f72
(GH-538) Use `schema_i18n!` for `dsc-lib::types`
michaeltlombardi 3d0a8a5
(MAINT) Update i18n testing for `schema_i18n` macro
michaeltlombardi 41d722c
(MAINT) Fix i18n tests for `dsc-lib-jsonschema`
michaeltlombardi e0e14ed
(MAINT) Address copilot feedback
michaeltlombardi 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
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 |
|---|---|---|
|
|
@@ -21,6 +21,10 @@ struct DscRepoSchemaReceiver { | |
| /// other schemas may not be. | ||
| #[darling(default)] | ||
| pub should_bundle: bool, | ||
| /// Defines the root dotpath for the JSON Schema keyword value translations used by `rust_i18n` | ||
| /// crate. Must be a literal string like `"schemas.definitions.tag"`. | ||
| #[darling(default)] | ||
| pub i18n_root_key: Option<String>, | ||
| /// Defines the field for the struct that is used as the `$schema` property. Typically only | ||
| /// defined for root schemas. | ||
| #[darling(default)] | ||
|
|
@@ -62,6 +66,12 @@ pub(crate) fn dsc_repo_schema_impl(input: TokenStream) -> TokenStream { | |
| } | ||
| }; | ||
|
|
||
| let i18n_root_key = args.i18n_root_key.unwrap_or(format!( | ||
| "schemas.{}.{}", | ||
| args.folder_path.replace('/', "."), | ||
| args.base_name | ||
| )); | ||
|
|
||
| let mut output = quote!(); | ||
|
|
||
| if let Some(schema_field) = args.schema_field { | ||
|
|
@@ -70,14 +80,16 @@ pub(crate) fn dsc_repo_schema_impl(input: TokenStream) -> TokenStream { | |
| args.base_name, | ||
| args.folder_path, | ||
| args.should_bundle, | ||
| i18n_root_key, | ||
| schema_field | ||
| )); | ||
| } else { | ||
| output.extend(generate_without_schema_field( | ||
| ident, | ||
| args.base_name, | ||
| args.folder_path, | ||
| args.should_bundle | ||
| args.should_bundle, | ||
| i18n_root_key | ||
| )); | ||
| } | ||
|
|
||
|
|
@@ -91,14 +103,19 @@ fn generate_without_schema_field( | |
| ident: Ident, | ||
| base_name: String, | ||
| folder_path: String, | ||
| should_bundle: bool | ||
| should_bundle: bool, | ||
| i18n_root_key: String, | ||
| ) -> proc_macro2::TokenStream { | ||
| let translation_fn = generate_schema_i18n_fn(); | ||
| quote!( | ||
| #[automatically_derived] | ||
| impl DscRepoSchema for #ident { | ||
| const SCHEMA_FILE_BASE_NAME: &'static str = #base_name; | ||
| const SCHEMA_FOLDER_PATH: &'static str = #folder_path; | ||
| const SCHEMA_SHOULD_BUNDLE: bool = #should_bundle; | ||
| const SCHEMA_I18N_ROOT_KEY: &'static str = #i18n_root_key; | ||
|
|
||
| #translation_fn | ||
|
|
||
| fn schema_property_metadata() -> schemars::Schema { | ||
| schemars::json_schema!({}) | ||
|
|
@@ -118,16 +135,21 @@ fn generate_with_schema_field( | |
| base_name: String, | ||
| folder_path: String, | ||
| should_bundle: bool, | ||
| i18n_root_key: String, | ||
| schema_field: DscRepoSchemaField | ||
| ) -> proc_macro2::TokenStream { | ||
| let schema_property_metadata = generate_schema_property_metadata_fn(&schema_field); | ||
| let translation_fn = generate_schema_i18n_fn(); | ||
| let field = schema_field.name; | ||
| quote!( | ||
| #[automatically_derived] | ||
| impl DscRepoSchema for #ident { | ||
| const SCHEMA_FILE_BASE_NAME: &'static str = #base_name; | ||
| const SCHEMA_FOLDER_PATH: &'static str = #folder_path; | ||
| const SCHEMA_SHOULD_BUNDLE: bool = #should_bundle; | ||
| const SCHEMA_I18N_ROOT_KEY: &'static str = #i18n_root_key; | ||
|
|
||
| #translation_fn | ||
|
|
||
| #schema_property_metadata | ||
|
|
||
|
|
@@ -169,3 +191,18 @@ fn generate_schema_property_metadata_fn(schema_field: &DscRepoSchemaField) -> pr | |
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generates a crate-local implementation for the `schema_i18n()` trait function. This is | ||
| /// required to ensure that the translations use the correct locale definitions. | ||
| fn generate_schema_i18n_fn() -> proc_macro2::TokenStream { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this increase the binary size in any impactful way? If so, perhaps we can do the check only on debug builds?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so:
|
||
| quote! { | ||
| fn schema_i18n(suffix: &str) -> Result<String, dsc_lib_jsonschema::dsc_repo::DscRepoSchemaMissingTranslation> { | ||
| let i18n_key = format!("{}.{}", Self::SCHEMA_I18N_ROOT_KEY, suffix); | ||
| if let Some(translated) = crate::_rust_i18n_try_translate(&rust_i18n::locale(), &i18n_key) { | ||
| Ok(translated.into()) | ||
| } else { | ||
| Err(dsc_lib_jsonschema::dsc_repo::DscRepoSchemaMissingTranslation { i18n_key }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.