Skip to content

Commit 2253a1c

Browse files
committed
Enhance caching, fix bug.
1 parent cf62ec9 commit 2253a1c

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Custom functions must abide by the `func( element, str )` signature, where `elem
4242
# SiLo - Library Summary
4343
## SiLo.language
4444
+ **SiLo.language.load( dictionary, makeFallback )** - Sets the `dictionary` object as the current dictionary. If `makeFallback` is true this dictionary will also be set as the fallback dictionary for when a key can't be found in the current one (for example, to write in english if a sentence is unavailable in the current language).
45-
+ **SiLo.language.cache( code, loader, makeFallback )** - Sets the current dictionary from cache. If there is no `code` entry in the cache, `loader` (a callback you must provide) will be asked to load it; the `loader` must follow the `loader( code, callback )` signature, where `code` is the same language/country code entry missing from cache and `callback( dictionary )` must be called once you successfuly load the dictionary file -- not calling it simply results in the operation being aborted. `makeFallback` works in the same way as in the simpler **load** function.
45+
+ **SiLo.language.cache( code, loader, relocalize, makeFallback )** - Sets the current dictionary from cache. If there is no `code` entry in the cache, `loader` (a callback you must provide) will be asked to load it; the `loader` must follow the `loader( code, callback )` signature, where `code` is the same language/country code entry missing from cache and `callback( dictionary )` must be called once you successfuly load the dictionary file -- not calling it simply results in the operation being aborted. When the language finishes loading, `relocalize` will be called: this is a callback you provide so that you can relocalize the document once the language switching is done successfully. `makeFallback` works in the same way as in the simpler **load** function.
4646

4747
## SiLo.localize
4848
+ **SiLo.localize.all( )** - Localizes the entire document. The same as calling `Silo.localize.recursive(document.body)`.

silo.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var SiLo = (function(){
2-
var language = null, fallback = null, cache = null, functions = null, warn_fallback = null, warn_no_key = null, warn_no_func = null;
2+
var language = null, fallback = null, cache = {}, functions = null, warn_fallback = null, warn_no_key = null, warn_no_func = null;
33

44
function replace_all(s, t, r){ return s.split(t).join(r); }
55
function tree_find(keys, def, idx, obj){ ret = obj[keys[idx++]]; return ret ? (idx == keys.length ? ret : tree_find(keys, def, idx, ret)) : def; }
@@ -51,11 +51,12 @@ var SiLo = (function(){
5151
function localize_class(className){ localize(document.getElementsByClassName(className)); }
5252

5353
function language_load(dictionary, makeFallback){ language = dictionary; if(makeFallback) fallback = dictionary; }
54-
function language_cache(code, loader, makeFallback){
54+
function language_cache(code, loader, relocalize, makeFallback){
5555
var callback = function(dict){
5656
if(!cache[code]) cache[code] = dict;
5757
language = cache[code];
5858
if(makeFallback) fallback = cache[code];
59+
relocalize();
5960
}
6061
if(!cache[code]) loader(code, callback);
6162
else callback();

0 commit comments

Comments
 (0)