`.
* @param InnerComponent Component that will receive `locale` as a prop
* @returns Component enhanced with locale
* @example
*
* import { withLocale } from '@automattic/i18n-utils';
* function MyComponent( { locale } ) {
* return The current locale is: { locale }
;
* }
* export default withLocale( MyComponent );
*/
export const withLocale = createHigherOrderComponent(
< OuterProps, >( InnerComponent: React.ComponentType< OuterProps & { locale: string } > ) => {
return function OuterComponent( props: OuterProps ) {
const locale = useLocale();
const innerProps = { ...props, locale };
return ;
};
},
'withLocale'
);
/**
* React hook providing whether the current locale slug belongs to English or not
* @example
*
* import { useIsEnglishLocale } from '@automattic/i18n-utils';
* function MyComponent() {
* const isEnglishLocale = useIsEnglishLocale();
* return The current locale is English: { isEnglishLocale }
;
* }
*/
export function useIsEnglishLocale(): boolean {
const locale = useLocale();
return englishLocales.includes( locale );
}
type HasTranslation = ( single: string, context?: string, domain?: string ) => boolean;
export function useHasEnTranslation(): HasTranslation {
const isEnglishLocale = useIsEnglishLocale();
return useCallback(
( ...args ) => isEnglishLocale || i18n.hasTranslation( ...args ),
[ isEnglishLocale ]
);
}