your bug regarding the phrases not showing in some situation is related to your phrases included...
in function insert_phrase, you add the phrases in the languageid #0 ... put them in -1 if you want them to work, because #0 is a unknown language... -1 is the default . and your build_language(-1) tells it...
...always add your phrases in the languageid #-1 ... to make them defaults.. like the templates...