import i18next , { StringMap , TOptions } from 'i18next' ; import LanguageDetector from 'i18next-browser-languagedetector' ; import { Stage , Status } from 'brackets-model' ; import { isMajorRound } from './helpers' ; import { FinalType , BracketType , OriginHint } from './types' ; import en from './i18n/en/translation.json' ; import fr from './i18n/fr/translation.json' ; export const locales = { en , fr , } ; export type Locale = typeof locales [ 'en' ] ; i18next . use ( LanguageDetector ) . init ( { fallbackLng : 'en' , debug : false , resources : { en : { translation : locales . en , } , fr : { translation : locales . fr , } , } , } ) ; param name param locale export function addLocale ( name , locale ) { i18next . addResourceBundle ( name , 'translation' , locale , true , true ) ; i18next . changeLanguage ( ) ; } param key param options export function t < Scope extends keyof Locale , SubKey extends string & keyof Locale [ Scope ] , T extends TOptions > ( key , options ? ) { return i18next . t ( key , options ) ; } param roundNumber param roundCount param skipFirstRound param matchLocation export function getOriginHint ( roundNumber , roundCount , skipFirstRound , matchLocation ) { if ( roundNumber === 1 ) { if ( matchLocation === 'single-bracket' ) return ( position ) : string => t ( 'origin-hint.seed' , { position } ) ; if ( matchLocation === 'winner-bracket' ) return ( position ) : string => t ( 'origin-hint.seed' , { position } ) ; if ( matchLocation === 'loser-bracket' && skipFirstRound ) return ( position ) : string => t ( 'origin-hint.seed' , { position } ) ; } if ( isMajorRound ( roundNumber ) && matchLocation === 'loser-bracket' ) { if ( roundNumber === roundCount - 2 ) return ( position ) : string => t ( 'origin-hint.winner-bracket-semi-final' , { position } ) ; if ( roundNumber === roundCount ) return ( ) : string => t ( 'origin-hint.winner-bracket-final' ) ; const roundNumberWB = Math . ceil ( ( roundNumber + 1 ) / 2 ) ; if ( skipFirstRound ) return ( position ) : string => t ( 'origin-hint.winner-bracket' , { round : roundNumberWB - 1 , position } ) ; return ( position ) : string => t ( 'origin-hint.winner-bracket' , { round : roundNumberWB , position } ) ; } return undefined ; } param finalType param roundNumber export function getFinalOriginHint ( finalType , roundNumber ) { if ( finalType === 'consolation_final' ) return ( position ) : string => t ( 'origin-hint.consolation-final' , { position } ) ; if ( roundNumber === 1 ) return ( ) : string => t ( 'origin-hint.grand-final' ) ; return undefined ; } param matchNumber param roundNumber param roundCount param matchLocation export function getMatchLabel ( matchNumber , roundNumber , roundCount , matchLocation ) { const matchPrefix = matchLocation === 'winner-bracket' ? t ( 'match-label.winner-bracket' ) : matchLocation === 'loser-bracket' ? t ( 'match-label.loser-bracket' ) : t ( 'match-label.standard-bracket' ) ; const inSemiFinalRound = roundNumber === roundCount - 1 ; const inFinalRound = roundNumber === roundCount ; if ( matchLocation === 'single-bracket' ) { if ( inSemiFinalRound ) return t ( 'match-label.standard-bracket-semi-final' , { matchNumber } ) ; if ( inFinalRound ) return t ( 'match-label.standard-bracket-final' ) ; } if ( inSemiFinalRound ) return t ( 'match-label.double-elimination-semi-final' , { matchPrefix , matchNumber } ) ; if ( inFinalRound ) return t ( 'match-label.double-elimination-final' , { matchPrefix } ) ; return t ( 'match-label.double-elimination' , { matchPrefix , roundNumber , matchNumber } ) ; } param finalType param roundNumber param roundCount export function getFinalMatchLabel ( finalType , roundNumber , roundCount ) { if ( finalType === 'consolation_final' ) return t ( 'match-label.consolation-final' ) ; if ( roundCount === 1 ) return t ( 'match-label.grand-final-single' ) ; return t ( 'match-label.grand-final' , { roundNumber } ) ; } param status export function getMatchStatus ( status ) { switch ( status ) { case Status . Locked : return t ( 'match-status.locked' ) ; case Status . Waiting : return t ( 'match-status.waiting' ) ; case Status . Ready : return t ( 'match-status.ready' ) ; case Status . Running : return t ( 'match-status.running' ) ; case Status . Completed : return t ( 'match-status.completed' ) ; case Status . Archived : return t ( 'match-status.archived' ) ; default : return 'Unknown status' ; } } param groupNumber export function getGroupName ( groupNumber ) { return t ( 'common.group-name' , { groupNumber } ) ; } param stage param type export function getBracketName ( stage , type ) { switch ( type ) { case 'winner-bracket' : case 'loser-bracket' : return t ( `common.group-name-${ type }` , { stage } ) ; default : return undefined ; } } param roundNumber param roundCount export function getRoundName ( roundNumber , roundCount ) { return roundNumber === roundCount ? t ( 'common.round-name-final' ) : t ( 'common.round-name' , { roundNumber } ) ; } param roundNumber param roundCount export function getWinnerBracketRoundName ( roundNumber , roundCount ) { return roundNumber === roundCount ? t ( 'common.round-name-winner-bracket-final' ) : t ( 'common.round-name-winner-bracket' , { roundNumber } ) ; } param roundNumber param roundCount export function getLoserBracketRoundName ( roundNumber , roundCount ) { return roundNumber === roundCount ? t ( 'common.round-name-loser-bracket-final' ) : t ( 'common.round-name-loser-bracket' , { roundNumber } ) ; }