Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/en-US/core/functions/locales/resolve-canonical-locale.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: resolveCanonicalLocale
description: API reference for the resolveCanonicalLocale function
---

## Overview

The `resolveCanonicalLocale` function converts alias locale codes into their canonical locale codes when custom mapping is configured. This standalone function provides the same functionality as the GT class method without requiring an instance.

It is the inverse operation of [`resolveAliasLocale`](/docs/core/functions/locales/resolve-alias-locale).

---

## Reference

### Parameters

<TypeTable
type={{
"locale?": {
type: 'string',
optional: true,
},
"customMapping?": {
type: 'CustomMapping',
optional: true,
}
}}
/>

### Returns

`string` - The canonical locale code if mapping exists, otherwise the original locale

---

## Examples

```typescript
import { resolveCanonicalLocale } from 'generaltranslation';

const customMapping = {
'simplified-chinese': { code: 'zh-CN', name: 'Simplified Chinese' },
'traditional-chinese': { code: 'zh-TW', name: 'Traditional Chinese' }
};

// Resolve alias to canonical
console.log(resolveCanonicalLocale('simplified-chinese', customMapping)); // 'zh-CN'
console.log(resolveCanonicalLocale('traditional-chinese', customMapping)); // 'zh-TW'

// Non-mapped locale returns original
console.log(resolveCanonicalLocale('es-ES', customMapping)); // 'es-ES'
```
---

## Notes

- Converts user-friendly aliases back to canonical locale codes
- Returns the original locale if no mapping exists
- Essential for normalizing locales before passing them to translation services
- Works with custom locale mappings
- Stateless function - no side effects

## Next steps

- Use GT class method [`resolveCanonicalLocale`](/docs/core/class/methods/locales/resolve-canonical-locale)
- Convert back to an alias with [`resolveAliasLocale`](/docs/core/functions/locales/resolve-alias-locale)
Loading