[][src]Crate opencc_rust

Open Chinese Convert(OpenCC, 開放中文轉換) binding for the Rust language for conversion between Traditional Chinese and Simplified Chinese.

Compilation

To compile this crate, you need to compile the OpenCC C++ library first. You can install OpenCC in your operating system, or in somewhere in your file system. As for the latter, you need to set the following environment variables to link the OpenCC library:

  • OPENCC_LIB_DIRS: The directories of library files, like -L. Use : to separate.
  • OPENCC_LIBS: The library names that you want to link, like -l. Use : to separate. Typically, it contains opencc:marisa.
  • OPENCC_INCLUDE_DIRS: The directories of header files, like -i. Use : to separate.
  • OPENCC_STATIC: Whether to use static or dylib.
  • OPENCC_DYLIB_STDCPP: If you use static linking, and your OpenCC library is compiled by the GNU C, this environment variable should be set.

Examples

extern crate opencc_rust;

use opencc_rust::*;

let opencc = OpenCC::new(DefaultConfig::TW2SP).unwrap();

let s = opencc.convert("涼風有訊");

assert_eq!("凉风有讯", &s);

let s = opencc.convert_to_buffer(",秋月無邊", s);

assert_eq!("凉风有讯,秋月无边", &s);
extern crate opencc_rust;

use opencc_rust::*;

let opencc = OpenCC::new(DefaultConfig::S2TWP).unwrap();

let s = opencc.convert("凉风有讯");

assert_eq!("涼風有訊", &s);

let s = opencc.convert_to_buffer(",秋月无边", s);

assert_eq!("涼風有訊,秋月無邊", &s);

Static Dictionaries

Usually, OpenCC needs to be executed on an environment where OpenCC is installed. If you want to make it portable, you can enable the static-dictionaries feature.

[dependencies.opencc-rust]
version = "*"
features = ["static-dictionaries"]

Then, the generate_static_dictionary and generate_static_dictionaries functions are available.

The default OpenCC dictionaries will be compiled into the binary file by lazy_static_include crate. And you can use the two functions to recover them on demand.

For example,

This example is not tested
extern crate opencc_rust;

use opencc_rust::*;

let output_path = "/path/to/dictionaries-directory";

generate_static_dictionary(&output_path, DefaultConfig::TW2SP).unwrap();

let opencc = OpenCC::new(Path::join(&output_path, DefaultConfig::TW2SP)).unwrap();

assert_eq!("凉风有讯", &opencc.convert("涼風有訊"));

Structs

OpenCC

OpenCC binding for Rust.

Enums

DefaultConfig

Default configs.

Functions

generate_static_dictionaries

Generate files for specific dictionaries. These files are used for opening a new OpenCC instance.

generate_static_dictionary

Generate files for a specific dictionary. These files are used for opening a new OpenCC instance.

opencc_close
opencc_convert_utf8
opencc_convert_utf8_to_buffer
opencc_convert_utf8_free
opencc_error
opencc_open