Repository navigation
fix(url,editor): locale-aware slug preview for German umlauts and other digraphs #76248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,44 @@ | |
| */ | ||
| import removeAccents from 'remove-accents'; | ||
|
|
||
| /** | ||
| * Locale-specific digraph replacements that must run before removeAccents(), | ||
| * mirroring the locale block in PHP's remove_accents() (wp-includes/formatting.php). | ||
| */ | ||
| const LOCALE_CHAR_MAP: Record< string, Record< string, string > > = { | ||
| de: { | ||
| Ä: 'Ae', | ||
| ä: 'ae', | ||
| Ö: 'Oe', | ||
| ö: 'oe', | ||
| Ü: 'Ue', | ||
| ü: 'ue', | ||
| ẞ: 'SS', | ||
| ß: 'ss', | ||
| }, | ||
| da_DK: { | ||
| Æ: 'Ae', | ||
| æ: 'ae', | ||
| Ø: 'Oe', | ||
| ø: 'oe', | ||
| Å: 'Aa', | ||
| å: 'aa', | ||
| }, | ||
| ca: { 'l·l': 'll' }, | ||
| sr_RS: { Đ: 'DJ', đ: 'dj' }, | ||
| bs_BA: { Đ: 'DJ', đ: 'dj' }, | ||
| }; | ||
|
|
||
| function getLocaleChars( | ||
| locale: string | ||
| ): Record< string, string > | undefined { | ||
| // str_starts_with( $locale, 'de' ) covers de_DE, de_CH, de_AT, etc. | ||
| if ( locale.startsWith( 'de' ) ) { | ||
| return LOCALE_CHAR_MAP.de; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just noticed this late in the review, but BCP47 specifies how to match these, see also the W3C spec. so I think we would want to do something like this, just to avoid problem sin the future with added locales const mostSpecificLocaleTag = ( objectWithTagKeys, givenTag ) => {
if ( objectWithTagKeys.hasOwnProperty( givenTag ) ) {
return givenTag;
}
// @todo verify the difference between _ and - here, with Core
const lastSeparatorAt = givenTag.lastIndexOf( '_' );
if ( -1 === lastSeparatorAt ) {
return null;
}
return mostSpecificLocaleTag( objectWithTagKeys, givenTag.slice( 0, lastSeparatorAt );
}and then… const mostSpecificLocale = mostSpecificLocalTag( LOCALE_CHAR_MAP, locale );
return mostSpecificLocale
? LOCALE_CHAR_MAP[ mostSpecificLocale ]
: null;that was nobody comes in and adds |
||
| return LOCALE_CHAR_MAP[ locale ]; | ||
| } | ||
|
|
||
| /** | ||
| * Performs some basic cleanup of a string for use as a post slug. | ||
| * | ||
|
|
@@ -16,13 +54,24 @@ import removeAccents from 'remove-accents'; | |
| * for octets, HTML entities, or other encoded characters. | ||
| * | ||
| * @param string Title or slug to be processed. | ||
| * @param locale Optional BCP 47 / WordPress locale string (e.g. 'de_DE'). | ||
| * When provided, locale-specific digraph replacements are applied | ||
| * before generic accent removal, mirroring PHP's remove_accents(). | ||
| * | ||
| * @return Processed string. | ||
| */ | ||
| export function cleanForSlug( string: string ): string { | ||
| export function cleanForSlug( string: string, locale = '' ): string { | ||
| if ( ! string ) { | ||
| return ''; | ||
| } | ||
|
|
||
| const localeChars = getLocaleChars( locale ); | ||
| if ( localeChars ) { | ||
| for ( const [ char, replacement ] of Object.entries( localeChars ) ) { | ||
| string = string.replaceAll( char, replacement ); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| removeAccents( string ) | ||
| // Convert  , &ndash, and &mdash to hyphens. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Comparison script: cleanForSlug( string, locale ) vs PHP sanitize_title() | ||
| * | ||
| * Run with: npm run test:unit packages/url/src/test/compare-slug-php-parity.test.js | ||
| * | ||
| * PHP results were generated with: | ||
| * npm run wp-env run cli -- wp eval \ | ||
| * 'foreach($cases as [$l,$i]){echo sanitize_title_with_dashes(remove_accents($i,$l))."\n";}' | ||
| */ | ||
| import { cleanForSlug } from '../clean-for-slug'; | ||
|
|
||
| const cases = [ | ||
| // German — de_DE | ||
| { | ||
| locale: 'de_DE', | ||
| input: 'Künstler überraschen Hörer', | ||
| php: 'kuenstler-ueberraschen-hoerer', | ||
| }, | ||
| { | ||
| locale: 'de_DE', | ||
| input: 'Straße und Öffentlichkeit', | ||
| php: 'strasse-und-oeffentlichkeit', | ||
| }, | ||
| { | ||
| locale: 'de_DE', | ||
| input: 'Ärger mit Übergängen', | ||
| php: 'aerger-mit-uebergaengen', | ||
| }, | ||
| // German — capital Eszett (ẞ, U+1E9E), standardized in 2017 (DIN 5008) | ||
| { locale: 'de_DE', input: 'STRAẞE', php: 'strasse' }, | ||
| // German variants — same digraph rules apply | ||
| { locale: 'de_CH', input: 'Zürich Straße', php: 'zuerich-strasse' }, | ||
| { locale: 'de_AT', input: 'Österreich', php: 'oesterreich' }, | ||
| // Danish | ||
| { locale: 'da_DK', input: 'Æble Ødemark Åre', php: 'aeble-oedemark-aare' }, | ||
| // Catalan | ||
| { locale: 'ca', input: 'col·legi', php: 'collegi' }, | ||
| // Serbian / Bosnian | ||
| { locale: 'sr_RS', input: 'Đorđe', php: 'djordje' }, | ||
| { locale: 'bs_BA', input: 'Đakovo', php: 'djakovo' }, | ||
| // No locale — generic diacritic stripping only (ä → a, not ae) | ||
| { locale: '', input: 'Künstler', php: 'kunstler' }, | ||
| ]; | ||
|
|
||
| describe( 'cleanForSlug() matches PHP sanitize_title() for locale-specific digraphs', () => { | ||
| test.each( cases )( | ||
| '[$locale] "$input" → "$php"', | ||
| ( { locale, input, php } ) => { | ||
| expect( cleanForSlug( input, locale ) ).toBe( php ); | ||
| } | ||
| ); | ||
| } ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when no locale is provided, would we want to set the default to the one provided by the browser?
things are odd here between site locale and browser locale, but probably there’s a good chance they are the same.