@next-box/i18n 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cjs/constants.cjs.map +1 -0
  2. package/dist/cjs/helpers/injectVariables.cjs.map +1 -0
  3. package/dist/cjs/helpers/loadConfig.cjs.map +1 -0
  4. package/dist/cjs/macros.cjs.map +1 -0
  5. package/dist/cjs/main.cjs +1 -1
  6. package/dist/cjs/main.cjs.map +1 -1
  7. package/dist/esm/constants.mjs.map +1 -0
  8. package/dist/esm/helpers/injectVariables.mjs.map +1 -0
  9. package/dist/esm/helpers/loadConfig.mjs.map +1 -0
  10. package/dist/esm/macros.mjs.map +1 -0
  11. package/dist/esm/main.mjs +1 -1
  12. package/dist/esm/main.mjs.map +1 -1
  13. package/dist/types/cjs/main.d.cts.map +1 -1
  14. package/dist/types/esm/main.d.ts.map +1 -1
  15. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  16. package/package.json +2 -2
  17. package/rollup.config.mjs +1 -9
  18. package/src/main.ts +12 -11
  19. package/dist/cjs/client/helpers/injectVariables.cjs.map +0 -1
  20. package/dist/cjs/macros/constants.cjs.map +0 -1
  21. package/dist/cjs/macros/helpers/loadConfig.cjs.map +0 -1
  22. package/dist/cjs/macros/macros.cjs.map +0 -1
  23. package/dist/esm/client/helpers/injectVariables.mjs.map +0 -1
  24. package/dist/esm/macros/constants.mjs.map +0 -1
  25. package/dist/esm/macros/helpers/loadConfig.mjs.map +0 -1
  26. package/dist/esm/macros/macros.mjs.map +0 -1
  27. /package/dist/cjs/{client/client.cjs → client.cjs} +0 -0
  28. /package/dist/cjs/{client/client.cjs.map → client.cjs.map} +0 -0
  29. /package/dist/cjs/{client/components → components}/I18nProvider.cjs +0 -0
  30. /package/dist/cjs/{client/components → components}/I18nProvider.cjs.map +0 -0
  31. /package/dist/cjs/{client/components → components}/Md.cjs +0 -0
  32. /package/dist/cjs/{client/components → components}/Md.cjs.map +0 -0
  33. /package/dist/cjs/{macros/constants.cjs → constants.cjs} +0 -0
  34. /package/dist/cjs/{client/helpers → helpers}/injectVariables.cjs +0 -0
  35. /package/dist/cjs/{macros/helpers → helpers}/loadConfig.cjs +0 -0
  36. /package/dist/cjs/{macros/macros.cjs → macros.cjs} +0 -0
  37. /package/dist/esm/{client/client.mjs → client.mjs} +0 -0
  38. /package/dist/esm/{client/client.mjs.map → client.mjs.map} +0 -0
  39. /package/dist/esm/{client/components → components}/I18nProvider.mjs +0 -0
  40. /package/dist/esm/{client/components → components}/I18nProvider.mjs.map +0 -0
  41. /package/dist/esm/{client/components → components}/Md.mjs +0 -0
  42. /package/dist/esm/{client/components → components}/Md.mjs.map +0 -0
  43. /package/dist/esm/{macros/constants.mjs → constants.mjs} +0 -0
  44. /package/dist/esm/{client/helpers → helpers}/injectVariables.mjs +0 -0
  45. /package/dist/esm/{macros/helpers → helpers}/loadConfig.mjs +0 -0
  46. /package/dist/esm/{macros/macros.mjs → macros.mjs} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.cjs","sources":["../../src/constants.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n"],"names":[],"mappings":"qCAA+B"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injectVariables.cjs","sources":["../../../src/helpers/injectVariables.ts"],"sourcesContent":["const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue !== undefined) {\n return String(replacementValue);\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n"],"names":["extractTemplateVariables","extractVarName","markdown","variables","replaceAll","match","varName","replace","replacementValue","undefined","String","Error"],"mappings":"aAAA,MAAMA,EAA2B,gBAC3BC,EAAiB,2CAEQ,CAACC,EAAkBC,IAChDA,EACID,EAASE,WAAWJ,EAA2BK,IAC7C,MAAMC,EAAUD,EAAME,QAAQN,EAAgB,MACxCO,EAAmBL,EAAUG,GAEnC,QAAyBG,IAArBD,EACF,OAAOE,OAAOF,GAGhB,MAAM,IAAIG,MAAM,2CAA2CL,QAE7DJ"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadConfig.cjs","sources":["../../../src/helpers/loadConfig.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["cwd","process","config","JSON","parse","readFileSync","resolve","CONFIG_FILENAME","encoding"],"mappings":"2FAKA,MAAMA,EAAMC,QAAQD,yBAEM,KACxB,IAAIE,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAAA,aAAaC,EAAAA,QAAQN,EAAKO,EAAAA,iBAAkB,CAAEC,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAON"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"macros.cjs","sources":["../../src/macros.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, path);\n // @ts-expect-error `md` function runs and build time and its return value\n // replaces the `md` invocation. We are forcing the return type to be the\n // input type to aid type inference when using `createI18n` and its\n // `tt` content resolvers.\n return readFileSync(fullPath, { encoding: 'utf8' }).trim();\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","path","Error","dir","cwd","markdownDir","loadConfig","replaceAll","resolve","fullPath","readFileSync","encoding","trim"],"mappings":"mGAIA,MAAMA,0BAAEA,GAA8BC,QAAQC,eAETC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAMJ,QAAQK,OACdC,YAAEA,GAAgBC,eAElBD,IACEP,IACFO,EAAcA,EAAYE,WAAW,gCAAiC,IAAMT,IAG9EK,EAAMK,EAAAA,QAAQL,EAAKE,IAGrB,MAAMI,EAAWD,EAAAA,QAAQL,EAAKF,GAK9B,OAAOS,EAAAA,aAAaD,EAAU,CAAEE,SAAU,SAAUC"}
package/dist/cjs/main.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var r=require("zcb");exports.createI18n=e=>{const t=r.createConfigReader(e);return{t:(r,e)=>t.read(r,e),tt:(r,e)=>{const o=t.read(r,e);if(void 0===o)throw new Error(`Path "${String(r)}" did not resolve to a value`);return o}}};
1
+ "use strict";var r=require("zcb");exports.createI18n=e=>{const t=r.createConfigReader(e);return{t:(r,e)=>t.read(r,e),tt:(r,...e)=>{const o=t.read(r,...e);if(void 0===o)throw new Error(`Path "${String(r)}" did not resolve to a value`);return o}}};
2
2
  //# sourceMappingURL=main.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.cjs","sources":["../../src/main.ts"],"sourcesContent":["import { type Get } from 'type-fest';\nimport { type AnyRecord, type Path, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt: <P extends Path<U>>(path: P, variables?: AnyRecord): Get<U, P> => {\n const result = reader.read(path, variables);\n\n if (result === undefined) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n },\n };\n};\n"],"names":["i18n","reader","createConfigReader","t","key","variables","read","tt","path","result","undefined","Error","String"],"mappings":"qDAI6CA,IAC3C,MAAMC,EAASC,EAAAA,mBAAsBF,GAErC,MAAO,CAGLG,EAAG,CAACC,EAAaC,IAA8CJ,EAAOK,KAAKF,EAAKC,GAChFE,GAAI,CAAoBC,EAASH,KAC/B,MAAMI,EAASR,EAAOK,KAAKE,EAAMH,GAEjC,QAAeK,IAAXD,EACF,MAAM,IAAIE,MAAM,SAASC,OAAOJ,kCAGlC,OAAOC"}
1
+ {"version":3,"file":"main.cjs","sources":["../../src/main.ts"],"sourcesContent":["import { type AnyRecord, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n const tt: CreateI18nResult<U>['tt'] = (path, ...variables) => {\n const result = reader.read(path, ...variables);\n\n if (result === undefined) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n };\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt,\n };\n};\n"],"names":["i18n","reader","createConfigReader","t","key","variables","read","tt","path","result","undefined","Error","String"],"mappings":"qDAG6CA,IAC3C,MAAMC,EAASC,EAAAA,mBAAsBF,GAYrC,MAAO,CAGLG,EAAG,CAACC,EAAaC,IAA8CJ,EAAOK,KAAKF,EAAKC,GAChFE,GAdoC,CAACC,KAASH,KAC9C,MAAMI,EAASR,EAAOK,KAAKE,KAASH,GAEpC,QAAeK,IAAXD,EACF,MAAM,IAAIE,MAAM,SAASC,OAAOJ,kCAGlC,OAAOC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.mjs","sources":["../../src/constants.ts"],"sourcesContent":["export const CONFIG_FILENAME = '.i18n-macro.config.json';\n"],"names":["CONFIG_FILENAME"],"mappings":"AAAO,MAAMA,EAAkB"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injectVariables.mjs","sources":["../../../src/helpers/injectVariables.ts"],"sourcesContent":["const extractTemplateVariables = /{{[^\\s{}]+}}/g;\nconst extractVarName = /{{ *([^ }]*) *}}/;\n\nexport const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>\n variables\n ? markdown.replaceAll(extractTemplateVariables, (match: string) => {\n const varName = match.replace(extractVarName, '$1');\n const replacementValue = variables[varName];\n\n if (replacementValue !== undefined) {\n return String(replacementValue);\n }\n\n throw new Error(`Markdown contains un-replaced variable '${varName}'`);\n })\n : markdown;\n"],"names":["extractTemplateVariables","extractVarName","injectVariables","markdown","variables","replaceAll","match","varName","replace","replacementValue","undefined","String","Error"],"mappings":"AAAA,MAAMA,EAA2B,gBAC3BC,EAAiB,mBAEVC,EAAkB,CAACC,EAAkBC,IAChDA,EACID,EAASE,WAAWL,EAA2BM,IAC7C,MAAMC,EAAUD,EAAME,QAAQP,EAAgB,MACxCQ,EAAmBL,EAAUG,GAEnC,QAAyBG,IAArBD,EACF,OAAOE,OAAOF,GAGhB,MAAM,IAAIG,MAAM,2CAA2CL,QAE7DJ"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadConfig.mjs","sources":["../../../src/helpers/loadConfig.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { CONFIG_FILENAME } from '#constants.ts';\nimport { type MacrosConfig } from '#types.ts';\n\nconst cwd = process.cwd();\n\nexport const loadConfig = (): Partial<MacrosConfig> => {\n let config: Partial<MacrosConfig> = {};\n\n try {\n // JSON.parse returns an any time\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n config = JSON.parse(readFileSync(resolve(cwd, CONFIG_FILENAME), { encoding: 'utf8' })) as Partial<MacrosConfig>;\n } catch {\n // no catch\n }\n\n return config;\n};\n"],"names":["cwd","process","loadConfig","config","JSON","parse","readFileSync","resolve","CONFIG_FILENAME","encoding"],"mappings":"8HAKA,MAAMA,EAAMC,QAAQD,MAEPE,EAAa,KACxB,IAAIC,EAAgC,CAAA,EAEpC,IAGEA,EAASC,KAAKC,MAAMC,EAAaC,EAAQP,EAAKQ,GAAkB,CAAEC,SAAU,SAC9E,CAAE,MAEF,CAEA,OAAON"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"macros.mjs","sources":["../../src/macros.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { loadConfig } from '#helpers/loadConfig.ts';\n\nconst { NEXT_PUBLIC_LANGUAGE_CODE } = process.env;\n\nexport const md = <P extends string>(path: P): P => {\n if (!path) {\n throw new Error('md expected a path, but received none');\n }\n\n let dir = process.cwd();\n let { markdownDir } = loadConfig();\n\n if (markdownDir) {\n if (NEXT_PUBLIC_LANGUAGE_CODE) {\n markdownDir = markdownDir.replaceAll('{{NEXT_PUBLIC_LANGUAGE_CODE}}', () => NEXT_PUBLIC_LANGUAGE_CODE);\n }\n\n dir = resolve(dir, markdownDir);\n }\n\n const fullPath = resolve(dir, path);\n // @ts-expect-error `md` function runs and build time and its return value\n // replaces the `md` invocation. We are forcing the return type to be the\n // input type to aid type inference when using `createI18n` and its\n // `tt` content resolvers.\n return readFileSync(fullPath, { encoding: 'utf8' }).trim();\n};\n"],"names":["NEXT_PUBLIC_LANGUAGE_CODE","process","env","md","path","Error","dir","cwd","markdownDir","loadConfig","replaceAll","resolve","fullPath","readFileSync","encoding","trim"],"mappings":"iIAIA,MAAMA,0BAAEA,GAA8BC,QAAQC,IAEjCC,EAAwBC,IACnC,IAAKA,EACH,MAAM,IAAIC,MAAM,yCAGlB,IAAIC,EAAML,QAAQM,OACdC,YAAEA,GAAgBC,IAElBD,IACER,IACFQ,EAAcA,EAAYE,WAAW,gCAAiC,IAAMV,IAG9EM,EAAMK,EAAQL,EAAKE,IAGrB,MAAMI,EAAWD,EAAQL,EAAKF,GAK9B,OAAOS,EAAaD,EAAU,CAAEE,SAAU,SAAUC"}
package/dist/esm/main.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{createConfigReader as r}from"zcb";const t=t=>{const o=r(t);return{t:(r,t)=>o.read(r,t),tt:(r,t)=>{const e=o.read(r,t);if(void 0===e)throw new Error(`Path "${String(r)}" did not resolve to a value`);return e}}};export{t as createI18n};
1
+ import{createConfigReader as r}from"zcb";const t=t=>{const o=r(t);return{t:(r,t)=>o.read(r,t),tt:(r,...t)=>{const e=o.read(r,...t);if(void 0===e)throw new Error(`Path "${String(r)}" did not resolve to a value`);return e}}};export{t as createI18n};
2
2
  //# sourceMappingURL=main.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","sources":["../../src/main.ts"],"sourcesContent":["import { type Get } from 'type-fest';\nimport { type AnyRecord, type Path, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt: <P extends Path<U>>(path: P, variables?: AnyRecord): Get<U, P> => {\n const result = reader.read(path, variables);\n\n if (result === undefined) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n },\n };\n};\n"],"names":["createI18n","i18n","reader","createConfigReader","t","key","variables","read","tt","path","result","undefined","Error","String"],"mappings":"yCAIO,MAAMA,EAAgCC,IAC3C,MAAMC,EAASC,EAAsBF,GAErC,MAAO,CAGLG,EAAG,CAACC,EAAaC,IAA8CJ,EAAOK,KAAKF,EAAKC,GAChFE,GAAI,CAAoBC,EAASH,KAC/B,MAAMI,EAASR,EAAOK,KAAKE,EAAMH,GAEjC,QAAeK,IAAXD,EACF,MAAM,IAAIE,MAAM,SAASC,OAAOJ,kCAGlC,OAAOC"}
1
+ {"version":3,"file":"main.mjs","sources":["../../src/main.ts"],"sourcesContent":["import { type AnyRecord, createConfigReader } from 'zcb';\nimport { type CreateI18nResult } from '#types.ts';\n\nexport const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {\n const reader = createConfigReader<U>(i18n);\n\n const tt: CreateI18nResult<U>['tt'] = (path, ...variables) => {\n const result = reader.read(path, ...variables);\n\n if (result === undefined) {\n throw new Error(`Path \"${String(path)}\" did not resolve to a value`);\n }\n\n return result;\n };\n\n return {\n // @ts-expect-error intentionally ignoring the type checking here as\n // function is supposed to be untyped.\n t: (key: string, variables?: AnyRecord): string | undefined => reader.read(key, variables),\n tt,\n };\n};\n"],"names":["createI18n","i18n","reader","createConfigReader","t","key","variables","read","tt","path","result","undefined","Error","String"],"mappings":"yCAGO,MAAMA,EAAgCC,IAC3C,MAAMC,EAASC,EAAsBF,GAYrC,MAAO,CAGLG,EAAG,CAACC,EAAaC,IAA8CJ,EAAOK,KAAKF,EAAKC,GAChFE,GAdoC,CAACC,KAASH,KAC9C,MAAMI,EAASR,EAAOK,KAAKE,KAASH,GAEpC,QAAeK,IAAXD,EACF,MAAM,IAAIE,MAAM,SAASC,OAAOJ,kCAGlC,OAAOC"}
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.cts","sourceRoot":"","sources":["../../../src/main.cts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,MAAM,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAiBxE,CAAC"}
1
+ {"version":3,"file":"main.d.cts","sourceRoot":"","sources":["../../../src/main.cts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,MAAM,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAmBxE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,MAAM,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAiBxE,CAAC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAElD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,MAAM,CAAC,KAAG,gBAAgB,CAAC,CAAC,CAmBxE,CAAC"}