Skip to content

Commit

Permalink
feat: 🎉🎉🎉 create translator web site
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshihiro503 committed Oct 31, 2024
1 parent ea18d2d commit 1c288dd
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
_build/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log

# Merlin configuring file for Vim and Emacs
.merlin

# Dune generated files
*.install

# Local OPAM switch
_opam/

# node_modules
node_modules/

# dotenv files
.env
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# アルベド語自動翻訳サイト

https://proof-ninja.github.io/albhed/

## アルベド語とはなんですか

* see: https://www.jp.square-enix.com/column/detail/3/

## Requirement

```console
opam install js_of_ocaml-ppx
```

## How to try this

```console
./compile.sh
open index.html
```
5 changes: 5 additions & 0 deletions bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(executable
(name main)
(modes js)
(libraries uutf)
(preprocess (pps js_of_ocaml-ppx)))
46 changes: 46 additions & 0 deletions bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
open Js_of_ocaml
open Translate

let get_params () : (string * string) list = Url.Current.arguments
let query key params = List.assoc_opt key params

type operator =
Init | Translate of lang * string

let operator_of_params params =
match (query "lang" params , query "ja" params, query "al" params) with
| (Some "al2ja", _, al) ->
let text = Option.value al ~default:"" in
Translate (Albhed_to_Ja, text)
| (Some "ja2al", ja, _) ->
let text = Option.value ja ~default:"" in
Translate (Ja_to_Albhed, text)
| _ -> Init

let init () =
print_endline "init";
(Dom_html.getElementById_exn "tweet")##.innerHTML := Js.string (Tweet_button.show Albhed_to_Ja "マギレヤキセ");
()
let translate lang src =
print_endline "translate";
let dst = Translate.f lang src in
(Dom_html.getElementById_exn "tweet")##.innerHTML := Js.string (Tweet_button.show lang src);
match lang with
| Albhed_to_Ja ->
(Dom_html.getElementById_exn "japanese")##.innerText := Js.string dst;
(Dom_html.getElementById_exn "albhed")##.innerText := Js.string src
| Ja_to_Albhed ->
(Dom_html.getElementById_exn "japanese")##.innerText := Js.string src;
(Dom_html.getElementById_exn "albhed")##.innerText := Js.string dst

let onload _ =
let params = get_params () in
begin match operator_of_params params with
| Init -> init ()
| Translate (lang, text) -> translate lang text
end;
Js._false


let () =
Dom_html.window##.onload := Dom_html.handler onload
41 changes: 41 additions & 0 deletions bin/translate.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type lang = Albhed_to_Ja | Ja_to_Albhed

let table = [ (*c.f. https://www.jp.square-enix.com/column/detail/3/ *)
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", "");
("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
("", ""); ("", ""); ("", ""); ("", ""); ("", "");
]

let albhed_to_ja uchar =
match List.find_opt (fun (_ja, al) -> Utf8.uchar_of_string al = uchar) table with
| None -> uchar
| Some (ja, _) -> Utf8.uchar_of_string ja

let ja_to_albhed uchar =
match List.find_opt (fun (ja, _al) -> Utf8.uchar_of_string ja = uchar) table with
| None -> uchar
| Some (_, al) -> Utf8.uchar_of_string al

let f lang text =
match lang with
| Albhed_to_Ja ->
Utf8.of_string text
|> Utf8.map albhed_to_ja
|> Utf8.to_string
| Ja_to_Albhed ->
Utf8.of_string text
|> Utf8.map ja_to_albhed
|> Utf8.to_string
26 changes: 26 additions & 0 deletions bin/tweet_button.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
open Js_of_ocaml

let button_text = "アルベド語をツイートする"

let hashtag = Url.urlencode "アルベド語翻訳"

let body text =
Printf.sprintf {|%s

翻訳をみる → |} text
|> Url.urlencode

let show lang text =
Printf.printf "Tweet_button.show: '%s'" text;
let link =
let query =
match lang with
| Translate.Albhed_to_Ja -> Url.encode_arguments [("lang", "al2ja"); ("al", text)]
| Ja_to_Albhed -> Url.encode_arguments [("lang", "ja2al"); ("ja", text)]
in
let base = "proof-ninja.github.io/albhed" in
"https://" ^ base ^ "?" ^ query
in
Printf.sprintf {|<a href="https://twitter.com/intent/tweet?hashtags=%s&ref_src=&text=%s&url=%s" class="twitter-share-button">%s</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>|}
hashtag
(body text) (Url.urlencode link) button_text
25 changes: 25 additions & 0 deletions bin/utf8.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type t = string

let fold f init ustr =
Uutf.String.fold_utf_8 (fun acc _i -> function
| `Uchar uchar -> f acc uchar
| `Malformed _s -> acc) init ustr

let map f ustr =
let buf = Buffer.create (String.length ustr) in
fold (fun buf uchar -> Uutf.Buffer.add_utf_8 buf (f uchar); buf) buf ustr
|> Buffer.contents

let uchar_of_string s =
let decoder = Uutf.decoder ~encoding:`UTF_8 (`String s) in
match Uutf.decode decoder with
| `Uchar u -> u
| _ -> failwith "Invalid UTF-8 character"

let string_of_uchar uchar =
let buf = Buffer.create 4 in
Uutf.Buffer.add_utf_8 buf uchar;
Buffer.contents buf

let of_string s = s
let to_string s = s
10 changes: 10 additions & 0 deletions bin/utf8.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type t

val fold : ('a -> Uchar.t -> 'a) -> 'a -> t -> 'a
val map : (Uchar.t -> Uchar.t) -> t -> t

val uchar_of_string : string -> Uchar.t
val string_of_uchar : Uchar.t -> string

val of_string : string -> t
val to_string : t -> string
2 changes: 2 additions & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set -eux
dune build bin/main.bc.js
7 changes: 7 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(alias
(name default)
(deps
bin/main.bc.js
index.html
(glob_files statics/*))
)
26 changes: 26 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(lang dune 3.0)

(name jsoo-alhbed)

(generate_opam_files true)

(source
(github username/reponame))

(authors "Yoshihiro Imai")

(maintainers "Yoshihiro Imai")

(license LICENSE)

(documentation https://url/to/documentation)

(package
(name jsoo-alhbed)
(synopsis "A short synopsis")
(description "A longer description")
(depends ocaml dune js_of_ocaml-ppx uutf)
(tags
(topics "to describe" your project)))

; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html
29 changes: 29 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<script type="text/javascript" src="bin/main.bc.js"></script>
<link href="statics/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="translator">
<div class="container">
<div class="head">
<h2>アルベド語 翻訳</h2>
</div>
<label for="albhed">アルベド語</label><br />
<textarea id="albhed" name="al" placeholder="マギレヤキセ"></textarea><br />

<input type="radio" id="al2ja" name="lang" value="al2ja" checked />
<label for="al2ja">↓アル日翻訳</label>
<input type="radio" id="ja2al" name="lang" value="ja2al" />
<label for="ja2al">↑日アル翻訳</label><br />

<textarea id="japanese" name="ja"></textarea><br />

<div class="message">Message Sent</div>
<button id="submit" type="submit">翻訳!</button>

<div id="tweet"></div>
</div>
</form>
</body>
</html>
Loading

0 comments on commit 1c288dd

Please sign in to comment.