Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

[NEW] Add setLanguage method in livechat api methods#214

Merged
renatobecker-zz merged 6 commits intoRocketChat:devfrom
knrt10:setLanguage
Apr 25, 2019
Merged

[NEW] Add setLanguage method in livechat api methods#214
renatobecker-zz merged 6 commits intoRocketChat:devfrom
knrt10:setLanguage

Conversation

@knrt10
Copy link
Copy Markdown
Contributor

@knrt10 knrt10 commented Apr 16, 2019

TODO

  • Store language in iframe
  • Create a new file for language handling and handle all the task there

@knrt10 knrt10 changed the title [WIP] Add setLanguage method in livechat api methods [NEW] Add setLanguage method in livechat api methods Apr 17, 2019
@knrt10 knrt10 force-pushed the setLanguage branch 2 times, most recently from 213950b to 9d593dd Compare April 17, 2019 17:57
await loadConfig();
this.handleTriggers();
CustomFields.init();
Language.init();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you create a class to handle Language stuff instead of just exporting the methods you need to use?

src/lib/hooks.js Outdated
@@ -1,5 +1,6 @@
import Triggers from './triggers';
import CustomFields from './customFields';
import Language from './language';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a class to call just one method, you can create a file and export the method there.

src/lib/hooks.js Outdated
},

setLanguage(language) {
Language.setLanguage(language);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, export the setLanguage method rather than instantiate a class to call the method.

@@ -0,0 +1,68 @@
import store from '../store';

class Language {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need all of this stuff. The idea is to export the method(s) you want to call.
Please fix it.

@knrt10
Copy link
Copy Markdown
Contributor Author

knrt10 commented Apr 24, 2019

Updated changes

/**
* To get browser Language of user
*/
export const browserLanguage = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to export this method.

* To normalize Language String and return language code
* @param {String} languageString
*/
export const normalizeLanguageString = (languageString) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to export this method.

}

return countryCode ? `${ languageCode }_${ countryCode }` : languageCode;
const iframeLanguage = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're calling updateWidgetLanguage(), I think you don't need this block of code here, right?

};

I18n.changeLocale(normalizeLanguageString(configLanguage() || browserLanguage()));
I18n.changeLocale(normalizeLanguageString(iframeLanguage() ? iframeLanguage() : configLanguage() || browserLanguage()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this code into updateWidgetLanguage method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

expanded,
alerts,
modal,
iframe,
Copy link
Copy Markdown
Contributor

@renatobecker-zz renatobecker-zz Apr 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still necessary?

src/lib/hooks.js Outdated

setLanguage(language) {
setLanguage(language);
updateWidgetLanguage();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this method inside setLanguage method.

*/
export const setLanguage = (language) => {
const { iframe } = store.state;
store.setState({ iframe: { ...iframe, language } });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
store.setState({ iframe: { ...iframe, language } });
await store.setState({ iframe: { ...iframe, language } });

* This will update langauge of widget
*/
export const updateWidgetLanguage = () => {
const { iframe } = store.state;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use Object destructuring below as well.

*/
export const updateWidgetLanguage = () => {
const { iframe } = store.state;
if (iframe && iframe.language) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve this code. Instead of if/else, prepare the language value before calling I18n.changeLocale.
Please, improve the way you write the code.

* This will update langauge of widget
*/
export const updateWidgetLanguage = () => {
const { iframe: { language } = {} } = store.state;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not dealing with this information on configLanguage method?
You don't need to read the store so many times since you're reading the store on configLanguage method.
Please, test the iframe data on configLanguage method.

*/
export const updateWidgetLanguage = () => {
const { iframe: { language } = {} } = store.state;
I18n.changeLocale(language ? language : normalizeLanguageString(configLanguage() || browserLanguage()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
I18n.changeLocale(language ? language : normalizeLanguageString(configLanguage() || browserLanguage()));
I18n.changeLocale(normalizeLanguageString(configLanguage() || browserLanguage()));

Related to the comment I left above..

/**
* This will update langauge of widget
*/
export const updateWidgetLanguage = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's replace the name of this method by setWidgetLanguage.

@knrt10 knrt10 force-pushed the setLanguage branch 3 times, most recently from 243e33d to 9fef76a Compare April 25, 2019 13:29
/**
* To get browser Language of user
*/
const browserLanguage = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const browserLanguage = () => {
const browserLanguage = () => (navigator.userLanguage || navigator.language);

Use the correct arrow function statement. Your function does not have a return.

src/lib/hooks.js Outdated
createOrUpdateGuest(data);
},

setLanguage(language) {
Copy link
Copy Markdown
Contributor

@renatobecker-zz renatobecker-zz Apr 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this method here, you can set the iframe language on the setLanguage method created on hooks.js.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants