]> Untitled Git - lemmy-ui.git/commitdiff
fix: Use Interface Language instead
authorJay Sitter <jay@jaysitter.com>
Fri, 23 Jun 2023 19:14:16 +0000 (15:14 -0400)
committerJay Sitter <jay@jaysitter.com>
Fri, 23 Jun 2023 21:24:07 +0000 (17:24 -0400)
src/shared/components/comment/comment-form.tsx
src/shared/components/post/post-form.tsx
src/shared/utils/app/index.ts
src/shared/utils/app/user-interface-language.ts [new file with mode: 0644]
src/shared/utils/app/user-primary-language.ts [deleted file]

index 11366fc4b95a572308b19dfae1ed1f29be36390e..0226e23ef13cdcce5bc54c57d98ca3f6242d9ac9 100644 (file)
@@ -1,5 +1,5 @@
 import { myAuthRequired } from "@utils/app";
-import getUserPrimaryLanguage from "@utils/app/user-primary-language";
+import getUserInterfaceLangId from "@utils/app/user-interface-language";
 import { capitalizeFirstLetter } from "@utils/helpers";
 import { Component } from "inferno";
 import { T } from "inferno-i18next-dess";
@@ -41,9 +41,10 @@ export class CommentForm extends Component<CommentFormProps, any> {
           : undefined
         : undefined;
 
-    const userPrimaryLanguage = getUserPrimaryLanguage(
-      UserService?.Instance?.myUserInfo
-    );
+    const userInterfaceLangId = getUserInterfaceLangId({
+      myUserInfo: UserService.Instance.myUserInfo,
+      allLanguages: this.props.allLanguages,
+    });
 
     return (
       <div
@@ -54,7 +55,7 @@ export class CommentForm extends Component<CommentFormProps, any> {
         {UserService.Instance.myUserInfo ? (
           <MarkdownTextArea
             initialContent={initialContent}
-            initialLanguageId={userPrimaryLanguage}
+            initialLanguageId={userInterfaceLangId}
             showLanguage
             buttonTitle={this.buttonTitle}
             finished={this.props.finished}
index 337cc6e3e43bcb7b0a88238a82dcf7e5ef0bd906..f14eac739372092650d088ad11ab0e7c18218e06 100644 (file)
@@ -4,7 +4,7 @@ import {
   myAuth,
   myAuthRequired,
 } from "@utils/app";
-import getUserPrimaryLanguage from "@utils/app/user-primary-language";
+import getUserInterfaceLangId from "@utils/app/user-interface-language";
 import {
   capitalizeFirstLetter,
   debounce,
@@ -324,12 +324,13 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
   }
 
   render() {
-    const userPrimaryLanguage = getUserPrimaryLanguage(
-      UserService?.Instance?.myUserInfo
-    );
-
     const url = this.state.form.url;
 
+    const userInterfaceLangId = getUserInterfaceLangId({
+      myUserInfo: UserService.Instance.myUserInfo,
+      allLanguages: this.props.allLanguages,
+    });
+
     return (
       <form className="post-form" onSubmit={linkEvent(this, handlePostSubmit)}>
         <NavigationPrompt
@@ -496,7 +497,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
         </div>
         <LanguageSelect
           allLanguages={this.props.allLanguages}
-          selectedLanguageIds={[userPrimaryLanguage]}
+          selectedLanguageIds={[userInterfaceLangId]}
           siteLanguages={this.props.siteLanguages}
           multiple={false}
           onChange={this.handleLanguageChange}
index 2e459516297d93fd95e5fb73456ae171375f12c0..f98357d7a9150d6259d617018d894d64bd827142 100644 (file)
@@ -53,7 +53,7 @@ import showScores from "./show-scores";
 import siteBannerCss from "./site-banner-css";
 import updateCommunityBlock from "./update-community-block";
 import updatePersonBlock from "./update-person-block";
-import getUserPrimaryLanguage from "./user-primary-language";
+import getUserInterfaceLangId from "./user-interface-language";
 
 export {
   buildCommentsTree,
@@ -88,7 +88,7 @@ export {
   getIdFromProps,
   getRecipientIdFromProps,
   getUpdatedSearchId,
-  getUserPrimaryLanguage,
+  getUserInterfaceLangId,
   initializeSite,
   insertCommentIntoTree,
   isAuthPath,
diff --git a/src/shared/utils/app/user-interface-language.ts b/src/shared/utils/app/user-interface-language.ts
new file mode 100644 (file)
index 0000000..5ccd61c
--- /dev/null
@@ -0,0 +1,22 @@
+import { Language, MyUserInfo } from "lemmy-js-client";
+import { I18NextService } from "../../services/I18NextService";
+
+export default function getUserInterfaceLanguageId({
+  myUserInfo,
+  allLanguages,
+}: {
+  myUserInfo: MyUserInfo;
+  allLanguages: Language[];
+}): number {
+  // Get the string of the browser- or user-defined language, like en-US
+  const i18nLang = I18NextService.i18n.language;
+
+  // Find the Language object with a code that matches the initial characters of
+  // this string
+  const userLang = allLanguages.find(lang => {
+    return i18nLang.indexOf(lang.code) === 0;
+  });
+
+  // Return the ID of that language object
+  return userLang.id || 0;
+}
diff --git a/src/shared/utils/app/user-primary-language.ts b/src/shared/utils/app/user-primary-language.ts
deleted file mode 100644 (file)
index 06ae259..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-import { UserService } from "../../services";
-
-export default function getUserPrimaryLanguage(
-  myUserInfo = UserService.Instance.myUserInfo
-): number {
-  // Get first language in discussion languages array that isn't equal to "0",
-  // which is the language selection "Undetermined"
-  return myUserInfo?.discussion_languages.find(lang => lang !== 0) || 0;
-}