Skip to content

Commit

Permalink
feat: take into account nested structure of translations
Browse files Browse the repository at this point in the history
  • Loading branch information
myandrienko committed Feb 15, 2024
1 parent d45b62c commit 9957842
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"isomorphic-ws": "^4.0.1",
"linkifyjs": "^4.1.0",
"lodash.debounce": "^4.0.8",
"lodash.defaultsdeep": "^4.6.1",
"lodash.throttle": "^4.1.1",
"lodash.uniqby": "^4.7.0",
"nanoid": "^3.3.4",
Expand Down Expand Up @@ -154,6 +155,7 @@
"@types/jsdom": "^21.1.5",
"@types/linkifyjs": "^2.1.3",
"@types/lodash.debounce": "^4.0.7",
"@types/lodash.defaultsdeep": "^4.6.9",
"@types/lodash.throttle": "^4.1.7",
"@types/lodash.uniqby": "^4.7.7",
"@types/moment": "^2.13.0",
Expand Down
15 changes: 10 additions & 5 deletions src/i18n/Streami18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import localeData from 'dayjs/plugin/localeData';
import relativeTime from 'dayjs/plugin/relativeTime';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import defaultsDeep from 'lodash.defaultsdeep';

import type momentTimezone from 'moment-timezone';
import type { TranslationLanguages } from 'stream-chat';
Expand Down Expand Up @@ -251,7 +252,11 @@ export type Streami18nOptions = {
logger?: (message?: string) => void;
parseMissingKeyHandler?: (key: string, defaultValue?: string) => string;
timezone?: string;
translationsForLanguage?: Partial<typeof enTranslations>;
translationsForLanguage?: DeepPartial<typeof enTranslations>;
};

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Record<string, unknown> ? DeepPartial<T[P]> : T[P];
};

/**
Expand Down Expand Up @@ -532,10 +537,10 @@ export class Streami18n {
[defaultNS]:
this.translations[this.currentLanguage] &&
this.translations[this.currentLanguage][defaultNS]
? {
...this.translations[this.currentLanguage][defaultNS],
...translationsForLanguage,
}
? defaultsDeep(
translationsForLanguage,
this.translations[this.currentLanguage][defaultNS],
)
: translationsForLanguage,
};
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,13 @@
dependencies:
"@types/lodash" "*"

"@types/lodash.defaultsdeep@^4.6.9":
version "4.6.9"
resolved "https://registry.yarnpkg.com/@types/lodash.defaultsdeep/-/lodash.defaultsdeep-4.6.9.tgz#050fbe389a7a6e245b15da9ee83a8a62f047a1c4"
integrity sha512-pLtCFK0YkHfGtGLYLNMTbFB5/G5+RsmQCIbbHH8GOAXjv+gDkVilY98kILfe8JH2Kev0OCReYxp1AjxEjP8ixA==
dependencies:
"@types/lodash" "*"

"@types/lodash.throttle@^4.1.7":
version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/lodash.throttle/-/lodash.throttle-4.1.7.tgz#4ef379eb4f778068022310ef166625f420b6ba58"
Expand Down Expand Up @@ -9326,6 +9333,11 @@ lodash.deburr@^4.1.0:
resolved "https://registry.yarnpkg.com/lodash.deburr/-/lodash.deburr-4.1.0.tgz#ddb1bbb3ef07458c0177ba07de14422cb033ff9b"
integrity sha1-3bG7s+8HRYwBd7oH3hRCLLAz/5s=

lodash.defaultsdeep@^4.6.1:
version "4.6.1"
resolved "https://registry.yarnpkg.com/lodash.defaultsdeep/-/lodash.defaultsdeep-4.6.1.tgz#512e9bd721d272d94e3d3a63653fa17516741ca6"
integrity sha512-3j8wdDzYuWO3lM3Reg03MuQR957t287Rpcxp1njpEa8oDrikb+FwGdW3n+FELh/A6qib6yPit0j/pv9G/yeAqA==

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
Expand Down

0 comments on commit 9957842

Please sign in to comment.