49 lines
6.4 KiB
HTML
49 lines
6.4 KiB
HTML
<!DOCTYPE html><html lang="en"><head><title>core/lang/i18n</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../../"><meta name="groc-document-path" content="core/lang/i18n"><meta name="groc-project-path" content="core/lang/i18n.js"><link rel="stylesheet" type="text/css" media="all" href="../../assets/style.css"><script type="text/javascript" src="../../assets/behavior.js"></script><body><div id="meta"><div class="file-path">core/lang/i18n.js</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
|
|
<span class="s2">"use strict"</span><span class="p">;</span>
|
|
|
|
<span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">),</span>
|
|
<span class="cm">/**</span>
|
|
<span class="cm"> * Create new Polyglot object</span>
|
|
<span class="cm"> * @type {Polyglot}</span>
|
|
<span class="cm"> */</span>
|
|
<span class="nx">I18n</span><span class="p">;</span>
|
|
|
|
<span class="nx">I18n</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">ghost</span><span class="p">)</span> <span class="p">{</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>TODO: validate</p></div></div><div class="code"><div class="wrapper"> <span class="kd">var</span> <span class="nx">lang</span> <span class="o">=</span> <span class="nx">ghost</span><span class="p">.</span><span class="nx">config</span><span class="p">().</span><span class="nx">defaultLang</span><span class="p">,</span>
|
|
<span class="nx">path</span> <span class="o">=</span> <span class="nx">ghost</span><span class="p">.</span><span class="nx">paths</span><span class="p">().</span><span class="nx">lang</span><span class="p">,</span>
|
|
<span class="nx">langFilePath</span> <span class="o">=</span> <span class="nx">path</span> <span class="o">+</span> <span class="nx">lang</span> <span class="o">+</span> <span class="s1">'.json'</span><span class="p">;</span>
|
|
|
|
<span class="k">return</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
|
|
|
<span class="k">if</span> <span class="p">(</span><span class="nx">lang</span> <span class="o">===</span> <span class="s1">'en'</span><span class="p">)</span> <span class="p">{</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>TODO: do stuff here to optimise for en</p></div></div><div class="code"><div class="wrapper"> <span class="p">}</span>
|
|
|
|
<span class="cm">/** TODO potentially use req.acceptedLanguages rather than the default</span>
|
|
<span class="cm"> * TODO handle loading language file for frontend on frontend request etc</span>
|
|
<span class="cm"> * TODO switch this mess to be promise driven */</span>
|
|
<span class="nx">fs</span><span class="p">.</span><span class="nx">stat</span><span class="p">(</span><span class="nx">langFilePath</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stat</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'No language file found for language '</span> <span class="o">+</span> <span class="nx">lang</span> <span class="o">+</span> <span class="s1">'. Defaulting to en'</span><span class="p">);</span>
|
|
<span class="nx">lang</span> <span class="o">=</span> <span class="s1">'en'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span><span class="p">(</span><span class="nx">langFilePath</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="k">throw</span> <span class="nx">err</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">try</span> <span class="p">{</span>
|
|
<span class="nx">data</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
|
|
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="k">throw</span> <span class="nx">e</span><span class="p">;</span> <span class="c1">// TODO - do something better with the error here</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="nx">ghost</span><span class="p">.</span><span class="nx">polyglot</span><span class="p">().</span><span class="nx">extend</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span>
|
|
|
|
<span class="nx">next</span><span class="p">();</span>
|
|
<span class="p">});</span>
|
|
<span class="p">});</span>
|
|
<span class="p">};</span>
|
|
<span class="p">};</span>
|
|
|
|
|
|
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">load</span> <span class="o">=</span> <span class="nx">I18n</span><span class="p">;</span>
|
|
<span class="p">}());</span></div></div></div></div></body></html> |