@@ -15,6 +15,17 @@ macro_rules! define_get_config_manager {
15
15
}
16
16
} ;
17
17
}
18
+ /// An internal macro used for defining the HTML `id` at which to render the Perseus app (which requires multiple branches). The default
19
+ /// is `root`.
20
+ #[ macro_export]
21
+ macro_rules! define_app_root {
22
+ ( ) => {
23
+ pub static APP_ROOT : & str = "root" ;
24
+ } ;
25
+ ( $root_id: literal) => {
26
+ pub static APP_ROOT : & str = $root_id;
27
+ } ;
28
+ }
18
29
/// An internal macro used for defining a function to get the user's preferred translations manager (which requires multiple branches).
19
30
#[ macro_export]
20
31
macro_rules! define_get_translations_manager {
@@ -72,18 +83,17 @@ macro_rules! define_get_locales {
72
83
}
73
84
}
74
85
} ;
86
+ // With i18n disabled, the default locale will be `xx-XX`
75
87
{
76
88
default : $default_locale: literal,
77
89
other: [ $( $other_locale: literal) ,* ] ,
78
90
no_i18n: $no_i18n: literal
79
91
} => {
80
92
pub fn get_locales( ) -> $crate:: Locales {
81
93
$crate:: Locales {
82
- default : $default_locale. to_string( ) ,
83
- other: vec![
84
- $( $other_locale. to_string( ) ) ,*
85
- ] ,
86
- using_i18n: !$no_i18n
94
+ default : "xx-XX" . to_string( ) ,
95
+ other: Vec :: new( ) ,
96
+ using_i18n: false
87
97
}
88
98
}
89
99
} ;
@@ -95,17 +105,18 @@ macro_rules! define_get_locales {
95
105
/// to `true` in `locales`. Note that you must still specify a default locale for verbosity and correctness. If you specify `no_i18n` and
96
106
/// a custom translations manager, the latter will override.
97
107
///
98
- /// Warning: all properties must currently be in the correct order (`root`, `error_pages `, `templates `, `locales`, `config_manager`,
108
+ /// Warning: all properties must currently be in the correct order (`root`, `templates `, `error_pages `, `locales`, `config_manager`,
99
109
/// `translations_manager`).
100
110
// TODO make this syntax even more compact and beautiful? (error pages inside templates?)
101
111
#[ macro_export]
102
112
macro_rules! define_app {
113
+ // With locales
103
114
{
104
- root: $root_selector: literal,
105
- error_pages: $error_pages: expr,
115
+ $( root: $root_selector: literal, ) ?
106
116
templates: [
107
117
$( $template: expr) ,+
108
118
] ,
119
+ error_pages: $error_pages: expr,
109
120
// This deliberately enforces verbose i18n definition, and forces developers to consider i18n as integral
110
121
locales: {
111
122
default : $default_locale: literal,
@@ -116,9 +127,77 @@ macro_rules! define_app {
116
127
$( , config_manager: $config_manager: expr) ?
117
128
$( , translations_manager: $translations_manager: expr) ?
118
129
} => {
130
+ define_app!(
131
+ @define_app,
132
+ {
133
+ $( root: $root_selector, ) ?
134
+ templates: [
135
+ $( $template) ,+
136
+ ] ,
137
+ error_pages: $error_pages,
138
+ locales: {
139
+ default : $default_locale,
140
+ // The user doesn't have to define any other locales (but they'll still get locale detection and the like)
141
+ other: [ $( $other_locale) ,* ]
142
+ }
143
+ $( , config_manager: $config_manager) ?
144
+ $( , translations_manager: $translations_manager) ?
145
+ }
146
+ ) ;
147
+ } ;
148
+ // Without locales (default locale is set to xx-XX)
149
+ {
150
+ $( root: $root_selector: literal, ) ?
151
+ templates: [
152
+ $( $template: expr) ,+
153
+ ] ,
154
+ error_pages: $error_pages: expr
155
+ $( , config_manager: $config_manager: expr) ?
156
+ $( , translations_manager: $translations_manager: expr) ?
157
+ } => {
158
+ define_app!(
159
+ @define_app,
160
+ {
161
+ $( root: $root_selector, ) ?
162
+ templates: [
163
+ $( $template) ,+
164
+ ] ,
165
+ error_pages: $error_pages,
166
+ // This deliberately enforces verbose i18n definition, and forces developers to consider i18n as integral
167
+ locales: {
168
+ default : "xx-XX" ,
169
+ other: [ ] ,
170
+ no_i18n: true
171
+ }
172
+ $( , config_manager: $config_manager) ?
173
+ $( , translations_manager: $translations_manager) ?
174
+ }
175
+ ) ;
176
+ } ;
177
+ // This is internal, and allows syntax abstractions and defaults
178
+ (
179
+ @define_app,
180
+ {
181
+ $( root: $root_selector: literal, ) ?
182
+ templates: [
183
+ $( $template: expr) ,+
184
+ ] ,
185
+ error_pages: $error_pages: expr,
186
+ // This deliberately enforces verbose i18n definition, and forces developers to consider i18n as integral
187
+ locales: {
188
+ default : $default_locale: literal,
189
+ // The user doesn't have to define any other locales
190
+ other: [ $( $other_locale: literal) ,* ]
191
+ // If this is defined at all, i18n will be disabled and the default locale will be set to `xx-XX`
192
+ $( , no_i18n: $no_i18n: literal) ?
193
+ }
194
+ $( , config_manager: $config_manager: expr) ?
195
+ $( , translations_manager: $translations_manager: expr) ?
196
+ }
197
+ ) => {
119
198
/// The html `id` that will find the app root to render Perseus in. For server-side interpolation, this MUST be an element of
120
199
/// the form <div id="root_id">` in your markup (double or single quotes, `root_id` replaced by what this property is set to).
121
- pub const APP_ROOT : & str = $ root_selector;
200
+ $crate :: define_app_root! ( $ ( $ root_selector) ? ) ;
122
201
123
202
/// Gets the config manager to use. This allows the user to conveniently test production managers in development. If nothing is
124
203
/// given, the filesystem will be used.
0 commit comments