Giter VIP home page Giter VIP logo

transliterator's Introduction

Golang text Transliterator

Build Status Coverage Status Go Report Card Codacy Badge

Golang Transliterator provides one-way string transliteration. It takes Unicode text and converts to ASCII characters. Example use-case: transliterate cyrilic city name to be able to use it in the url ("Київ" ==> "Куiv").

For now, only these languages have specific transliteration rules: DE, DA, EO, RU, BG, SV, HU, HR, SL, SR, NB, UK, MK, CA, BS. For other languages, general ASCII transliteration rules will be applied. Also, this package supports adding custom transliteration rules for your specific use-case. Please check the examples section below.

Installation

go get -u github.com/alexsergivan/transliterator

Language specific transliteration example

package main

import (
	"fmt"
	"github.com/alexsergivan/transliterator"
)

func main() {
	trans := transliterator.NewTransliterator(nil)
	text := "München"
	// Langcode should be provided accrding to ISO 639-1.
	fmt.Println(trans.Transliterate(text, "de")) // Result: Muenchen
	fmt.Println(trans.Transliterate(text, "en")) // Result: Munchen

	anotherText := "你好"
	fmt.Println(trans.Transliterate(anotherText, "")) // Result: Ni Hao

	oneMoreText := "Київ"
	fmt.Println(trans.Transliterate(oneMoreText, "uk")) // Result: Kyiv
	fmt.Println(trans.Transliterate(oneMoreText, "en")) // Result: Kiyiv
	fmt.Println(trans.Transliterate(oneMoreText, "")) // Result: Kiyiv
}

Adding of custom Language translitartion rules

package main

import (
	"fmt"
	"github.com/alexsergivan/transliterator"
)

func main() {
	customLanguageOverrites := make(map[string]map[rune]string)

	customLanguageOverrites["myLangcode"] = map[rune]string{
		// Ї
		0x407: "CU",
		// и
		0x438: "y",
	}
	trans := transliterator.NewTransliterator(&customLanguageOverrites)
	text := "КиЇв"
	fmt.Println(trans.Transliterate(text, "myLangcode")) // Result: KyCUv

}

transliterator's People

Contributors

alexsergivan avatar srlehn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.