返回介绍

在项目中使用

发布于 2025-04-26 18:09:31 字数 1717 浏览 0 评论 0 收藏

修改 index.ios.js 文件内容,引入 LocalizedStrings.js:

/**
  * Sample React Native App
  * https://github.com/facebook/react-native
  */
'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;
var LocalizedString = require('./LocalizedString');
var strings = new LocalizedString({
    en:{
        hello_world: "Hello World"
    },
    fr:{
        hello_world: "Bonjour le monde"
    }
})

var LocalizationSample = React.createClass(
    componentDidMount: function(){
        strings.setLanguage('fr');
        this.setState({});
    },
    render: function() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    {strings.hello_world}
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.ios.js
                </Text>
                <Text style={styles.instructions}>
                    Press Cmd+R to reload, {'\n'}
                    Cmd+D or shake for dev menu
                </Text>
            </View>
        );
    }
});

var styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});

AppRegistry.registerComponent('LocalizationSample', () =>
    LocalizationSample);

一切准备就绪!现在你可以测试使用本地化功能了。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。