Skip to content

Commit

Permalink
Improved pluralizer
Browse files Browse the repository at this point in the history
better tests
  • Loading branch information
sergix44 committed Dec 14, 2023
1 parent 8c997fb commit 92aab9e
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 105 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [ 18.x ]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Npm install
run: npm ci

- name: Execute tests
run: npm run tests
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Laravel localization bridge for your frontend.",
"main": "index.ts",
"scripts": {
"test": "vitest"
"tests": "vitest --run"
},
"repository": {
"type": "git",
Expand Down
10 changes: 7 additions & 3 deletions src/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface CandidateTranslation {
type: 'php' | 'json'
basePath: string
path: string
name: string
name: string | null
nesting: string[]
locale: string
}
Expand Down Expand Up @@ -53,7 +53,11 @@ export const exportTranslations = (...paths: string[]) => {
current = current[nest]
})

current[candidate.name] = content
if (candidate.name) {
current[candidate.name] = content
} else {
translations[candidate.locale][candidate.type] = {...current, ...content}
}
})

return translations
Expand All @@ -62,7 +66,7 @@ export const exportTranslations = (...paths: string[]) => {
const getTranslationCandidates = (pattern: string, path: string, type: 'php' | 'json'): CandidateTranslation[] => {
return glob.sync(pattern, {cwd: path}).map((transPath) => {
const withoutExtension = transPath.split('.').shift()
const name = basename(withoutExtension).toLocaleLowerCase()
const name = type === 'php' ? basename(withoutExtension).toLocaleLowerCase() : null
const locale = withoutExtension.split(sep).shift().toLocaleLowerCase()
const nesting = withoutExtension.split(sep).slice(1, -1)
return {
Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,39 @@ const isServer = typeof window === 'undefined'

const defaultConfig: Config = {
locale: !isServer && document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : 'en',
fallbackLocale: !isServer && window ? window?.fallbackLocale : null,
fallbackLocale: !isServer && window ? window?.fallbackLocale.replace('-', '_') : null,
translations: translations,
}

const trans = (key: string, replace: object = {}, locale: string = null, config: Config = null) => {
if (locale) {
(config ?? defaultConfig).locale = locale
if (!config) {
config = {...defaultConfig}
}
config.locale = locale
}

return translator(key, replace, false, config ?? defaultConfig)
}

const transChoice = (key: string, number: number, replace: Object = {}, locale: string = null, config: Config = null) => {
if (locale) {
(config ?? defaultConfig).locale = locale
if (!config) {
config = {...defaultConfig}
}
config.locale = locale
}

return translator(key, {...replace, count: number}, true, config ?? defaultConfig)
}

const setLocale = (locale: string, fallbackLocale: string | null = null) => {
defaultConfig.locale = locale.replace('-', '_')
defaultConfig.fallbackLocale = fallbackLocale.replace('-', '_')
}

const __ = trans;
const t = trans;
const trans_choice = transChoice;

export {trans, __, t, transChoice, trans_choice}
export {trans, __, t, transChoice, trans_choice, setLocale}
Loading

0 comments on commit 92aab9e

Please sign in to comment.