|
|
import interpolateComponents from '@automattic/interpolate-components'; |
|
|
import sprintf from '@tannin/sprintf'; |
|
|
import debugFactory from 'debug'; |
|
|
import sha1 from 'hash.js/lib/hash/sha/1'; |
|
|
import LRU from 'lru'; |
|
|
import Tannin from 'tannin'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const debug = debugFactory( 'i18n-calypso' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const domain_key = 'messages'; |
|
|
|
|
|
const translationLookup = [ |
|
|
|
|
|
function ( options ) { |
|
|
return options; |
|
|
}, |
|
|
]; |
|
|
|
|
|
const hashCache = {}; |
|
|
|
|
|
|
|
|
function warn() { |
|
|
if ( ! I18N.throwErrors ) { |
|
|
return; |
|
|
} |
|
|
if ( 'undefined' !== typeof window && window.console && window.console.warn ) { |
|
|
window.console.warn.apply( window.console, arguments ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function simpleArguments( args ) { |
|
|
return Array.prototype.slice.call( args ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeTranslateArguments( args ) { |
|
|
const original = args[ 0 ]; |
|
|
|
|
|
|
|
|
if ( |
|
|
typeof original !== 'string' || |
|
|
args.length > 3 || |
|
|
( args.length > 2 && typeof args[ 1 ] === 'object' && typeof args[ 2 ] === 'object' ) |
|
|
) { |
|
|
warn( |
|
|
'Deprecated Invocation: `translate()` accepts ( string, [string], [object] ). These arguments passed:', |
|
|
simpleArguments( args ), |
|
|
'. See https://github.com/Automattic/i18n-calypso#translate-method' |
|
|
); |
|
|
} |
|
|
if ( args.length === 2 && typeof original === 'string' && typeof args[ 1 ] === 'string' ) { |
|
|
warn( |
|
|
'Invalid Invocation: `translate()` requires an options object for plural translations, but passed:', |
|
|
simpleArguments( args ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let options = {}; |
|
|
for ( let i = 0; i < args.length; i++ ) { |
|
|
if ( typeof args[ i ] === 'object' ) { |
|
|
options = args[ i ]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( typeof original === 'string' ) { |
|
|
options.original = original; |
|
|
} else if ( typeof options.original === 'object' ) { |
|
|
options.plural = options.original.plural; |
|
|
options.count = options.original.count; |
|
|
options.original = options.original.single; |
|
|
} |
|
|
if ( typeof args[ 1 ] === 'string' ) { |
|
|
options.plural = args[ 1 ]; |
|
|
} |
|
|
|
|
|
if ( typeof options.original === 'undefined' ) { |
|
|
throw new Error( 'Translate called without a `string` value as first argument.' ); |
|
|
} |
|
|
|
|
|
return options; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getTranslationFromTannin( tannin, options ) { |
|
|
return tannin.dcnpgettext( |
|
|
domain_key, |
|
|
options.context, |
|
|
options.original, |
|
|
options.plural, |
|
|
options.count |
|
|
); |
|
|
} |
|
|
|
|
|
function getTranslation( i18n, options ) { |
|
|
for ( let i = translationLookup.length - 1; i >= 0; i-- ) { |
|
|
const lookup = translationLookup[ i ]( Object.assign( {}, options ) ); |
|
|
const key = lookup.context ? lookup.context + '\u0004' + lookup.original : lookup.original; |
|
|
|
|
|
|
|
|
if ( i18n.state.locale[ key ] ) { |
|
|
return getTranslationFromTannin( i18n.state.tannin, lookup ); |
|
|
} |
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
function I18N() { |
|
|
if ( ! ( this instanceof I18N ) ) { |
|
|
return new I18N(); |
|
|
} |
|
|
this.defaultLocaleSlug = 'en'; |
|
|
|
|
|
this.defaultPluralForms = ( n ) => ( n === 1 ? 0 : 1 ); |
|
|
this.state = { |
|
|
tannin: undefined, |
|
|
locale: undefined, |
|
|
localeSlug: undefined, |
|
|
localeVariant: undefined, |
|
|
textDirection: undefined, |
|
|
translations: LRU( { max: 100 } ), |
|
|
}; |
|
|
this.componentUpdateHooks = []; |
|
|
this.translateHooks = []; |
|
|
this.subscribers = new Set(); |
|
|
|
|
|
this.configure(); |
|
|
} |
|
|
|
|
|
I18N.throwErrors = false; |
|
|
|
|
|
I18N.prototype.subscribe = function ( callback ) { |
|
|
this.subscribers.add( callback ); |
|
|
return () => this.subscribers.delete( callback ); |
|
|
}; |
|
|
|
|
|
I18N.prototype.emitChange = function () { |
|
|
this.subscribers.forEach( ( callback ) => callback() ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.getBrowserSafeLocale = function () { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.getLocaleVariant()?.split( '_' )[ 0 ] ?? this.getLocaleSlug(); |
|
|
}; |
|
|
|
|
|
I18N.prototype.configure = function ( options ) { |
|
|
Object.assign( this, options || {} ); |
|
|
this.setLocale(); |
|
|
}; |
|
|
|
|
|
I18N.prototype.setLocale = function ( localeData ) { |
|
|
if ( localeData && localeData[ '' ] && localeData[ '' ][ 'key-hash' ] ) { |
|
|
const keyHash = localeData[ '' ][ 'key-hash' ]; |
|
|
|
|
|
const transform = function ( string, hashLength ) { |
|
|
const lookupPrefix = hashLength === false ? '' : String( hashLength ); |
|
|
if ( typeof hashCache[ lookupPrefix + string ] !== 'undefined' ) { |
|
|
return hashCache[ lookupPrefix + string ]; |
|
|
} |
|
|
const hash = sha1().update( string ).digest( 'hex' ); |
|
|
|
|
|
if ( hashLength ) { |
|
|
return ( hashCache[ lookupPrefix + string ] = hash.substr( 0, hashLength ) ); |
|
|
} |
|
|
|
|
|
return ( hashCache[ lookupPrefix + string ] = hash ); |
|
|
}; |
|
|
|
|
|
const generateLookup = function ( hashLength ) { |
|
|
return function ( options ) { |
|
|
if ( options.context ) { |
|
|
options.original = transform( |
|
|
options.context + String.fromCharCode( 4 ) + options.original, |
|
|
hashLength |
|
|
); |
|
|
delete options.context; |
|
|
} else { |
|
|
options.original = transform( options.original, hashLength ); |
|
|
} |
|
|
|
|
|
return options; |
|
|
}; |
|
|
}; |
|
|
|
|
|
if ( keyHash.substr( 0, 4 ) === 'sha1' ) { |
|
|
if ( keyHash.length === 4 ) { |
|
|
translationLookup.push( generateLookup( false ) ); |
|
|
} else { |
|
|
const variableHashLengthPos = keyHash.substr( 5 ).indexOf( '-' ); |
|
|
if ( variableHashLengthPos < 0 ) { |
|
|
const hashLength = Number( keyHash.substr( 5 ) ); |
|
|
translationLookup.push( generateLookup( hashLength ) ); |
|
|
} else { |
|
|
const minHashLength = Number( keyHash.substr( 5, variableHashLengthPos ) ); |
|
|
const maxHashLength = Number( keyHash.substr( 6 + variableHashLengthPos ) ); |
|
|
|
|
|
for ( let hashLength = minHashLength; hashLength <= maxHashLength; hashLength++ ) { |
|
|
translationLookup.push( generateLookup( hashLength ) ); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! localeData || ! localeData[ '' ].localeSlug ) { |
|
|
this.state.locale = { |
|
|
'': { localeSlug: this.defaultLocaleSlug, plural_forms: this.defaultPluralForms }, |
|
|
}; |
|
|
} else if ( localeData[ '' ].localeSlug === this.state.localeSlug ) { |
|
|
|
|
|
if ( localeData === this.state.locale ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
Object.assign( this.state.locale, localeData ); |
|
|
} else { |
|
|
this.state.locale = Object.assign( {}, localeData ); |
|
|
} |
|
|
|
|
|
this.state.localeSlug = this.state.locale[ '' ].localeSlug; |
|
|
this.state.localeVariant = this.state.locale[ '' ].localeVariant; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.state.textDirection = |
|
|
this.state.locale[ 'text direction\u0004ltr' ]?.[ 0 ] || |
|
|
this.state.locale[ '' ]?.momentjs_locale?.textDirection; |
|
|
|
|
|
this.state.tannin = new Tannin( { [ domain_key ]: this.state.locale } ); |
|
|
|
|
|
this.emitChange(); |
|
|
}; |
|
|
|
|
|
I18N.prototype.getLocale = function () { |
|
|
return this.state.locale; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.getLocaleSlug = function () { |
|
|
return this.state.localeSlug; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.getLocaleVariant = function () { |
|
|
return this.state.localeVariant; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.isRtl = function () { |
|
|
return this.state.textDirection === 'rtl'; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.addTranslations = function ( localeData ) { |
|
|
for ( const prop in localeData ) { |
|
|
if ( prop !== '' ) { |
|
|
this.state.tannin.data.messages[ prop ] = localeData[ prop ]; |
|
|
} |
|
|
} |
|
|
|
|
|
this.emitChange(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.hasTranslation = function () { |
|
|
return !! getTranslation( this, normalizeTranslateArguments( arguments ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.fixMe = function ( { text, newCopy, oldCopy, translationOptions = {} } ) { |
|
|
if ( typeof text !== 'string' ) { |
|
|
warn( 'fixMe() requires an object with a proper text property (string)' ); |
|
|
return null; |
|
|
} |
|
|
|
|
|
if ( |
|
|
[ 'en', 'en-gb' ].includes( this.getLocaleSlug() ) || |
|
|
this.hasTranslation( text, translationOptions ) |
|
|
) { |
|
|
return newCopy; |
|
|
} |
|
|
|
|
|
return oldCopy; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.translate = function () { |
|
|
const options = normalizeTranslateArguments( arguments ); |
|
|
|
|
|
let translation = getTranslation( this, options ); |
|
|
if ( ! translation ) { |
|
|
|
|
|
|
|
|
translation = getTranslationFromTannin( this.state.tannin, options ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( options.args ) { |
|
|
const sprintfArgs = Array.isArray( options.args ) ? options.args.slice( 0 ) : [ options.args ]; |
|
|
sprintfArgs.unshift( translation ); |
|
|
try { |
|
|
translation = sprintf( ...sprintfArgs ); |
|
|
} catch ( error ) { |
|
|
if ( ! window || ! window.console ) { |
|
|
return; |
|
|
} |
|
|
const errorMethod = this.throwErrors ? 'error' : 'warn'; |
|
|
if ( typeof error !== 'string' ) { |
|
|
window.console[ errorMethod ]( error ); |
|
|
} else { |
|
|
window.console[ errorMethod ]( 'i18n sprintf error:', sprintfArgs ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( options.components ) { |
|
|
translation = interpolateComponents( { |
|
|
mixedString: translation, |
|
|
components: options.components, |
|
|
throwErrors: this.throwErrors, |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
this.translateHooks.forEach( function ( hook ) { |
|
|
translation = hook( translation, options ); |
|
|
} ); |
|
|
|
|
|
return translation; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I18N.prototype.reRenderTranslations = function () { |
|
|
debug( 'Re-rendering all translations due to external request' ); |
|
|
this.emitChange(); |
|
|
}; |
|
|
|
|
|
I18N.prototype.registerComponentUpdateHook = function ( callback ) { |
|
|
this.componentUpdateHooks.push( callback ); |
|
|
}; |
|
|
|
|
|
I18N.prototype.registerTranslateHook = function ( callback ) { |
|
|
this.translateHooks.push( callback ); |
|
|
}; |
|
|
|
|
|
export default I18N; |
|
|
|