romanize 0.0.1 copy "romanize: ^0.0.1" to clipboard
romanize: ^0.0.1 copied to clipboard

A lightweight Dart package for converting Korean, Japanese, Chinese, Cyrillic, and Arabic text to Romanized form with auto-detection support.

A powerful Dart package for seamlessly converting multilingual text into its Romanized form.

Features #

  • 🌏 Multi-language support: Korean, Japanese, Chinese, Cyrillic, and Arabic
  • πŸ” Auto-detection: Automatically detects the languages present in the input text
  • πŸ› οΈ Flexible & extensible: Easily create your own custom romanizer for any language or writing system
  • πŸ“¦ Lightweight: Minimal dependencies, fast performance

Installation #

Add romanize to your pubspec.yaml:

dependencies:
  romanize: ^0.0.1

Then run:

dart pub get

Usage #

Import the package:

import 'package:romanize/romanize.dart';

Romanize Text #

The romanize method automatically detects and romanizes each word separately, making it perfect for multi-language text:

// Multi-language text - each word is detected and romanized independently
final text = 'δ½ ε₯½ Hello μ•ˆλ…•';
final romanized = TextRomanizer.romanize(text);
print(romanized); // ni hao Hello annyeong

// Single language text also works
final koreanText = 'μ•ˆλ…•ν•˜μ„Έμš”';
final koreanRomanized = TextRomanizer.romanize(koreanText);
print(koreanRomanized); // annyeonghaseyo

It will fail to detect multiple languagues if they are not separated by spaces.

Detect Language #

Detect the first language present in the text:

final romanizer = TextRomanizer.detectLanguage('μ•ˆλ…•ν•˜μ„Έμš”');
print(romanizer.language); // korean

Or detect all languages present in the text:

final romanizers = TextRomanizer.detectLanguages('μ•ˆλ…• Hello δ½ ε₯½ ΠŸΡ€ΠΈΠ²Π΅Ρ‚ ΠΌΠΈΡ€');
print(romanizers.map((r) => r.language)); // {korean, chinese, cyrillic}

Specify Language #

You can also specify the language explicitly using the forLanguage method:

final japaneseText = 'こんにけは';
final japaneseRomanizer = TextRomanizer.forLanguage('japanese');
print(japaneseRomanizer.romanize(japaneseText)); // konnichiwa

Or you can instantiate the romanizer directly:

final chineseText = 'δ½ ε₯½';
final chineseRomanizer = ChineseRomanizer(toneAnnotation: ToneAnnotation.mark);
print(chineseRomanizer.romanize(chineseText)); // nǐ hǎo

Some romanizers have additional options. For example, the ChineseRomanizer has the toneAnnotation option to specify the tone annotation to use.

Supported Languages #

  • Korean (ν•œκ΅­μ–΄)
  • Japanese (ζ—₯本θͺž) - Using kana_kit for Kana conversion
  • Chinese (δΈ­ζ–‡) - Using pinyin for Pinyin conversion (Simplified and Traditional)
  • Cyrillic (ΠšΠΈΡ€ΠΈΠ»Π»ΠΈΡ†Π°) - Custom transliteration for Russian, Ukrainian, Serbian, and more
  • Arabic (Ψ§Ω„ΨΉΨ±Ψ¨ΩŠΨ©) - Custom transliteration based on ISO 233 and DIN 31635

API Reference #

TextRomanizer #

Main class for romanizing text.

Static Methods

  • romanize(String input) - Processes each word separately, auto-detecting and romanizing each word. Perfect for multi-language text.
  • detectLanguage(String input) - Detects the first matching language and returns the corresponding Romanizer. Returns EmptyRomanizer if no match is found.
  • detectLanguages(String input) - Detects all matching languages and returns a Set<Romanizer>. Returns empty set if no matches are found.
  • forLanguage(String language) - Returns a Romanizer for the specified language. Throws UnimplementedError if not found.
  • forLanguageOrNull(String? language) - Returns a Romanizer? for the specified language, or null if not found.
  • supportedLanguages - Returns a list of all supported language names.

Romanizer #

Interface for language-specific romanizers.

  • language - The language name (e.g., 'korean', 'japanese', 'arabic')
  • isValid(String input) - Checks if the input is valid for this romanizer
  • romanize(String input) - Converts the input to Romanized form

Example #

See the example directory for a complete example.

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

Creating a Custom Romanizer #

To create a custom romanizer for a new language or writing system, you can extend the Romanizer class and implement the romanize and isValid methods.

class EmojiRomanizer extends Romanizer {
  const EmojiRomanizer() : super(language: 'emoji');

  static const Map<String, String> _transliterationMap = {
    'πŸ‘‹': 'wave',
    '🌍': 'earth',
    'πŸš€': 'rocket',
    'πŸŽ‰': 'party',
  };

  @override
  bool isValid(String input) {
    return RegExp(r'[\uD800-\uDBFF][\uDC00-\uDFFF]').hasMatch(input);
  }

  @override
  String romanize(String input) {
    final buffer = StringBuffer();
    for (final char in input.runes) {
      final charString = String.fromCharCode(char);
      if (isValid(charString)) {
        if (_transliterationMap.containsKey(charString)) {
          buffer.write(':${_transliterationMap[charString]}:');
        } else {
          buffer.write(':$charString:');
        }
      } else {
        buffer.write(charString);
      }
    }
    return buffer.toString();
  }
}

Then you can use your custom romanizer like this:

final emojiText = 'πŸ‘‹ 🌍 πŸš€ πŸŽ‰ πŸ’œ';
final emojiOutput = EmojiRomanizer().romanize(emojiText);
print('Emoji Romanization: \n$emojiOutput'); // :wave: :earth: :rocket: :party: :πŸ’œ:
7
likes
0
points
335
downloads

Publisher

verified publisherbdlukaa.dev

Weekly Downloads

A lightweight Dart package for converting Korean, Japanese, Chinese, Cyrillic, and Arabic text to Romanized form with auto-detection support.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

kana_kit, pinyin

More

Packages that depend on romanize