Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api for managing locale messages #679

Merged
merged 5 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/fluent-vue-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fluent-vue-cli
=================

fluent-vue tool for managing locale messages

WIP
24 changes: 24 additions & 0 deletions packages/fluent-vue-cli/__tests__/fixtures/Multiple.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div>
{{ $t('greeting', { name: 'World' }) }}
</div>
</template>

<script>
export default {
}
</script>

<fluent locale="en">
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
</fluent>

<fluent locale="uk">
user-name = Світ
aria-key = Значення aria
greeting = Привіт, {$name}
.aria-label = Значення мітки
</fluent>
17 changes: 17 additions & 0 deletions packages/fluent-vue-cli/__tests__/fixtures/Simple.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div>
{{ $t('greeting', { name: 'World' }) }}
</div>
</template>

<script>
export default {
}
</script>

<fluent locale="en">
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
</fluent>
52 changes: 52 additions & 0 deletions packages/fluent-vue-cli/__tests__/getFtlMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getFtlMessages } from '../src'

describe('getVueMessages', () => {
it('extracts messages from ftl file', async () => {
// Arrange
const source = `
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
`

// Act
const messages = getFtlMessages(source)

// Assert
expect(messages).toMatchInlineSnapshot(`
Object {
"aria-key": "Aria value",
"greeting": "Hello, { $name }
.aria-label = Label value",
"user-name": "World",
}
`)
})

it('ignores comments', async () => {
// Arrange
const source = `
## Group comment

# Inline comment
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
`

// Act
const messages = getFtlMessages(source)

// Assert
expect(messages).toMatchInlineSnapshot(`
Object {
"aria-key": "Aria value",
"greeting": "Hello, { $name }
.aria-label = Label value",
"user-name": "World",
}
`)
})
})
113 changes: 113 additions & 0 deletions packages/fluent-vue-cli/__tests__/getVueMessages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { readFile } from 'fs/promises'
import { resolve } from 'path'

import { getVueMessages, MessagesWithLocale } from '../src'

describe('getVueMessages', () => {
it('extracts locale messages from SFC', async () => {
// Arrange
const source = await readFile(resolve(__dirname, 'fixtures', './Simple.vue'))

// Act
const messages = getVueMessages(source.toString())

// Assert
expect(messages).toHaveLength(1)
expect(messages).toMatchInlineSnapshot(`
Array [
Object {
"locale": "en",
"messages": Object {
"aria-key": "Aria value",
"greeting": "Hello, { $name }
.aria-label = Label value",
"user-name": "World",
},
},
]
`)
})

it('extracts multiple SFC blocks', async () => {
// Arrange
const source = await readFile(resolve(__dirname, 'fixtures', './Multiple.vue'))

// Act
const messages = getVueMessages(source.toString())

// Assert
expect(messages).toHaveLength(2)
expect(messages).toMatchInlineSnapshot(`
Array [
Object {
"locale": "en",
"messages": Object {
"aria-key": "Aria value",
"greeting": "Hello, { $name }
.aria-label = Label value",
"user-name": "World",
},
},
Object {
"locale": "uk",
"messages": Object {
"aria-key": "Значення aria",
"greeting": "Привіт, { $name }
.aria-label = Значення мітки",
"user-name": "Світ",
},
},
]
`)
})

it('throws if fluent block does not have locale', () => {
// Arrange
const source = `
<fluent>
key = value
</fluent>
`

// Act
const func = (): MessagesWithLocale[] => getVueMessages(source)

// Assert
expect(func).toThrowError('fluent custom block does not have locale specified')
})

it('ignores non-fluent blocks', () => {
// Arrange
const source = `
<fluent locale="en">
key = value
fluent-key = fluent value
</fluent>

<i18n>
{
"en": {
"key": "value",
"i18n-key": "i18n value"
}
}
</i18n>
`

// Act
const messages = getVueMessages(source)

// Assert
expect(messages).toMatchInlineSnapshot(`
Array [
Object {
"locale": "en",
"messages": Object {
"fluent-key": "fluent value",
"key": "value",
},
},
]
`)
})
})
80 changes: 80 additions & 0 deletions packages/fluent-vue-cli/__tests__/mergeFtl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { mergeFtl } from '../src'

describe('mergeFtl', () => {
it('adds new key/values', () => {
// Arrange
const source = `
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
`
const newTranslation = { hello: 'Hello' }

// Act
const newSource = mergeFtl(source, newTranslation)

// Assert
expect(newSource).toMatchInlineSnapshot(`
"user-name = World
aria-key = Aria value
greeting = Hello, { $name }
.aria-label = Label value
hello = Hello
"
`)
})

it('preserves comments', () => {
// Arrange
const source = `
## Group comment

# Inline comment
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
`
const newTranslation = { 'user-name': 'John Doe' }

// Act
const newSource = mergeFtl(source, newTranslation)

// Assert
expect(newSource).toMatchInlineSnapshot(`
"## Group comment

# Inline comment
user-name = John Doe
aria-key = Aria value
greeting = Hello, { $name }
.aria-label = Label value
"
`)
})

it('changes value if key is already present', () => {
// Arrange
const source = `
user-name = World
aria-key = Aria value
greeting = Hello, {$name}
.aria-label = Label value
`

const existingTranslation = { 'user-name': 'Jorn Doe' }

// Act
const newSource = mergeFtl(source, existingTranslation)

// Assert
expect(newSource).toMatchInlineSnapshot(`
"user-name = Jorn Doe
aria-key = Aria value
greeting = Hello, { $name }
.aria-label = Label value
"
`)
})
})
Loading