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

docs:web arch translation account share #72

Merged
merged 7 commits into from
Jan 30, 2024
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
43 changes: 30 additions & 13 deletions docs/architecture/01_architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@

Go+ Community is a community for Go+ developers. It provides a platform for developers to share their knowledge and experience, and to communicate with each other.It contains the following modules:

- Account Module
- Article Module
- Markdown Module
- Translate Module
- Share Module
- Web Module
- Account Module: User login and logout
- Article Package: Article CRUD
- Markdown Component: Markdown Editor
- Translate Package: Markdown and Video Translation
- Media Package: Media Management
- Share Component: Share to Twitter or Facebook
- Web Module: Web UI

## Architecture Design

![Architecture](../assets/community_architecture.png)
Community is a web application based on `yap`, the interaction of each module is as follows:

![Interaction](../assets/01_module_interaction_2.png)

The main interaction process is as surrouned by user interaction.

User info is stored in the database, and the user info is used to verify the user's login status and parse the user info. And get the OAuth login authentication information from github twitter facebook and store the user information temporarily in a cookie.

The article module is the core of the goplus community, and it not only connects the user module, but also integrates the article translation and resource upload module. User can create, update, delete and query articles when they login, then the article will be stored in the database. The article operation involves the storage of HTML files, it also needs to be connected to Qiniu cloud storage.

The markdown component is used to edit the article, support markdown syntax and preview. Especially, our markdown editor supports goplus syntax highlighting, markdown translation and video translation, easy to paste pictures and videos.

The share component is used to share the article to Twitter or Facebook.

The Media package is used to manage resources to the cloud storage(QiNiu Cloud Storage). It can upload, delete and query resources.

The web module is the front-end of the community, which is used to display the community interface. It is based on yap template.

Community is a web application based on `yap`,

## Module list

- [Account Module](./02_account_module.md)
- [Article Module](./03_article_module.md)
- [Markdown Module](./04_markdown_module.md)
- [Translate Module](./05_translation_module.md)
- [Share Module](./06_share_module.md)
- [Web Module](./07_web_module.md)
- [Article Package](./03_article_package.md)
- [Markdown Component](./04_markdown_component.md)
- [Translate Package](./05_translation_package.md)
- [Share Component](./06_share_component.md)
- [Media Package](./07_media_package.md)
- [Web Module](./08_web_module.md)
155 changes: 0 additions & 155 deletions docs/architecture/05_translation_module.md

This file was deleted.

179 changes: 179 additions & 0 deletions docs/architecture/05_translation_package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Translation module

## Module purpose

This module is used to translate the multilingual markdown file into english version.

## Module scope

This module get raw markdown file from input, and translate it into english version with the help of [Niu Translation API](niutrans.com).

## Module structure

When the request translation starts, it will facilitate the text position and start-stop Index of each node in markdown, split the text with random separators, send it to markdown translator, and finally restore the obtained results to the text according to Index.

The pseudocode is as follows:

```go
func TranslateMarkdown(src, from, to) {
// Step 1: Initialize markdown parser
md := InitializeMarkdownParser()
doc := ParseMarkdown(md, src)

// Step 2: Prepare translation
translationVec, translationSeg = PrepareTranslation()

// Step 3: Walk through the AST
WalkThroughAST(doc, translationVec, translationSeg)

// Step 4: Translate
translatedStr = TranslateText(translationVec, from, to)

// Step 5: Replace text
result = ReplaceText(src, translatedStr, translationSeg)

// Step 6: Return result
return result
}
```

## Module Interface

None.

## Functions

### New

```go
type Engine struct {
// Has unexported fields.
}
Engine is the config of translation server

func New(translationAPIKey string, qiniuAccessKey string, qiniuSecretKey string) *Engine
New create a new TranslateConfig
```

**Example:**

```go
func TestNew(t *testing.T) {
e := New(mockKey, "", "")
if e == nil {
t.Fatal("new engine failed")
}
}
```

### Text2Audio

```go
func (e *Engine) Text2Audio(content string) (*TTSResponse, error)
Text2Audio
```

**Example:**

```go
func TestText2Audio(t *testing.T) {
e := New("", mockAccessKey, mockSecretKey)
resp, err := e.Text2Audio("hello")
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}
```

### TranslateBatchPlain

```go
func (e *Engine) TranslateBatchPlain(src []string, from, to language.Tag) ([]string, error)
TranslateBatchSeq translate a series of sequences of text
```

**Example:**

```go
func TestTranslateBatchPlain(t *testing.T) {
e := New(mockKey, "", "")
src := []string{"你好", "hello"}
ret, err := e.TranslateBatchPlain(src, language.Chinese, language.English)
if err != nil {
t.Fatal(err)
}
t.Log(ret)
}
```

### TranslateMarkdown

```go
func (e *Engine) TranslateMarkdown(src []byte, from, to language.Tag) (ret []byte, err error)
TranslateMarkdown translate markdown with bytes

func (e *Engine) TranslateMarkdownText(src string, from, to language.Tag) (ret string, err error)
TranslateMarkdown translate markdown text
```

**Example:**

```go
func TestTranslateMarkdown(t *testing.T) {
tests := []struct {
src string
from language.Tag
to language.Tag
}{
{`# Hello, World!`, language.English, language.Chinese},
}

trans := New(mockKey, "", "")
for _, test := range tests {
translatedResult, err := trans.TranslateMarkdownText(test.src, test.from, test.to)
fmt.Println(translatedResult, err)
if err != nil {
t.Fatal(err)
}
}
}
```

### TranslatePlain

```go
func (e *Engine) TranslatePlain(src []byte, from, to language.Tag) (ret []byte, err error)
Translate translate sequence of bytes

func (e *Engine) TranslatePlainText(src string, from, to language.Tag) (ret string, err error)
Translate translate sequence of text
```

**Example:**

```go
func TestTranslatePlainText(t *testing.T) {
if mockKey == "" {
t.Skip("NIUTRANS_API_KEY not set")
}

tests := []struct {
src string
from language.Tag
to language.Tag
}{
{"你好", language.Chinese, language.English},
{"hello", language.English, language.Chinese},
}

trans := New(mockKey, "", "")
for _, test := range tests {
to, err := trans.TranslatePlainText(test.src, test.from, test.to)
fmt.Println(to, err)
if err != nil {
t.Fatal(err)
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ var intentUrl = "http://www.facebook.com/sharer/sharer.php?"
window.open(intentUrl, "_blank", "width=550,height=420");
}

```
```
Loading