Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18nManager.forceRTL doesn't apply changes on react-native components without reloading the app #16215

Closed
hadar-ayoub opened this issue Oct 5, 2017 · 16 comments
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@hadar-ayoub
Copy link

Is this a bug report?

(yes)

I was working lately on a mobile app using react-native, i was led to implement i18n for both languages (arabic and english). to switch between these two languages, i force RTL using I18nManager but the app needs to be reloaded to apply the changes, i use react-native-restart to reload app automatically after persisting the chosen language. is there any convenient way to force RTL without restating the app. Here is my code snippets :

Have you read the Contributing Guidelines?

(yes)

Environment

Environment:
OS: macOS Sierra 10.12.4
Node: 7.10.0
Yarn: 0.23.4
npm: 4.2.0
Watchman: 4.7.0
Xcode: Version 8.3.2 Build version 8E2002
Android Studio: 2.2.3 AI.145.3537739

Packages: (wanted => installed)
react-native: 0.47.2 => 0.47.2
react: 16.0.0-alpha.12 => 16.0.0-alpha.12
react-native-localization: 0.1.32 => 0.1.32

Steps to Reproduce

  1. create a screen with a Switch component to change displaying react-native components from LTR to RTL
  2. call I18nManager.forceRTL method on the onValueChange param of the Switch component
  3. call setLanguage method to set RTL language (arabic, hebrew …)

Expected Behavior

Render the screen with the chosen RTL language and alignement

Actual Behavior

Render the screen with chosen language but screen components doesn’t change alignement from right to left.

Reproducible Demo

(https://github.com/hadar-ayoub/react-native-RTLdemo.git)

@amorenew
Copy link

amorenew commented Oct 5, 2017

@hadar-ayoub there is a library to restart the app
https://github.com/avishayil/react-native-restart

@hadar-ayoub
Copy link
Author

thanks @amorenew, i'm currently using react-native-restart library to apply RTL changes but i'm looking for an other way without restarting the app.

@amorenew
Copy link

amorenew commented Oct 5, 2017

@hadar-ayoub there is no other way and it's not a bug
otherwise you will need to make a custom handling
like render isArabic return arabic view else return english view

@amorenew
Copy link

amorenew commented Oct 5, 2017

@hadar-ayoub I think we can make a class for handling it
like
I18nManager.localizeStyle(style,language)
and in localizeStyle function we can flip style value
1-search for value called alignText
https://stackoverflow.com/questions/20804163/check-if-a-key-exists-inside-a-json-object
2-flip the value of alignText

(alignText =='left' ){
  alignText='righ'}

@hadar-ayoub
Copy link
Author

hi @amorenew, sorry for my late reply.

It's just not about alignText. I need to handle flexDirection ("row" & "row-reverse"), padding & margin (Left, Right), also change "flex-end" to "flex-start" and vice-versa, and changePosition of the drawer from left to right.

I managed to overcome this issue by creating a class and a static method which take the state of isRTL dispatched with redux. Here is my code snippets :

// styles.js
export default class StyleSheetFactory {
     static getSheet(isRTL) {
       return StyleSheet.create({
          container: {
              flexDirection : isRTL ? "row-reverse" : "row",
              paddingLeft :  isRTL ? a : b,
              paddingRight: isRTL ? b : a,
              marginLeft :  isRTL ? a : b,
              marginRight: isRTL ? b : a,
          },
          text:{
             textAlign: isRTL ? 'right':'left',
          },
         image:{
            transform: [{scaleX: isRTL ? -1 : 1}]
         },
      )}
    }
}
// index.js
import StyleSheetFactory from './styles'

class App extends Component {
   render(){
       const styles = StyleSheetFactory.getsheet(this.props.isRTL);
       return (...)
   }
}
const mapStateToProps = (state) => {
    return {
       isRTL : state.settings.isRTL,
    };
};
export default connect(mapStateToProps)(App);

I'll make soon a functional demo on my github repo .

hope this solution works for you, until this issue is fixed on the upcoming releases.

Cheers,

@stale
Copy link

stale bot commented Dec 12, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 12, 2017
@stale stale bot closed this as completed Dec 19, 2017
@gentlee
Copy link

gentlee commented Jan 14, 2018

Issue should be reopened and fixed.

@mhemrg
Copy link

mhemrg commented Jan 27, 2018

Same issue with forceRTL method. The issue should be reopened.

@ovy9086
Copy link

ovy9086 commented Jan 30, 2018

yep. issue must be reopened

@eleboys
Copy link

eleboys commented Feb 24, 2018

same issue here.
RN version : 0.53

@iamsoorena
Copy link

same issue
RN version: 0.55

@webeasystep
Copy link

webeasystep commented Jul 5, 2018

is there is any news about rtl ltr switcher ?

@rawit1976
Copy link

Verry good

@Fortidude
Copy link

Ye, I would like to see any other solution. React-native-restart is not a good option in my opinion.

@itskevinsam
Copy link

ping.. same issue

@renecardozo
Copy link

in iOS after made forceRTL it is not possible to change to LTR anymore even when I used react-native-restart and to also does not change nothing and keep the screens in RTL

@facebook facebook locked as resolved and limited conversation to collaborators Dec 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests