From 4a92d361fbd535609a60b0803591a2a52d2286fb Mon Sep 17 00:00:00 2001 From: Kirby Wu Date: Sat, 23 Jan 2021 14:17:49 +0800 Subject: [PATCH] add pug function to wrap url with i18n path --- README.md | 4 ++++ bin/lib/build/pug.js | 11 ++++++++++- lib/build/pug.ls | 3 ++- src/pug/index.pug | 1 + static/index.html | 2 +- static/intl/en/index.html | 2 +- static/intl/zh-TW/index.html | 2 +- 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b46e061..41f350b 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,10 @@ will access to `another-key` in `new-ns.yaml`. Be sure to add your namespace nam "i18n": { ... "ns": ["default", "new-ns"] } +additionally, use `intlbase` to wrap path with a i18n based relative path: + + a(href=intlbase('/faq')) + ## TODO diff --git a/bin/lib/build/pug.js b/bin/lib/build/pug.js index 4a67d93..fd6c490 100644 --- a/bin/lib/build/pug.js +++ b/bin/lib/build/pug.js @@ -1,6 +1,7 @@ // Generated by LiveScript 1.6.0 -var fs, fsExtra, pug, livescript, stylus, path, jsYaml, marked, aux, lc, mdOptions, pugExtapi, main; +var fs, path, fsExtra, pug, livescript, stylus, jsYaml, marked, aux, lc, mdOptions, pugExtapi, main; fs = require('fs'); +path = require('path'); fsExtra = require('fs-extra'); pug = require('pug'); livescript = require('livescript'); @@ -73,6 +74,14 @@ main = { pugExtapi.i18n = function(it){ return opt.i18n.t(it); }; + pugExtapi.intlbase = function(p){ + p == null && (p = ""); + if (opt.i18n.language) { + return path.join("/intl", opt.i18n.language, p); + } else { + return p; + } + }; (pugExtapi.filters || (pugExtapi.filters = {})).i18n = function(t, o){ return opt.i18n.t(t); }; diff --git a/lib/build/pug.ls b/lib/build/pug.ls index d2afe0f..007d4c6 100644 --- a/lib/build/pug.ls +++ b/lib/build/pug.ls @@ -1,4 +1,4 @@ -require! <[fs fs-extra pug livescript stylus path js-yaml marked ./aux]> +require! <[fs path fs-extra pug livescript stylus path js-yaml marked ./aux]> lc = {i18n: {}} md-options = html: {breaks: true, renderer: new marked.Renderer!} @@ -33,6 +33,7 @@ main = do opt: (opt = {}) -> if opt.i18n => pug-extapi.i18n = -> opt.i18n.t(it) + pug-extapi.intlbase = (p = "") -> if opt.i18n.language => path.join("/intl",opt.i18n.language,p) else p pug-extapi.{}filters.i18n = (t, o) -> opt.i18n.t(t) lc.i18n = opt.i18n map: (list) -> diff --git a/src/pug/index.pug b/src/pug/index.pug index b7772c5..5c1ada4 100644 --- a/src/pug/index.pug +++ b/src/pug/index.pug @@ -21,3 +21,4 @@ html i18n key filter div= i18n("i18n key") div= i18n("alternative:namespace-test") + div= intlbase("/") diff --git a/static/index.html b/static/index.html index 93d3cc5..829a3b7 100644 --- a/static/index.html +++ b/static/index.html @@ -9,4 +9,4 @@ div.textContent = "livescript filter test";
i18n 翻譯測試 ( 編譯時期, pug filter )
i18n 翻譯測試 ( 執行時期, pug function )
命名域測試成功.
\ No newline at end of file +
i18n 翻譯測試 ( 編譯時期, pug filter )
i18n 翻譯測試 ( 執行時期, pug function )
命名域測試成功.
/intl/zh-TW/
\ No newline at end of file diff --git a/static/intl/en/index.html b/static/intl/en/index.html index 9cccd7c..485324f 100644 --- a/static/intl/en/index.html +++ b/static/intl/en/index.html @@ -9,4 +9,4 @@ div.textContent = "livescript filter test";
i18n translation test ( compile time filter )
i18n translation test ( runtime function )
namespace test ok.
\ No newline at end of file +
i18n translation test ( compile time filter )
i18n translation test ( runtime function )
namespace test ok.
/intl/en/
\ No newline at end of file diff --git a/static/intl/zh-TW/index.html b/static/intl/zh-TW/index.html index 93d3cc5..829a3b7 100644 --- a/static/intl/zh-TW/index.html +++ b/static/intl/zh-TW/index.html @@ -9,4 +9,4 @@ div.textContent = "livescript filter test";
i18n 翻譯測試 ( 編譯時期, pug filter )
i18n 翻譯測試 ( 執行時期, pug function )
命名域測試成功.
\ No newline at end of file +
i18n 翻譯測試 ( 編譯時期, pug filter )
i18n 翻譯測試 ( 執行時期, pug function )
命名域測試成功.
/intl/zh-TW/
\ No newline at end of file