1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > SiLo - 02 Dictionary Caching</ title >
6+ < script type ="text/javascript " src ="../silo.js "> </ script >
7+
8+ < script type ="text/javascript ">
9+ var english = {
10+ welcome_message : "Welcome to the SiLo caching example. Click the links below to translate." ,
11+ about_caching : "In this example both languages will take a while to load.\n"
12+ + "But once they load they're cached and subsequent loads will be instantaneous." ,
13+ lang : {
14+ en_US : "English" ,
15+ pt_PT : "Portuguese"
16+ }
17+ } ;
18+
19+ var portuguese = {
20+ welcome_message : "Bem vindo ao exemplo de 'caching' do SiLo. Clique nas ligações abaixo para traduzir." ,
21+ about_caching : "Neste exemplo ambas as linguagens demoram um pouco a carregar.\n"
22+ + "Mas uma vez carregadas são guardadas em 'cache' e os carregamentos seguintes serão instantâneos." ,
23+ lang : {
24+ en_US : "Inglês" ,
25+ pt_PT : "Português"
26+ }
27+ } ;
28+
29+ function loadLang ( code , callback ) {
30+ console . log ( "Pretending to load language '" + code + "' from an AJAX request (taking 1,5s to load)." ) ;
31+ setTimeout ( function imagineThisIsAJAX ( ) {
32+ if ( code == "en_US" ) callback ( english ) ;
33+ else if ( code == "pt_PT" ) callback ( portuguese ) ;
34+ // by not calling callback(dictionary) for a language that doesn't exist, we silently abort.
35+ } , 1500 ) ;
36+ }
37+
38+ function onBodyLoad ( ) {
39+ // These 2 lines are optional and merely for debug purposes.
40+ SiLo . warnings . all ( console . error ) ; // everything is a serious error
41+ SiLo . warnings . fallback ( console . warn ) ; // but using fallbacks is tolerable
42+ toLang ( "en_US" ) ;
43+ }
44+
45+ // This is now needed because language switch can be sync and async, depending on caching status,
46+ // and it's better to have a single callback to re-localize the document
47+ // than to blindly relocalize twice in hopes to nail it.
48+ function onLanguageLoaded ( ) {
49+ SiLo . localize . all ( ) ;
50+ }
51+
52+ function toLang ( langCode ) {
53+ // Equivalent to SiLo.language.cache(langCode, loadLang, SiLo.localize.all)
54+ // But the way this works is simpler to understand if we have the onLanguageLoaded function
55+ // So that it's dead obvious you can re-localize however you want.
56+ SiLo . language . cache ( langCode , loadLang , onLanguageLoaded ) ; // Set a new language, by code, from cache.
57+ }
58+ </ script >
59+
60+ < style type ="text/css ">
61+ # root { font-family : "Helvetica" ; width : 600px ; margin : auto; margin-top : 100px ;
62+ border : 1px solid # 444 ; padding : 20px ; }
63+ # root > div : not (: last-child ){ margin-bottom : 20px ; }
64+ </ style >
65+ </ head >
66+
67+
68+ < body onload ="onBodyLoad(); ">
69+ < div id ="root ">
70+ <!-- Did I mention you can still have default messages while the language loads / in case JS is disabled?? -->
71+ < div data-silo-key ="text:welcome_message "> Please wait...</ div >
72+ < div data-silo-key ="text:about_caching "> </ div >
73+ < div >
74+ < a href ="javascript:toLang('en_US'); " data-silo-key ="string:lang.en_US "> </ a >
75+ |
76+ < a href ="javascript:toLang('pt_PT'); " data-silo-key ="string:lang.pt_PT "> </ a >
77+ </ div >
78+ </ div >
79+ </ body >
80+ </ html >
0 commit comments