Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active October 14, 2024 22:38

Revisions

  1. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions utils.ts
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    // export function useI18n(locale, path, ...args) {
    // return (path, ...args) => {
    // const keys = path.trim().split('.')
    // const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
    // return typeof fn === 'function' ? fn(...args) : fn
    // export function useI18n(translations) {
    // return (key, ...args) => {
    // const value = key.trim().split('.').reduce((obj, k) => obj?.[k], translations) ?? key
    // return typeof value === 'function' ? value(...args) : value
    // }
    // }
    export function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
  2. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions utils.ts
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,9 @@
    // export function useI18n(locale, path, ...args) {
    // return (path, ...args) => {
    // const keys = path.trim().split('.')
    // const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
    // return typeof fn === 'function' ? fn(...args) : fn
    // }
    // }
    export function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
    export const t = (x, y) => useRoute().meta.t(x, y)
  3. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion utils.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,2 @@
    // 132 bytes 💥
    export function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
    export const t = (x, y) => useRoute().meta.t(x, y)
  4. daliborgogic revised this gist Oct 14, 2024. No changes.
  5. daliborgogic revised this gist Oct 14, 2024. 2 changed files with 3 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    // 132 bytes 💥
    function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
    3 changes: 3 additions & 0 deletions utils.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    // 132 bytes 💥
    export function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
    export const t = (x, y) => useRoute().meta.t(x, y)
  6. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    // 137 bytes 💥
    function useI18n(t,e,...n){return(e,...n)=>{let r=e.trim().split('.'),u=r.reduce((t,e)=>t&&t[e],t);return'function'==typeof u?u(...n):u}}
    // 132 bytes 💥
    function useI18n(t){return(n,...e)=>{const r=n.trim().split('.').reduce(((t,n)=>t?.[n]),t)??n;return'function'==typeof r?r(...e):r}}
  7. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 1 addition and 22 deletions.
    23 changes: 1 addition & 22 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,2 @@
    // 137 bytes 💥
    function useI18n(t,e,...n){return(e,...n)=>{let r=e.trim().split('.'),u=r.reduce((t,e)=>t&&t[e],t);return'function'==typeof u?u(...n):u}}

    // Usage in Nuxt.js
    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr', 'de']
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang)
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    // DE
    // {
    // welcome: 'Willkommen!',
    // hello: x => `Hallo ${x}!`,
    // colors: [
    // 'Weiß',
    // 'Schwarz'
    // ]
    // }
    const t = useI18n(locale)
    to.meta = { t }
    })
    function useI18n(t,e,...n){return(e,...n)=>{let r=e.trim().split('.'),u=r.reduce((t,e)=>t&&t[e],t);return'function'==typeof u?u(...n):u}}
  8. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion i18n.ts
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr', 'de']
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)
    const isLang = supportedLanguages.includes(lang)
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    // DE
  9. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 13 additions and 19 deletions.
    32 changes: 13 additions & 19 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -7,23 +7,17 @@ export default defineNuxtRouteMiddleware(async (to) => {
    const supportedLanguages = ['sr', 'de']
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)

    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    // DE
    // {
    // welcome: 'Willkommen!',
    // hello: x => `Hallo ${x}!`,
    // colors: [
    // 'Weiß',
    // 'Schwarz'
    // ]
    // }
    const t = useI18n(locale)
    to.meta = { t }
    } catch (error) {
    // ToDo: handle error
    console.error(error)
    }
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    // DE
    // {
    // welcome: 'Willkommen!',
    // hello: x => `Hallo ${x}!`,
    // colors: [
    // 'Weiß',
    // 'Schwarz'
    // ]
    // }
    const t = useI18n(locale)
    to.meta = { t }
    })
  10. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // 137 bytes
    // 137 bytes 💥
    function useI18n(t,e,...n){return(e,...n)=>{let r=e.trim().split('.'),u=r.reduce((t,e)=>t&&t[e],t);return'function'==typeof u?u(...n):u}}

    // Usage in Nuxt.js
  11. daliborgogic revised this gist Oct 14, 2024. No changes.
  12. daliborgogic revised this gist Oct 14, 2024. No changes.
  13. daliborgogic revised this gist Oct 14, 2024. 1 changed file with 5 additions and 9 deletions.
    14 changes: 5 additions & 9 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,7 @@
    // 142 bytes
    function useI18n(locale, path, ...args) {
    return (path, ...args) => {
    const keys = path.trim().split('.')
    const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
    return typeof fn === 'function' ? fn(...args) : fn
    }
    }
    // 137 bytes
    function useI18n(t,e,...n){return(e,...n)=>{let r=e.trim().split('.'),u=r.reduce((t,e)=>t&&t[e],t);return'function'==typeof u?u(...n):u}}

    // Usage in Nuxt.js
    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr', 'de']
    @@ -15,6 +10,8 @@ export default defineNuxtRouteMiddleware(async (to) => {

    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    // DE
    // {
    // welcome: 'Willkommen!',
    // hello: x => `Hallo ${x}!`,
    @@ -23,7 +20,6 @@ export default defineNuxtRouteMiddleware(async (to) => {
    // 'Schwarz'
    // ]
    // }
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    const t = useI18n(locale)
    to.meta = { t }
    } catch (error) {
  14. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions i18n.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // 142 bytes
    function useI18n(locale, path, ...args) {
    return (path, ...args) => {
    const keys = path.trim().split('.')
  15. daliborgogic renamed this gist Oct 9, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,14 @@ export default defineNuxtRouteMiddleware(async (to) => {

    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    // {
    // welcome: 'Willkommen!',
    // hello: x => `Hallo ${x}!`,
    // colors: [
    // 'Weiß',
    // 'Schwarz'
    // ]
    // }
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    const t = useI18n(locale)
    to.meta = { t }
  17. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ function useI18n(locale, path, ...args) {
    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr', 'de']
    const allowedRouteNames = ['lang-login', 'lang-terms', 'lang-privacy']
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)

  18. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function useI18n(locale, path, ...args) {

    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr']
    const supportedLanguages = ['sr', 'de']
    const allowedRouteNames = ['lang-login', 'lang-terms', 'lang-privacy']
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)
  19. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 1 addition and 26 deletions.
    27 changes: 1 addition & 26 deletions auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -8,36 +8,11 @@ function useI18n(locale, path, ...args) {

    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr'] // more provided soon
    const supportedLanguages = ['sr']
    const allowedRouteNames = ['lang-login', 'lang-terms', 'lang-privacy']
    const { loggedIn, user } = useUserSession()
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)

    if (!loggedIn.value && !allowedRouteNames.includes(to.name as string)) {
    return navigateTo('/login')
    }
    const isTestRoute = computed(() => (to.name as string).startsWith('lang-test'))
    const isTestMode = computed(() => user.value.mode === 'test')

    const testParam = computed(() => isTestMode.value ? 'test' : '')

    if (to.name === 'lang' && isTestMode.value) {
    return navigateTo({
    name: isTestMode.value ? 'lang-test' : 'lang',
    params: {
    lang: isLang ? lang : ''
    }
    })
    }

    if (isTestRoute.value && testParam.value !== to.params.test) {
    return navigateTo({
    name: to.name,
    params: { ...to.params, test: testParam.value, lang: isLang ? lang : '' }
    })
    }

    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
  20. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ function useI18n(locale, path, ...args) {
    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr'] // more provided soon
    const allowedRouteNames = ['login', 'terms', 'privacy']
    const allowedRouteNames = ['lang-login', 'lang-terms', 'lang-privacy']
    const { loggedIn, user } = useUserSession()
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)
  21. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    export function useI18n(locale, path, ...args) {
    function useI18n(locale, path, ...args) {
    return (path, ...args) => {
    const keys = path.trim().split('.')
    const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
  22. daliborgogic revised this gist Oct 9, 2024. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,11 @@
    export function useI18n(locale, path, ...args) {
    return (path, ...args) => {
    const keys = path.trim().split('.')
    const fn = keys.reduce((prev, curr) => prev && prev[curr], locale)
    return typeof fn === 'function' ? fn(...args) : fn
    }
    }

    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr'] // more provided soon
    @@ -33,7 +41,8 @@ export default defineNuxtRouteMiddleware(async (to) => {
    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    to.meta = { locale }
    const t = useI18n(locale)
    to.meta = { t }
    } catch (error) {
    // ToDo: handle error
    console.error(error)
  23. daliborgogic created this gist Oct 9, 2024.
    41 changes: 41 additions & 0 deletions auth.global.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    export default defineNuxtRouteMiddleware(async (to) => {
    const DEFAUT_LANGUAGE = 'en'
    const supportedLanguages = ['sr'] // more provided soon
    const allowedRouteNames = ['login', 'terms', 'privacy']
    const { loggedIn, user } = useUserSession()
    const { lang } = to.params
    const isLang = supportedLanguages.includes(lang as string)

    if (!loggedIn.value && !allowedRouteNames.includes(to.name as string)) {
    return navigateTo('/login')
    }
    const isTestRoute = computed(() => (to.name as string).startsWith('lang-test'))
    const isTestMode = computed(() => user.value.mode === 'test')

    const testParam = computed(() => isTestMode.value ? 'test' : '')

    if (to.name === 'lang' && isTestMode.value) {
    return navigateTo({
    name: isTestMode.value ? 'lang-test' : 'lang',
    params: {
    lang: isLang ? lang : ''
    }
    })
    }

    if (isTestRoute.value && testParam.value !== to.params.test) {
    return navigateTo({
    name: to.name,
    params: { ...to.params, test: testParam.value, lang: isLang ? lang : '' }
    })
    }

    try {
    const filename = isLang ? lang : DEFAUT_LANGUAGE
    const { default: locale } = await import(`./../i18n/${to.name}/${filename}.js`)
    to.meta = { locale }
    } catch (error) {
    // ToDo: handle error
    console.error(error)
    }
    })