1.0.28 • Published 3 years ago

japhy-i18n v1.0.28

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

i18n

Fetch your Prismic translations per page and retrieve them easily.

Installation

First install the package

yarn add japhy-i18n

Use it in a vue plugin

import Vue from 'vue'
import i18n from 'japhy-i18n'
import { router } from '@/core/router'

Vue.use(i18n, {
  clientUrl: 'https://japhy.cdn.prismic.io/api/v2',
  router,
})

Declare you translated pages in your routes

const routes = [{
  path: '/',
  name: 'homepage',
  i18nPages: [
    'my_page', // my prismic document
    'my_second_page'
  ],
}]

Usage

With vue2 this.$i18n method will be available in context and will automatically retrieve the provided key from the i18n page.

With vue3 you can also use a hook e.g

<template>
  <div>{{ i18n('my_translation_key') }}</div>
</template>

<script>
import { useTranslation } from 'japhy-i18n'

export default {
  setup() {
    const i18n = useTranslation()

    return { i18n }
  }
}
</script>

This method uses icu to format the translations. Here is the api

Variables

// my_key_from_prismic = 'My name is {name}'

this.$i18n('my_key_from_prismic', { name: 'Eric' }) // -> My name is Eric

Plural

// my_key_from_prismic = 'You have {numPhotos, plural,
//   =0 {no photos.}
//   =1 {one photo.}
//   other {# photos.}
// }'

this.$i18n('my_key_from_prismic', { numPhotos: 1000 }) // -> You have 1,000 photos.

Chunks

<script>/* my_key_from_prismic = "My name is <name>jack l'ancien</name> who's <age>8</age> years old only" */</script>

<template>
  <RichTranslation translation="key"> 
    <template #name="name">
      <h1>{{ name }}</h1>
    </template>
    <template #age="age">
      <i>{{ age }}</i>
    </template>
  </RichTranslation>
</template>

<script>/* -> My name is <h1>jack l'ancien</h1> who's <i>8</i> years old only */</script>

Chunks from different translations

<script>
// my_key_from_prismic = "My name is <name></name> who's <age></age> years old only"
// my_second_key_from_prismic = "jack le gars grave sur"
</script>

<template>
  <RichTranslation translation="key">
    <template #name>
      <h1>{{ $i18n('my_second_key_from_prismic') }}</h1>
    </template>
    <template #age="age">
      <i>{{ age }}</i>
    </template>
  </RichTranslation>
</template>

<script>/* -> My name is <h1>jack le gars grave sur</h1> who's <i>8</i> years old only */</script>

Price

// my_key_from_prismic = "The price is: {price, number, ::currency/EUR}"

this.$i18n('my_key_from_prismic', { price: 100 }) // -> The price is: 100 €

Date

// my_key_from_prismic = "Today is: {now, date, ::yyyyMMdd}'"

this.$i18n('my_key_from_prismic', { now: new Date() }) // ->Today is: 16/06/2021

ProTip : You can use tampermonkey to get color syntax highlight in prismic (with icu language) with this script https://gist.github.com/blaadje/d5c054b85033a7c96ec80dfc7c0d47a1

image

With error parser

image

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.16

3 years ago

1.0.11

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago