Skip to content

Commit

Permalink
Merge pull request #1463 from jsit/feat/default-to-user-primary-lang
Browse files Browse the repository at this point in the history
feat: Default language dropdowns to user's interface language
  • Loading branch information
jsit committed Jun 24, 2023
2 parents e8dda77 + a2d68cb commit dd43827
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/shared/components/comment/comment-form.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { myAuthRequired } from "@utils/app";
import getUserInterfaceLangId from "@utils/app/user-interface-language";
import { capitalizeFirstLetter } from "@utils/helpers";
import { Component } from "inferno";
import { T } from "inferno-i18next-dess";
Expand Down Expand Up @@ -40,6 +41,8 @@ export class CommentForm extends Component<CommentFormProps, any> {
: undefined
: undefined;

const userInterfaceLangId = getUserInterfaceLangId(this.props.allLanguages);

return (
<div
className={["comment-form", "mb-3", this.props.containerClass].join(
Expand All @@ -49,6 +52,7 @@ export class CommentForm extends Component<CommentFormProps, any> {
{UserService.Instance.myUserInfo ? (
<MarkdownTextArea
initialContent={initialContent}
initialLanguageId={userInterfaceLangId}
showLanguage
buttonTitle={this.buttonTitle}
finished={this.props.finished}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/common/language-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class LanguageSelect extends Component<LanguageSelectProps, any> {
return this.props.iconVersion ? (
this.selectBtn
) : (
<div className="language-select mb-3">
<div className="language-select row mb-3">
<label
className={classNames(
"col-form-label",
Expand Down
6 changes: 5 additions & 1 deletion src/shared/components/common/markdown-textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,12 @@ export class MarkdownTextArea extends Component<
<LanguageSelect
iconVersion
allLanguages={this.props.allLanguages}
// Only set the selected language ID if it exists as an option
// in the dropdown; otherwise, set it to 0 (Undetermined)
selectedLanguageIds={
languageId ? Array.of(languageId) : undefined
languageId && this.props.siteLanguages.includes(languageId)
? [languageId]
: [0]
}
siteLanguages={this.props.siteLanguages}
onChange={this.handleLanguageChange}
Expand Down
8 changes: 4 additions & 4 deletions src/shared/components/post/post-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
myAuth,
myAuthRequired,
} from "@utils/app";
import getUserInterfaceLangId from "@utils/app/user-interface-language";
import {
capitalizeFirstLetter,
debounce,
Expand Down Expand Up @@ -323,11 +324,10 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
}

render() {
const firstLang = this.state.form.language_id;
const selectedLangs = firstLang ? Array.of(firstLang) : undefined;

const url = this.state.form.url;

const userInterfaceLangId = getUserInterfaceLangId(this.props.allLanguages);

return (
<form className="post-form" onSubmit={linkEvent(this, handlePostSubmit)}>
<NavigationPrompt
Expand Down Expand Up @@ -494,8 +494,8 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
</div>
<LanguageSelect
allLanguages={this.props.allLanguages}
selectedLanguageIds={[userInterfaceLangId]}
siteLanguages={this.props.siteLanguages}
selectedLanguageIds={selectedLangs}
multiple={false}
onChange={this.handleLanguageChange}
/>
Expand Down
2 changes: 2 additions & 0 deletions src/shared/utils/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +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 getUserInterfaceLangId from "./user-interface-language";

export {
buildCommentsTree,
Expand Down Expand Up @@ -87,6 +88,7 @@ export {
getIdFromProps,
getRecipientIdFromProps,
getUpdatedSearchId,
getUserInterfaceLangId,
initializeSite,
insertCommentIntoTree,
isAuthPath,
Expand Down
18 changes: 18 additions & 0 deletions src/shared/utils/app/user-interface-language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Language } from "lemmy-js-client";
import { I18NextService } from "../../services/I18NextService";

export default function getUserInterfaceLangId(
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, or "0" for Undetermined
return userLang?.id || 0;
}

0 comments on commit dd43827

Please sign in to comment.