4个回答
推荐于2017-11-27 · 知道合伙人互联网行家
关注
展开全部
1在Project设置,添加中英两种语言
2.新建Localizable.strings文件,作为多语言对应的词典,存储多种语言,点击右侧Localization,勾选中英
3.添加一个字段,
在English中,添加:SUBMIT_BTN_TITLE = Go;
在Chinese中,添加:SUBMIT_BTN_TITLE = 开始;
4.一个工具类GDLocalizableController,用来切换本地语言:
//
// GDLocalizableController.h
// guide-book
//
// Created by why on 7/16/14.
// Copyright (c) 2014 why. All rights reserved.
//
#import
@interface GDLocalizableController : NSObject
+(NSBundle *)bundle;//获取当前资源文件
+(void)initUserLanguage;//初始化语言文件
+(NSString *)userLanguage;//获取应用当前语言
+(void)setUserlanguage:(NSString *)language;//设置当前语言
@end
//
// GDLocalizableController.m
// guide-book
//
// Created by why on 7/16/14.
// Copyright (c) 2014 why. All rights reserved.
//
#import GDLocalizableController.h
@implementation GDLocalizableController
static NSBundle *bundle = nil;
+ ( NSBundle * )bundle{
return bundle;
}
+(void)initUserLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *string = [def valueForKey:@userLanguage];
if(string.length == 0){
//获取系统当前语言版本
NSArray* languages = [def objectForKey:@AppleLanguages];
NSString *current = [languages objectAtIndex:0];
string = current;
[def setValue:current forKey:@userLanguage];
[def synchronize];//持久化,不加的话不会保存
}
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:string ofType:@lproj];
bundle = [NSBundle bundleWithPath:path];//生成bundle
}
+(NSString *)userLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *language = [def valueForKey:@userLanguage];
return language;
}
+(void)setUserlanguage:(NSString *)language{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//1.第一步改变bundle的值
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@lproj ];
bundle = [NSBundle bundleWithPath:path];
//2.持久化
[def setValue:language forKey:@userLanguage];
[def synchronize];
}
@end
5. 自定义一个宏方便处理:
// ----- 多语言设置
#define CHINESE @zh-Hans
#define ENGLISH @en
#define GDLocalizedString(key) [[GDLocalizableController bundle] localizedStringForKey:(key) value:@ table:nil]
6.使用:
[GDLocalizableController setUserlanguage:CHINESE];
NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));
[GDLocalizableController setUserlanguage:ENGLISH];
NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));
2.新建Localizable.strings文件,作为多语言对应的词典,存储多种语言,点击右侧Localization,勾选中英
3.添加一个字段,
在English中,添加:SUBMIT_BTN_TITLE = Go;
在Chinese中,添加:SUBMIT_BTN_TITLE = 开始;
4.一个工具类GDLocalizableController,用来切换本地语言:
//
// GDLocalizableController.h
// guide-book
//
// Created by why on 7/16/14.
// Copyright (c) 2014 why. All rights reserved.
//
#import
@interface GDLocalizableController : NSObject
+(NSBundle *)bundle;//获取当前资源文件
+(void)initUserLanguage;//初始化语言文件
+(NSString *)userLanguage;//获取应用当前语言
+(void)setUserlanguage:(NSString *)language;//设置当前语言
@end
//
// GDLocalizableController.m
// guide-book
//
// Created by why on 7/16/14.
// Copyright (c) 2014 why. All rights reserved.
//
#import GDLocalizableController.h
@implementation GDLocalizableController
static NSBundle *bundle = nil;
+ ( NSBundle * )bundle{
return bundle;
}
+(void)initUserLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *string = [def valueForKey:@userLanguage];
if(string.length == 0){
//获取系统当前语言版本
NSArray* languages = [def objectForKey:@AppleLanguages];
NSString *current = [languages objectAtIndex:0];
string = current;
[def setValue:current forKey:@userLanguage];
[def synchronize];//持久化,不加的话不会保存
}
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:string ofType:@lproj];
bundle = [NSBundle bundleWithPath:path];//生成bundle
}
+(NSString *)userLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *language = [def valueForKey:@userLanguage];
return language;
}
+(void)setUserlanguage:(NSString *)language{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//1.第一步改变bundle的值
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@lproj ];
bundle = [NSBundle bundleWithPath:path];
//2.持久化
[def setValue:language forKey:@userLanguage];
[def synchronize];
}
@end
5. 自定义一个宏方便处理:
// ----- 多语言设置
#define CHINESE @zh-Hans
#define ENGLISH @en
#define GDLocalizedString(key) [[GDLocalizableController bundle] localizedStringForKey:(key) value:@ table:nil]
6.使用:
[GDLocalizableController setUserlanguage:CHINESE];
NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));
[GDLocalizableController setUserlanguage:ENGLISH];
NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));
追问
那个,大神,这是啥语言的代码?这种切换方式能用于html不?
展开全部
使用.JSON实现本地语言存储下面具体介绍一下怎样实现静态页面语言切换:
1.准备好存贮不同语言的.json文件: text-en.json text-ja.json
text-ja.json :
{
"hpt": {
"features": "特徴",
"tutorial": "商品说明",
"support": "サポート",
"management": "マネージメント"
}
}
text-en.json :
{
"hpt": {
"features": "FEATURES",
"tutorial": "TUTORIAL",
"support": "SUPPORT",
"management": "MANAGEMENT"
}
}
2.然后页面要加载 jquery.localize.js jquery.min.js和language_cookie.js
language_cookie.js :
var name = "somoveLanguage";
function chgLang() {
var value = $("#ddlSomoveLanguage").children('option:selected').val();
SetCookie(name,value);
location.reload();
}
function SetCookie(name,value){
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null
}
$(function() {
var uulanguage = (navigator.language || navigator.browserLanguage).toLowerCase();
if (uulanguage.indexOf("en") > -1) {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
}
else if (uulanguage.indexOf("ja") > -1) {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "ja"});
}else{
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
};
if (getCookie(name) != "") {
if (getCookie(name) == "ja") {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "ja"});
}
if (getCookie(name) == "en") {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
}
} //根据cookie选择语言
});
3.html代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Language</title>
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
<script src="js/language_cookie.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="top_lan">
<select id="ddlSomoveLanguage" onchange="chgLang();">
<option value="">LAGUAGE</option>
<option value="ja">日本语</option>
<option value="en">ENGLISH</option>
</select>
</div>
<div id="prod_navright">
<ul>
<li><a data-localize="hpt.management" href="hospitality_management_ja.html" >MANAGEMENT</a></li>
<li><a data-localize="hpt.support" href="hospitality_support_ja.html">SUPPORT</a></li>
<li><a data-localize="hpt.tutorial" href="hospitality_tutorial_ja.html">TUTORIAL</a></li>
<li><a data-localize="hpt.features" href="hospitality_features_ja.html">FEATURES</a></li>
</ul>
</div>
</body>
</html>
1.准备好存贮不同语言的.json文件: text-en.json text-ja.json
text-ja.json :
{
"hpt": {
"features": "特徴",
"tutorial": "商品说明",
"support": "サポート",
"management": "マネージメント"
}
}
text-en.json :
{
"hpt": {
"features": "FEATURES",
"tutorial": "TUTORIAL",
"support": "SUPPORT",
"management": "MANAGEMENT"
}
}
2.然后页面要加载 jquery.localize.js jquery.min.js和language_cookie.js
language_cookie.js :
var name = "somoveLanguage";
function chgLang() {
var value = $("#ddlSomoveLanguage").children('option:selected').val();
SetCookie(name,value);
location.reload();
}
function SetCookie(name,value){
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null
}
$(function() {
var uulanguage = (navigator.language || navigator.browserLanguage).toLowerCase();
if (uulanguage.indexOf("en") > -1) {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
}
else if (uulanguage.indexOf("ja") > -1) {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "ja"});
}else{
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
};
if (getCookie(name) != "") {
if (getCookie(name) == "ja") {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "ja"});
}
if (getCookie(name) == "en") {
$("[data-localize]").localize("text", {pathPrefix: "lang", language: "en"});
}
} //根据cookie选择语言
});
3.html代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Language</title>
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
<script src="js/language_cookie.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="top_lan">
<select id="ddlSomoveLanguage" onchange="chgLang();">
<option value="">LAGUAGE</option>
<option value="ja">日本语</option>
<option value="en">ENGLISH</option>
</select>
</div>
<div id="prod_navright">
<ul>
<li><a data-localize="hpt.management" href="hospitality_management_ja.html" >MANAGEMENT</a></li>
<li><a data-localize="hpt.support" href="hospitality_support_ja.html">SUPPORT</a></li>
<li><a data-localize="hpt.tutorial" href="hospitality_tutorial_ja.html">TUTORIAL</a></li>
<li><a data-localize="hpt.features" href="hospitality_features_ja.html">FEATURES</a></li>
</ul>
</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有两种方法:
自己写一个js语言脚本,进行切换操作
从网上找一个js脚本语言
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用本地文件,使用json对象保存语言中英文对应数据;然后根据选择展示;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询