Skip to content

Commit

Permalink
style: run prettier on the entire codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
SrIzan10 committed Dec 1, 2024
1 parent 89807ba commit 948b829
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 206 deletions.
14 changes: 7 additions & 7 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"useTabs": false,
"printWidth": 800,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"semi": true
}
"useTabs": false,
"printWidth": 800,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"semi": true
}
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A zero-dependency (for the forseen future hopefully), typescript-ready, fast, li
# Getting started

First get your locale files going. A json file is enough:

```json
// en.json
{
Expand All @@ -26,32 +27,34 @@ First get your locale files going. A json file is enough:
```

Then, you can use the package like this:

```ts
import { Localization } from "shrimple-locales";
import enLocale from "./en.json";
import esLocale from "./es.json";
import { Localization } from 'shrimple-locales';
import enLocale from './en.json';
import esLocale from './es.json';

const loc = new Localization({
defaultLocale: "en",
fallbackLocale: "en",
locales: {
en: enLocale,
es: esLocale
}
})
defaultLocale: 'en',
fallbackLocale: 'en',
locales: {
en: enLocale,
es: esLocale,
},
});

// ... later in the code
foo(`${loc.get('salutes.hello')} ${loc.get('world')}`) // Hello World
foo(`${loc.get('salutes.hello')} ${loc.get('world')}`); // Hello World

loc.setLocale('es')
foo(`${loc.get('salutes.hello')} ${loc.get('world')}`) // Hola Mundo
loc.setLocale('es');
foo(`${loc.get('salutes.hello')} ${loc.get('world')}`); // Hola Mundo
```

# Roadmap

- [x] Basic functionality
- [x] Fallback locales
- [x] Default locales
- [x] Set locale
- [x] Get all locales (`localizationFor()`)
- [] Make the package read files or a directory
... and more to come!
... and more to come!
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test": "vitest --disable-console-intercept"
"test": "vitest --disable-console-intercept",
"format": "prettier --write ."
},
"exports": {
".": {
Expand All @@ -28,6 +29,7 @@
"package.json"
],
"devDependencies": {
"prettier": "3.4.1",
"shrimple-locales": "^0.2.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
Expand Down
58 changes: 29 additions & 29 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// The file is here because tsconfig is set to compile all files in src folder, and not the tests folder
import { Localization } from "./index";
import { Localization } from './index';

export const locales = new Localization({
defaultLocale: 'en',
fallbackLocale: 'en',
locales: {
en: {
hello: 'Hello',
inside: {
world: 'World',
'another/object': {
key: 'key inside'
}
},
interp: {
hello: 'Hello {{int}}'
}
defaultLocale: 'en',
fallbackLocale: 'en',
locales: {
en: {
hello: 'Hello',
inside: {
world: 'World',
'another/object': {
key: 'key inside',
},
es: {
hello: 'Hola',
inside: {
world: 'Mundo',
'another/object': {
key: 'key dentro'
}
},
interp: {
hello: 'Hola {{int}}'
}
}
}
})
},
interp: {
hello: 'Hello {{int}}',
},
},
es: {
hello: 'Hola',
inside: {
world: 'Mundo',
'another/object': {
key: 'key dentro',
},
},
interp: {
hello: 'Hola {{int}}',
},
},
},
});
22 changes: 11 additions & 11 deletions src/util/getLocales.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { interpolate } from "./interpolate";
import { Interpolations, LocaleObject, Locales } from "./types";
import { interpolate } from './interpolate';
import { Interpolations, LocaleObject, Locales } from './types';

export default function getLocales(args: Args): string | Record<string, string> {
const { key, locales, defaultLang, fallbackLang, interpolations } = args;
Expand All @@ -8,18 +8,18 @@ export default function getLocales(args: Args): string | Record<string, string>
for (const lang in locales) {
// Navigate through nested keys
let value = locales[lang];
for (const k of key.split(".")) {
for (const k of key.split('.')) {
value = value[k] as LocaleObject;
if (!value) break;
}

// Handle both string values and object values
if (value) {
if (typeof value === "object") {
if (typeof value === 'object') {
const innerKey = Object.keys(value)[0];
const interpolatedKey = interpolate(innerKey, interpolations);
translations[lang] = value[interpolatedKey] as string;
} else if (typeof value === "string") {
} else if (typeof value === 'string') {
translations[lang] = interpolate(value, interpolations);
}
}
Expand All @@ -35,9 +35,9 @@ export default function getLocales(args: Args): string | Record<string, string>
}

type Args = {
key: string,
locales: Locales,
defaultLang?: string,
fallbackLang?: string,
interpolations?: Interpolations
}
key: string;
locales: Locales;
defaultLang?: string;
fallbackLang?: string;
interpolations?: Interpolations;
};
8 changes: 4 additions & 4 deletions src/util/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export default function sanitize(text: string): string {
/[&<>]/g,
(char) =>
({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
})[char] || char,
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
})[char] || char
);
}
14 changes: 7 additions & 7 deletions tests/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { it, expect } from "vitest";
import { locales } from "../src/test";
import { it, expect } from 'vitest';
import { locales } from '../src/test';

it("should give multiple keys correctly for both langs", () => {
expect(locales.t("hello", {}, 'en')).toBe("Hello");
expect(locales.t("hello", {}, 'es')).toBe("Hola");
expect(locales.t("hello")).toBe("Hello");
expect(locales.t("inside.world", {}, 'en')).toBe("World");
it('should give multiple keys correctly for both langs', () => {
expect(locales.t('hello', {}, 'en')).toBe('Hello');
expect(locales.t('hello', {}, 'es')).toBe('Hola');
expect(locales.t('hello')).toBe('Hello');
expect(locales.t('inside.world', {}, 'en')).toBe('World');
});
14 changes: 7 additions & 7 deletions tests/getInterps.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { it, expect } from "vitest";
import { locales } from "../src/test";
import { it, expect } from 'vitest';
import { locales } from '../src/test';

it("should return values with interpolations replaced", () => {
expect(locales.t("interp.hello", { int: 'noway' }, 'en')).toBe("Hello noway");
it('should return values with interpolations replaced', () => {
expect(locales.t('interp.hello', { int: 'noway' }, 'en')).toBe('Hello noway');

// test to handle nonexistent interpolaitons, should return the normal {{}} string
expect(locales.t("interp.hello", {}, 'en')).toBe("Hello {{int}}");
expect(locales.t('interp.hello', {}, 'en')).toBe('Hello {{int}}');

// check to prevent XSS
expect(locales.t("interp.hello", { int: '<script>alert("XSS")</script>' }, 'en')).toBe(`Hello &lt;script&gt;alert("XSS")&lt;/script&gt;`);
});
expect(locales.t('interp.hello', { int: '<script>alert("XSS")</script>' }, 'en')).toBe(`Hello &lt;script&gt;alert("XSS")&lt;/script&gt;`);
});
24 changes: 12 additions & 12 deletions tests/localizationFor.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { it, expect } from "vitest";
import { locales } from "../src/test.js";
import { it, expect } from 'vitest';
import { locales } from '../src/test.js';

it("should give multiple keys correctly for both langs", () => {
expect(locales.localizationFor("hello")).toEqual({
en: "Hello",
es: "Hola",
it('should give multiple keys correctly for both langs', () => {
expect(locales.localizationFor('hello')).toEqual({
en: 'Hello',
es: 'Hola',
});
expect(locales.localizationFor("inside.world")).toEqual({
en: "World",
es: "Mundo",
expect(locales.localizationFor('inside.world')).toEqual({
en: 'World',
es: 'Mundo',
});
expect(locales.localizationFor("inside.another/object.key")).toEqual({
en: "key inside",
es: "key dentro",
expect(locales.localizationFor('inside.another/object.key')).toEqual({
en: 'key inside',
es: 'key dentro',
});
});
Loading

0 comments on commit 948b829

Please sign in to comment.