isSameDialect
API reference for the GT isSameDialect method
Overview
The isSameDialect method checks whether multiple BCP‑47 locale codes represent the same dialect.
It compares the language and region components of locale codes to determine whether they represent the same linguistic variety.
Reference
Parameters
Prop
Type
Parameter Description
| Parameter | Description |
|---|---|
...locales | A variable number of locale codes (strings) or arrays of locale codes to compare. All provided locales must represent the same dialect. |
Returns
boolean — true if all provided locale codes represent the same dialect; false otherwise
Throws
No exceptions are thrown. Invalid locale codes are handled gracefully and return false.
Behaviour
Dialect Comparison Logic
The method uses the following logic to determine dialect equality:
- Normalise locales - Standardises all input locale codes
- Compare language codes - All locales must share the same base language
- Handle region hierarchy - A base language matches its regional variants
- Exact dialect matching - Regional variants must match exactly
Hierarchy rules
- Base language (
'en') matches any regional variant ('en-US','en-GB') - Regional variants only match exactly (
'en-US'≠'en-GB') - Script variants are taken into account in the comparison when present
- Different languages never match (
'en'≠'es')
Input flexibility
The method accepts:
- Multiple string parameters:
isSameDialect('en-US', 'en-GB') - Arrays of strings:
isSameDialect(['en-US', 'en-GB']) - Mixed format:
isSameDialect('en-US', ['en-GB', 'en-CA']) - Single locale:
isSameDialect('en-US')(always returnstrue)
Examples
Basic Dialect Comparison
const gt = new GT({
sourceLocale: 'en-US',
targetLocale: 'es-ES'
});
// Exactly the same dialects
console.log(gt.isSameDialect('en-US', 'en-US')); // true
console.log(gt.isSameDialect('zh-CN', 'zh-CN')); // true
// Different dialects of the same language
console.log(gt.isSameDialect('en-US', 'en-GB')); // false
console.log(gt.isSameDialect('es-ES', 'es-MX')); // false
console.log(gt.isSameDialect('pt-PT', 'pt-BR')); // false
// Base language with regional variants
console.log(gt.isSameDialect('en', 'en-US')); // true
console.log(gt.isSameDialect('es', 'es-ES')); // true
console.log(gt.isSameDialect('zh', 'zh-CN')); // true
// Different languages
console.log(gt.isSameDialect('en-US', 'es-ES')); // false
console.log(gt.isSameDialect('fr-FR', 'de-DE')); // falseNotes
- Uses standardised locale codes for accurate comparison
- Accepts flexible input formats (strings, arrays, mixed)
Next steps
- Compare languages with
isSameLanguage - Check the locale hierarchy with
isSupersetLocale - Validate locales with
isValidLocale
How is this guide?