Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed Jan 29, 2022
1 parent b043dc2 commit 9e62e18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ import { getActiveLanguage } from 'laravel-vue-i18n';
const lang = getActiveLanguage(); // en
```

### `isLoaded()`
### `isLoaded(lang?: string)`

The `isLoaded()` check if the language files are loaded or not.
The `isLoaded()` method checks if the language is loaded.
If the `lang` parameter is not passed it will check for the actual language set.

```jsx
import { isLoaded } from 'laravel-vue-i18n';

const loaded = isLoaded(); // true

const loaded = isLoaded('fr'); // false
```

22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const isServer = typeof window === 'undefined'
* The default options, for the plugin.
*/
const DEFAULT_OPTIONS: OptionsInterface = {
lang: ! isServer && document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : 'en',
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} })),
lang: !isServer && document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : 'en',
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} }))
}

/**
Expand All @@ -31,19 +31,19 @@ let loaded: LanguageInterface[] = []
const activeMessages: object = reactive({})

/**
* Check if the language files are loaded
* Checks if the language is loaded.
*/
export function isLoaded(lang?: string) {
lang ??= getActiveLanguage();
export function isLoaded(lang?: string): boolean {
lang ??= getActiveLanguage()

return loaded.some((row) => row.lang === lang);
return loaded.some((row) => row.lang === lang)
}

/**
* Loads the language file.
*/
export function loadLanguageAsync(lang: string): Promise<string | void> {
lang = lang.replace('-', '_');
lang = lang.replace('-', '_')

const loadedLang: LanguageInterface = loaded.find((row) => row.lang === lang)

Expand Down Expand Up @@ -89,19 +89,19 @@ export function transChoice(key: string, number: number, replacements: Replaceme
* Returns the current active language.
*/
export function getActiveLanguage(): string {
return options.lang;
return options.lang
}

/**
* Sets the language messages to the activeMessages.
*/
function setLanguage({ lang, messages }: LanguageInterface): string {
if (! isServer) {
if (!isServer) {
// When setting the HTML lang attribute, hyphen must be use instead of underscore.
document.documentElement.setAttribute("lang", lang.replace('_', '-'));
document.documentElement.setAttribute('lang', lang.replace('_', '-'))
}

options.lang = lang;
options.lang = lang

for (const [key, value] of Object.entries(messages)) {
activeMessages[key] = value
Expand Down

0 comments on commit 9e62e18

Please sign in to comment.