Javascript 如何使用AngularJS i18n为不同的语言定义不同的字体?
我正在使用。我的页面是这样的:Javascript 如何使用AngularJS i18n为不同的语言定义不同的字体?,javascript,angularjs,Javascript,Angularjs,我正在使用。我的页面是这样的: <div translate="main.logged.message" translate-values="{username: '{{account.login}}'}"> 但它不起作用。并且还检查了文档:。我找不到解决办法 怎么做 更新: 我找到一个帖子,上面写着: 首先指定英文字体,然后在字体列表中指定中文字体。这使得英文字体与所需字体呈现,然后中文字体拾取其他字符 它就像:font家族:“拉托”,“微软雅黑” 令人惊讶的是,它是有效的!但
<div translate="main.logged.message" translate-values="{username: '{{account.login}}'}">
但它不起作用。并且还检查了文档:。我找不到解决办法
怎么做
更新: 我找到一个帖子,上面写着: 首先指定英文字体,然后在字体列表中指定中文字体。这使得英文字体与所需字体呈现,然后中文字体拾取其他字符 它就像:
font家族:“拉托”,“微软雅黑”代码>
令人惊讶的是,它是有效的!但这似乎不是一个通用的解决方案。有更好的吗?您可以向rootscope添加一个变量“lang”,然后通过读取cookie或localstorage或通过读取浏览器设置(无论您采取何种方式)来设置此变量。基于此变量,您可以将语言类={{lang}}添加到与ng app属性相同的元素中。然后,您可以根据该类更改字体。如果您提供通过使用$translate.use的函数动态更改语言,那么还可以在rootscope上更改变量“lang”,并将更改写回持久层,如cookie或localstorage
app.controller('Ctrl', ['$translate', '$scope', '$rootScope',
function($translate, $scope, $rootScope) {
//better set this in your run function based
//on your cookie or localstorage
$rootScope.lang = "en";
$scope.changeLanguage = function(langKey) {
$translate.use(langKey);
$rootScope.lang = langKey;
//persist to cookie or localstorage here
};
}
]);
在这里,我使用AngularJS i18n将外部字体样式包含到我的程序中:您可以使用css组件轻松实现这一点
这是我的index.html文件(我只包括了重要部分)
这是我的app.js文件
angular.module('starter', ['ionic', 'pascalprecht.translate'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
// translations for english language
.config(function($translateProvider){
$translateProvider.translations("en", {
hello_message: "Hello Sri Lanka",
});
// translations for sinhala lanugage
$translateProvider.translations("si", {
hello_message: "wdhqfndajka Y%s ,xld",
})
// translations for tamil lanugage
$translateProvider.translations("ti", {
hello_message: "ஹலோ இலங்கை",
})
// default langugae when the application starts
$translateProvider.preferredLanguage("en");
// if the called element is not found this will be set as the default langugae
$translateProvider.fallbackLanguage("en");
})
// controller for button actions
.controller('Ctrl', function($translate, $scope, $rootScope) {
$scope.changeLanguage = function (langKey) {
// langKey is whether si/ti/en
$rootScope.lang = langKey;
// console.log("debug " + $rootScope.ti);
$translate.use(langKey);
$rootScope.lang=langKey;
};
});
您可以在正文中添加一个类,该类使用CSS提供该类的语言信息和样式。@localghost是的,似乎是一个解决方案。angular-i18n定义cookieNG\u TRANSLATE\u LANG\u KEY
,指定当前语言。我们可以根据这个值添加不同的类。但也许有更好的解决办法。最好是一些解决方案native
,我指的是angular-i18n的一些内置解决方案。
<html ng-app="starter" class="{{lang}}">
<ion-header-bar class="bar-stable">
<h1 class="ng-binding {{lang}}">{{ "hello_message" | translate }}</h1>
</ion-header-bar>
@font-face {
font-family: 'DL-Araliya..';
src: url('../fonts/DL-Araliya...eot');
src: url('../fonts/DL-Araliya...eot?#iefix') format('embedded-opentype'),
url('../fonts/DL-Araliya...woff') format('woff'),
url('../fonts/DL-Araliya...ttf') format('truetype');
font-weight: 100;
font-style: normal;}
.si {
font-family: 'DL-Araliya..';
font-weight: 100;
font-style: }
angular.module('starter', ['ionic', 'pascalprecht.translate'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
// translations for english language
.config(function($translateProvider){
$translateProvider.translations("en", {
hello_message: "Hello Sri Lanka",
});
// translations for sinhala lanugage
$translateProvider.translations("si", {
hello_message: "wdhqfndajka Y%s ,xld",
})
// translations for tamil lanugage
$translateProvider.translations("ti", {
hello_message: "ஹலோ இலங்கை",
})
// default langugae when the application starts
$translateProvider.preferredLanguage("en");
// if the called element is not found this will be set as the default langugae
$translateProvider.fallbackLanguage("en");
})
// controller for button actions
.controller('Ctrl', function($translate, $scope, $rootScope) {
$scope.changeLanguage = function (langKey) {
// langKey is whether si/ti/en
$rootScope.lang = langKey;
// console.log("debug " + $rootScope.ti);
$translate.use(langKey);
$rootScope.lang=langKey;
};
});