Skip to content

slugify/slugify

Repository files navigation

Slugify

GitHub license Check javadoc

Description

Small utility library for generating speaking URLs.

Usage Examples

Slugify is published in the Maven Central Repository: Click here

Basic

final Slugify slg = Slugify.builder().build();
final String result = slg.slugify("Hello, world!");
// result: hello-world

Underscore Separator

final Slugify slg = Slugify.builder().underscoreSeparator(true).build();
final String result = slg.slugify("Hello, world!");
// result: hello_world

Preserve Case

final Slugify slg = Slugify.builder().lowerCase(false).build();
final String result = slg.slugify("Hello, world!");
// result: Hello-world

Specifying a Locale

final Slugify slg = Slugify.builder().locale(Locale.GERMAN).build();
final String result = slg.slugify("ä");
// result: ae

Built-in character replacements exist for a subset of locales (see src/main/resources/slugify_*.properties). For all other locales, no built-in replacements are applied — custom replacements or transliteration can be used instead.

Custom Replacements

// provided as single key-value pairs
final Slugify slg = Slugify.builder()
    .customReplacement("Foo", "Hello")
    .customReplacement("bar", "world")
    .build();

// alternatively provided as a map
final Slugify slg = Slugify.builder()
    .customReplacements(Map.of("Foo", "Hello", "bar", "world"))
    .build();

final String result = slg.slugify("Foo, bar!");
// result: hello-world

Custom replacements take precedence over built-in locale replacements and are applied first.

Transliteration

Requirements

Gradle

There's a feature variant which can be used as follows:

capabilities {
    requireCapability('com.github.slugify:slugify-transliterator')
}

For more information about feature variants please check the section How to Create Feature Variants for a Library in Gradle.

Other

Manually add the optional dependency com.ibm.icu:icu4j to your project.

Usage Example

final Slugify slg = Slugify.builder().transliterator(true).build();
final String result = slg.slugify("Б");
// result: b

Contributors

Languages