Skip to content

Commit

Permalink
blogging-prompt: preserve language on answers link (#36730)
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro authored Oct 3, 2024
1 parent 4f7fd95 commit 7ec470b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Blogging Prompt: preserve language on answers link
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { __, _x, sprintf } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import clsx from 'clsx';
import './editor.scss';
import { languageToLocale } from '../../shared/locale';
import { usePromptTags } from './use-prompt-tags';

function BloggingPromptEdit( { attributes, noticeOperations, noticeUI, setAttributes } ) {
Expand Down Expand Up @@ -101,9 +102,10 @@ function BloggingPromptEdit( { attributes, noticeOperations, noticeUI, setAttrib
apiFetch( { path } )
.then( prompts => {
const promptData = promptId ? prompts : prompts[ 0 ];
const locale = languageToLocale( siteLanguage );

setAttributes( {
answersLink: promptData.answered_link,
answersLink: promptData.answered_link + `?locale=${ locale }`,
answersLinkText: promptData.answered_link_text,
gravatars: promptData.answered_users_sample.map( ( { avatar } ) => ( { url: avatar } ) ),
promptFetched: true,
Expand Down
22 changes: 22 additions & 0 deletions projects/plugins/jetpack/extensions/shared/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* languageToLocale converts a language tag to an ISO 639 conforming locale string.
*
* @param {string} language - a language tag to be converted, e.g. "en_US".
* @return {string} ISO 639 locale string, e.g. "en".
*/
export function languageToLocale( language ) {
const withCountryCode = [ 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ];

language = language.toLowerCase();
if ( withCountryCode.includes( language ) ) {
language = language.replace( '_', '-' );
} else {
language = language.replace( /([-_].*)$/i, '' );
}

if ( language === '' ) {
return 'en';
}

return language;
}
25 changes: 25 additions & 0 deletions projects/plugins/jetpack/extensions/shared/test/locale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { languageToLocale } from '../locale';

describe( 'languageToLocale', () => {
test( 'empty string should return en', () => {
expect( languageToLocale( '' ) ).toBe( 'en' );
} );

test( 'underscores should be replaced', () => {
expect( languageToLocale( 'pt_BR' ) ).toBe( 'pt-br' );
expect( languageToLocale( 'zh_CN' ) ).toBe( 'zh-cn' );
expect( languageToLocale( 'zh_TW' ) ).toBe( 'zh-tw' );
} );

test( 'country codes should be dropped', () => {
expect( languageToLocale( 'en-GB' ) ).toBe( 'en' );
expect( languageToLocale( 'en_US' ) ).toBe( 'en' );
expect( languageToLocale( 'es-ES' ) ).toBe( 'es' );
} );

test( 'locales should be lowercase', () => {
expect( languageToLocale( 'EN' ) ).toBe( 'en' );
expect( languageToLocale( 'FR' ) ).toBe( 'fr' );
expect( languageToLocale( 'PL' ) ).toBe( 'pl' );
} );
} );

0 comments on commit 7ec470b

Please sign in to comment.