1

I want to use WebView to show some html content

here is an example:

return (
            <WebView
                    style={styles.container}
                    source={source}
                    scalesPageToFit={Boolean(true)}
                    onNavigationStateChange={this._onNavigationStateChange} />
    )

and for the source variable I need to have two different values:

1) for android platform I need to use something like this:

source = {uri: `file:///android_asset/contents/${languageId}text.html`}

2) for ios I need to use smth. like this:

source = require(`../srv/localization/contents/${languageId}text.html`)

For android it works well, but for ios it doesn't work. And this url works fine for iOS also

require(`../srv/localization/contents/entext.html`)

As I understand that is because of dynamic url (${languageId}text.html)

The question is how to use dynamic urls for iOS?

1 Answer 1

6

As you find out, you can't have dynamic url for require. That's because require get the source at the app start regardless it's place in the code. You shuld require all of the {languageId}text.html and pass the required variable to the source:

var language = {
   en: require(`../srv/localization/contents/entext.html`)
   ...
}

and use it as below:

source = require(language[en])
Sign up to request clarification or add additional context in comments.

2 Comments

But what if I don't want to require all files. In one case I need to require entext.html, but in other case I need to require pttext.html. I don't want to store all the files that will probably required in the future. I want to add all necessary files before making a build and require them by prefix in the code @Meysam Izadmehr
If your user can change the language in he app, there is not other way with require. If the user can't change the language and text.html is not a small file, make a build for each language. If you don't want to require all the files, maybe you shouldn't use it in the first place. for example you can fetching it from server when user want to change language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.