/** * Languages of a form: the section in the form's properties (add and remove languages, the * visitors' language choice, what is not translated yet) and the choice in the top bar of the * language the texts are edited in. */ import React from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import { localeTag } from '@funnelforms/form-core'; import { addLanguage, languageName, LANGUAGES, missingTranslations, removeLanguage } from '../languages'; import type { Recipe } from '../store'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { Button, icons, Select, Toggle } from './ui'; /** The language of the admin, for the names of the languages. */ function adminLocale(): string { return localeTag( document.documentElement.lang ) ?? 'en'; } /** The name of a language in the admin's language. */ export function languageLabel( locale: string ): string { return name( locale ); } function name( locale: string ): string { return languageName( locale, adminLocale(), __( 'formal', 'funnelforms-pro' ) ); } function useChange(): ( recipe: Recipe, key?: string ) => void { return useEnv().store.getState().change; } export function LanguagesSection( { doc }: { doc: FormDocument } ) { const { readOnly, store } = useEnv(); const change = useChange(); const base = doc.locales.default; const others = doc.locales.available.filter( ( l ) => l !== base ); const addable = LANGUAGES.filter( ( l ) => ! doc.locales.available.includes( l ) && l !== base ); return (

{ __( 'Languages', 'funnelforms-pro' ) }

{ others.length ? (

{ __( 'Choose the language in the top bar to translate the texts. Where a translation is missing, visitors see the text of the default language.', 'funnelforms-pro' ) }

) : (

{ __( 'Add a language to show the form in it: on pages in that language (WPML, Polylang) or by the visitors\' choice.', 'funnelforms-pro' ) }

) } { readOnly || 'free' === process.env.FUNNELFORMS_EDITION ? null : ( label={ __( 'Add a language', 'funnelforms-pro' ) } value="" options={ [ { value: '', label: __( 'Choose…', 'funnelforms-pro' ) }, ...addable.map( ( l ) => ( { value: l, label: name( l ) } ) ).sort( ( a, b ) => a.label.localeCompare( b.label ) ) ] } onChange={ ( locale ) => { if ( locale ) { change( ( d ) => addLanguage( d, locale ) ); store.getState().setLang( locale ); } } } /> ) } { /* Forms in one language: buttons and messages follow the page or a language of their own (3.x fe_locale). */ } { 0 === others.length || '' !== doc.settings.uiLocale ? ( label={ __( 'Language of buttons and messages', 'funnelforms-pro' ) } value={ doc.settings.uiLocale } disabled={ readOnly } hint={ __( 'Such as Next, Back and the error messages.', 'funnelforms-pro' ) } options={ [ { value: '', label: __( 'Language of the page', 'funnelforms-pro' ) }, ...[ ...new Set( [ ...LANGUAGES, ...( '' !== doc.settings.uiLocale ? [ doc.settings.uiLocale ] : [] ) ] ) ].map( ( l ) => ( { value: l, label: name( l ) } ) ).sort( ( a, b ) => a.label.localeCompare( b.label ) ), ] } onChange={ ( locale ) => change( ( d ) => void ( d.settings.uiLocale = locale ), 'settings.uiLocale' ) } /> ) : null } { /* Pro: Funnelforms Free shows the choice only to switch it off. */ } { ( 'free' === process.env.FUNNELFORMS_EDITION ? true === doc.settings.languageSwitch : others.length > 0 ) ? ( change( ( d ) => { if ( on ) { d.settings.languageSwitch = true; } else { delete d.settings.languageSwitch; } } ) } /> ) : null }
); } /** The language the texts are edited in; only for forms in several languages. */ export function LanguagePicker() { const { store, lang } = useEnv(); const locales = useBuilder( ( s ) => s.doc?.locales ); if ( ! locales || locales.available.filter( ( l ) => l !== locales.default ).length === 0 ) { return null; } const list = [ locales.default, ...locales.available.filter( ( l ) => l !== locales.default ) ]; return ( ); }