-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: language name and capitalized correction
- Loading branch information
Showing
9 changed files
with
115 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import translate from '../src/index.js'; | ||
|
||
// Language name and capitalized correction | ||
translate('例子', { to: 'ENGlish' }).then(res => { | ||
// => example | ||
console.log(res.text); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { iso6391X } from '../core/index.js'; | ||
import validate from '../core/validate.js'; | ||
import getLocale from './get-locale.js'; | ||
|
||
function getCode(codeOrLang?: string, adaptive = false) { | ||
if (!codeOrLang || codeOrLang === 'auto') return adaptive ? getLocale() : 'auto'; | ||
|
||
codeOrLang = codeOrLang.toLowerCase(); | ||
|
||
// Check if a language is in supported; if not, throw an error object. | ||
const isValidated = validate(codeOrLang); | ||
if (isValidated) return codeOrLang; | ||
|
||
// Using Language code from supported name | ||
const code = iso6391X.getCode(codeOrLang); | ||
if (code) return code; | ||
|
||
throw new Error('EVALIDATION'); | ||
} | ||
|
||
export default getCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as getCode } from './get-code.js'; | ||
export { default as getLocale } from './get-locale.js'; | ||
export { default as mutable, type Mutable } from './mutable.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect, test } from 'vitest'; | ||
import getCode from '../../src/helper/get-code'; | ||
import getLocale from '../../src/helper/get-locale'; | ||
|
||
test('getCode - should return "auto" when no code or language is provided and adaptive is false', () => { | ||
const result = getCode(undefined, false); | ||
expect(result).toBe('auto'); | ||
}); | ||
|
||
test('getCode - should return the locale when no code or language is provided and adaptive is true', () => { | ||
const result = getCode(undefined, true); | ||
const expected = getLocale(); | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('getCode - should return the provided code when it is a valid language code', () => { | ||
const result = getCode('en'); | ||
expect(result).toBe('en'); | ||
}); | ||
|
||
test('getCode - should return the provided code when it is a valid language code (uppercase)', () => { | ||
const result = getCode('EN'); | ||
expect(result).toBe('en'); | ||
}); | ||
|
||
test('getCode - should return the provided code when it is a valid language code (mixed case)', () => { | ||
const result = getCode('eN'); | ||
expect(result).toBe('en'); | ||
}); | ||
|
||
test('getCode - should return the provided language code when it is a valid language name', () => { | ||
const result = getCode('English'); | ||
expect(result).toBe('en'); | ||
}); | ||
|
||
test('getCode - should throw an error when the provided code is not valid', () => { | ||
expect(() => getCode('invalid')).toThrowError('EVALIDATION'); | ||
}); |