Andriy Kornatskyy avatar Andriy Kornatskyy committed cd8aec8

Use Preprocessor for i18n and layout purpose.

Comments (0)

Files changed (9)

demos/quickstart-i18n/content/templates/public/home.html

-@extends("shared/master.html")
-@require(_)
+#extends("shared/master.html")
+#require(_)
 
-@def title():
-@_('Home')
-@end
+#def title():
+#_('Home')
+#end
 
-@def content():
+#def content():
 <div id="welcome">
-    <h2>@_('Welcome!')</h2>
+    <h2>#_('Welcome!')</h2>
     <p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     </p>
 </div>
-@end
+#end

demos/quickstart-i18n/content/templates/public/http400.html

-@extends("shared/master.html")
-@require(_)
+#extends("shared/master.html")
+#require(_)
 
-@def title():
-@_('Bad Request')
-@end
+#def title():
+#_('Bad Request')
+#end
 
-@def content():
+#def content():
 <div id="error">
-    <h2>@_("Oops! Code 400. Sorry, we can't process your request.")</h2>
+    <h2>#_("Oops! Code 400. Sorry, we can't process your request.")</h2>
     <p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     </p>
 </div>
-@end
+#end

demos/quickstart-i18n/content/templates/public/http403.html

-@extends("shared/master.html")
-@require(_)
+#extends("shared/master.html")
+#require(_)
 
-@def title():
-@_('Access Denied')
-@end
+#def title():
+#_('Access Denied')
+#end
 
-@def content():
+#def content():
 <div id="error">
-    <h2>@_('Oops! Code 403. Access is denied.')</h2>
+    <h2>#_('Oops! Code 403. Access is denied.')</h2>
     <p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     </p>
 </div>
-@end
+#end

demos/quickstart-i18n/content/templates/public/http404.html

-@extends("shared/master.html")
-@require(_)
+#extends("shared/master.html")
+#require(_)
 
-@def title():
-@_('We are sorry, the page you requested cannot be found.')
-@end
+#def title():
+#_('We are sorry, the page you requested cannot be found.')
+#end
 
-@def content():
+#def content():
 <div id="error">
-    <h2>@_("Oops! Code 404. Sorry, we can't find that page.")</h2>
+    <h2>#_("Oops! Code 404. Sorry, we can't find that page.")</h2>
     <p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     </p>
 </div>
-@end
+#end

demos/quickstart-i18n/content/templates/public/http500.html

-@extends("shared/master.html")
-@require(_)
+#extends("shared/master.html")
+#require(_)
 
-@def title():
-@_('We are sorry, we can not process your request.')
-@end
+#def title():
+#_('We are sorry, we can not process your request.')
+#end
 
-@def content():
+#def content():
 <div id="error">
-    <h2>@_('Oops! Code 500. Sorry, we can not process your request.')</h2>
+    <h2>#_('Oops! Code 500. Sorry, we can not process your request.')</h2>
     <p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
     </p>
 </div>
-@end
+#end

demos/quickstart-i18n/content/templates/shared/master.html

-@require(_, path_for)
+#require(_, path_for)
 <!DOCTYPE html>
 <html>
     <head>
-        @def title():
-        @end
-        <title>@_('My Site') - @title()</title>
-        <link href="@path_for('static', path='css/site.css')" type="text/css" rel="stylesheet" />
+        #def title():
+        #end
+        <title>#_('My Site') - #title()</title>
+        <link href="#path_for('static', path='css/site.css')" type="text/css" rel="stylesheet" />
     </head>
     <body>
         <div class="page">
             <div id="header">
-                <h1><a href="@path_for('default')">@_('My Site')</a></h1>
+                <h1><a href="#path_for('default')">#_('My Site')</a></h1>
                 #include("shared/snippet/menu-locale.html")
             </div>
             <div id="main">
                 <div id="placeholder">
-                    @def content():
-                    @end
-                    @content()\
+                    #def content():
+                    #end
+                    #content()\
                 </div>
             </div>
-            <div id="footer">@_('Version') #__version__</div>
+            <div id="footer">#_('Version') #__version__</div>
         </div>
         #include("shared/snippet/script.html")
-        @def script():
-        @end
-        @script()\
+        #def script():
+        #end
+        #script()\
     </body>
 </html>

demos/quickstart-i18n/content/templates/shared/snippet/menu-locale.html

-@require(_, path_for, route_args)
+#require(_)
+@require(path_for, route_args)
 <i>
-    <a href="@path_for(route_args.route_name, locale='en')">@_('English')</a> | \
-    <a href="@path_for(route_args.route_name, locale='ru')">@_('Russian')</a>
+    <a href="@path_for(route_args.route_name, locale='en')">#_('English')</a> | \
+    <a href="@path_for(route_args.route_name, locale='ru')">#_('Russian')</a>
 </i>

demos/quickstart-i18n/content/templates/shared/snippet/script.html

-@require(path_for)
-<script src="@path_for('static', path='js/core.js')"
+#require(path_for)
+<script src="#path_for('static', path='js/core.js')"
  type="text/javascript"></script>

demos/quickstart-i18n/src/config.py

 from wheezy.template.engine import Engine
 from wheezy.template.ext.core import CoreExtension
 from wheezy.template.loader import FileLoader
-from wheezy.template.loader import PreprocessLoader
+from wheezy.template.preprocessor import Preprocessor
 from wheezy.web.templates import WheezyTemplate
 from public import __version__
 from tracing import ERROR_REPORT_FORMAT
     'http_errors_extra_provider': error_report_extra_provider
 })
 
+
 # Template Engine
+def runtime_engine_factory(loader):
+    engine = Engine(
+        loader=loader,
+        extensions=[
+            CoreExtension(),
+            WidgetExtension(),
+            WhitespaceExtension(),
+        ])
+    engine.global_vars.update({
+        'format_value': format_value,
+        'h': html_escape,
+    })
+    return engine
+
 searchpath = ['content/templates']
 engine = Engine(
     loader=FileLoader(searchpath),
     extensions=[
-        CoreExtension(token_start='#')
-    ]
-)
+        CoreExtension(token_start='#', line_join=None)
+    ])
 engine.global_vars.update({
     '__version__': __version__
 })
-engine = Engine(
-    loader=PreprocessLoader(engine),
-    extensions=[
-        CoreExtension(),
-        WidgetExtension(),
-        WhitespaceExtension(),
-    ])
-engine.global_vars.update({
-    'format_value': format_value,
-    'h': html_escape
-})
+engine = Preprocessor(runtime_engine_factory, engine,
+                      key_factory=lambda ctx: ctx['locale'])
 options.update({
     'render_template': WheezyTemplate(engine)
 })
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.