|
|
import languages from '@automattic/languages'; |
|
|
import debugModule from 'debug'; |
|
|
import i18n, { localize } from 'i18n-calypso'; |
|
|
import { find, isEmpty } from 'lodash'; |
|
|
import { Component } from 'react'; |
|
|
import { connect } from 'react-redux'; |
|
|
import QueryUserSettings from 'calypso/components/data/query-user-settings'; |
|
|
import isCommunityTranslatorEnabled from 'calypso/state/selectors/is-community-translator-enabled'; |
|
|
import Translatable from './translatable'; |
|
|
|
|
|
import './style.scss'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const debug = debugModule( 'calypso:community-translator' ); |
|
|
|
|
|
class CommunityTranslator extends Component { |
|
|
languageJson = null; |
|
|
currentLocale = null; |
|
|
initialized = false; |
|
|
|
|
|
componentDidMount() { |
|
|
this.setLanguage(); |
|
|
|
|
|
|
|
|
i18n.registerTranslateHook( ( translation, options ) => |
|
|
this.wrapTranslation( options.original, translation, options ) |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
i18n.registerComponentUpdateHook( () => {} ); |
|
|
this.i18nUnsubscribe = i18n.subscribe( this.refresh ); |
|
|
} |
|
|
|
|
|
componentWillUnmount() { |
|
|
this.i18nUnsubscribe(); |
|
|
} |
|
|
|
|
|
setLanguage() { |
|
|
this.languageJson = i18n.getLocale() || { '': {} }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { localeSlug, localeVariant } = this.languageJson[ '' ]; |
|
|
this.localeCode = localeVariant || localeSlug; |
|
|
this.currentLocale = find( languages, ( lang ) => lang.langSlug === this.localeCode ); |
|
|
} |
|
|
|
|
|
refresh = () => { |
|
|
if ( this.initialized ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( ! this.props.isCommunityTranslatorEnabled ) { |
|
|
debug( 'not initializing, not enabled' ); |
|
|
return; |
|
|
} |
|
|
|
|
|
this.setLanguage(); |
|
|
|
|
|
if ( ! this.localeCode || ! this.languageJson ) { |
|
|
debug( 'trying to initialize translator without loaded language' ); |
|
|
return; |
|
|
} |
|
|
|
|
|
debug( 'Successfully initialized' ); |
|
|
this.initialized = true; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wrapTranslation( originalFromPage, displayedTranslationFromPage, optionsFromPage ) { |
|
|
if ( ! this.props.isCommunityTranslatorEnabled ) { |
|
|
return displayedTranslationFromPage; |
|
|
} |
|
|
|
|
|
if ( 'object' !== typeof optionsFromPage ) { |
|
|
optionsFromPage = {}; |
|
|
} |
|
|
|
|
|
if ( 'string' !== typeof originalFromPage ) { |
|
|
debug( 'unknown original format' ); |
|
|
return displayedTranslationFromPage; |
|
|
} |
|
|
|
|
|
if ( optionsFromPage.textOnly ) { |
|
|
debug( `respecting textOnly for string "${ originalFromPage }"` ); |
|
|
return displayedTranslationFromPage; |
|
|
} |
|
|
|
|
|
const props = { |
|
|
singular: originalFromPage, |
|
|
locale: this.currentLocale, |
|
|
}; |
|
|
|
|
|
let key = originalFromPage; |
|
|
|
|
|
|
|
|
if ( 'string' === typeof optionsFromPage.context ) { |
|
|
props.context = optionsFromPage.context; |
|
|
|
|
|
|
|
|
key = `${ optionsFromPage.context }\u0004${ originalFromPage }`; |
|
|
} |
|
|
|
|
|
|
|
|
if ( 'string' === typeof optionsFromPage.plural ) { |
|
|
props.plural = optionsFromPage.plural; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( isEmpty( this.languageJson[ key ] ) ) { |
|
|
props.untranslated = 'true'; |
|
|
} |
|
|
|
|
|
|
|
|
const translatableElement = Object.assign( |
|
|
{}, |
|
|
<Translatable { ...props }>{ displayedTranslationFromPage }</Translatable> |
|
|
); |
|
|
|
|
|
|
|
|
translatableElement.toString = () => { |
|
|
|
|
|
return displayedTranslationFromPage; |
|
|
}; |
|
|
|
|
|
|
|
|
Object.freeze( translatableElement ); |
|
|
|
|
|
return translatableElement; |
|
|
} |
|
|
|
|
|
render() { |
|
|
return <QueryUserSettings />; |
|
|
} |
|
|
} |
|
|
|
|
|
export default connect( ( state ) => ( { |
|
|
isCommunityTranslatorEnabled: isCommunityTranslatorEnabled( state ), |
|
|
} ) )( localize( CommunityTranslator ) ); |
|
|
|