Skip to content

Commit

Permalink
update dependencies (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika authored Dec 14, 2024
1 parent f6aaf3f commit 551228d
Show file tree
Hide file tree
Showing 12 changed files with 16,369 additions and 15,431 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
cache: 'npm'
- run: npm i
- run: npm test
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
cache: 'npm'
registry-url: https://registry.npmjs.org/
- run: npm i
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
cache: 'npm'
registry-url: https://npm.pkg.github.com/
#scope: '@DenysVuika'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
cache: 'npm'
- name: npm install, build, and test
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build
.rpt2_cache
yarn-error.log
coverage
.idea
size-plugin.json
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"tabWidth": 2
"tabWidth": 2,
"trailingComma": "none"
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
TranslateContext,
TranslateProvider,
TranslateProvider
} from '@denysvuika/preact-translate';
import { useContext } from 'preact/hooks';
import './style';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/__snapshots__/translateProvider.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TranslateProvider matches snapshot 1`] = `
Object {
{
"asFragment": [Function],
"baseElement": <body>
<div>
Expand Down
74 changes: 39 additions & 35 deletions lib/src/useTranslate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'preact/hooks';
import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
import { LanguageData } from './languageData';
import { TranslateOptions } from './translateOptions';
import { TranslateParams } from './translateParams';
Expand All @@ -9,7 +9,7 @@ let cache: LanguageData = {};
const defaultOptions: TranslateOptions = {
root: '',
lang: 'en',
fallbackLang: 'en'
fallbackLang: 'en',
};

export default function useTranslate(
Expand All @@ -23,48 +23,52 @@ export default function useTranslate(
const [data, setData] = useState(cache);
const [isReady, setReady] = useState(false);

const loadData = (langKey: string) => {
// eslint-disable-next-line no-prototype-builtins
if (data.hasOwnProperty(langKey)) {
return;
}
const loadData = useCallback(
async (langKey: string) => {
if (data[langKey]) {
return;
}

setReady(false);
setReady(false);

const url = getResourceUrl(options.root, langKey);

fetch(url)
.then(results => results.json())
.then(resource => {
cache[langKey] = resource;
setData({ ...cache });
setReady(true);
})
.catch(error => {
console.log('Aww, snap.', error);
try {
const url = getResourceUrl(options.root, langKey);
const response = await fetch(url);
cache[langKey] = await response.json();
} catch (error) {
console.error(`Failed to load language data for ${langKey}:`, error);
} finally {
setData({ ...cache });
setReady(true);
});
};
}
},
[data, options.root]
);

useEffect(() => {
loadData(options.fallbackLang);
loadData(lang);
}, [lang]);
const loadLanguages = async () => {
await loadData(options.fallbackLang);
await loadData(lang);
};
void loadLanguages();
}, [lang, loadData, options.fallbackLang]);

const t = (key: string, params?: TranslateParams) => {
// eslint-disable-next-line no-prototype-builtins
if (!data.hasOwnProperty(lang)) {
return key;
}
const t = useMemo(
() => (key: string, params?: TranslateParams) => {
// eslint-disable-next-line no-prototype-builtins
if (!data.hasOwnProperty(lang)) {
return key;
}

let value = getValue(data, lang, key);
if (value === key && lang !== options.fallbackLang) {
value = getValue(data, options.fallbackLang, key);
}
let value = getValue(data, lang, key);
if (value === key && lang !== options.fallbackLang) {
value = getValue(data, options.fallbackLang, key);
}

return format(value, params);
};
return format(value, params);
},
[data, lang, options.fallbackLang]
);

return { lang, setLang, t, isReady };
}
16 changes: 8 additions & 8 deletions lib/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Utils', () => {
const template = 'Good luck, {param1} {param2}!';
const params = {
param1: 'Mr.',
param2: 'Gorsky',
param2: 'Gorsky'
};

const value = format(template, params);
Expand Down Expand Up @@ -61,9 +61,9 @@ describe('Utils', () => {
const data = {
en: {
messages: {
404: 'Error, not found.',
},
},
404: 'Error, not found.'
}
}
};

const value = getValue(data, 'en', 'messages.404');
Expand All @@ -73,8 +73,8 @@ describe('Utils', () => {
test('returns original key if nested value not found', () => {
const data = {
en: {
messages: {},
},
messages: {}
}
};

const value = getValue(data, 'en', 'messages.500');
Expand All @@ -84,8 +84,8 @@ describe('Utils', () => {
test('supports tol-level values with dots', () => {
const data = {
en: {
'messages.errors.500': 'Aw, snap!',
},
'messages.errors.500': 'Aw, snap!'
}
};

const value = getValue(data, 'en', 'messages.errors.500');
Expand Down
Loading

0 comments on commit 551228d

Please sign in to comment.