-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blogging-prompt: preserve language on answers link (#36730)
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/preserve-language-on-answers-link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
projects/plugins/jetpack/extensions/shared/test/locale.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} ); | ||
} ); |