我有这段代码
<div *ngFor="let service of services">
<span><img [src]="service.imgPath" alt="{{ service.name }}"/></span>
<h4>{{ service.name}}</h4>
<p>{{ service.desc }}</p>
</div>
如何翻译具有3个参数的每项服务
在常规情况下,我使用{{ 'something' | translate }}
,其中“某事物”
放在.json文件中
"something" : "translation"
那么如何在ngFor状态下使用它呢? 它从.json读取了一个对象,但不是我在案例中需要的对象数组
答案 0 :(得分:2)
首先,您需要在app.component.ts中导入ngx-translate:
Collecting pylibmc==1.5.1 (from -r requirements.txt (line 2))
Using cached https://files.pythonhosted.org/packages/23/f4/3904b7171e61a83eafee0ed3b1b8efe4d3c6ddc05f7ebdff1831cf0e15f1/pylibmc-1.5.1.tar.gz
Building wheels for collected packages: pylibmc
Running setup.py bdist_wheel for pylibmc ... error
Complete output from command /home/przemek/usosApi34/bin/python3.4 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-859hcscx/pylibmc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-_u6qlw_7 --python-tag cp34:
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.4
creating build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/__main__.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/__init__.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/client.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/consts.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/test.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/pools.py -> build/lib.linux-x86_64-3.4/pylibmc
running build_ext
building '_pylibmc' extension
creating build/temp.linux-x86_64-3.4
creating build/temp.linux-x86_64-3.4/src
gcc -pthread -Wno-unused-result -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE_ZLIB -I/home/przemek/opt/python-3.4.0/include/python3.4m -c src/_pylibmcmodule.c -o build/temp.linux-x86_64-3.4/src/_pylibmcmodule.o -fno-strict-aliasing -std=c99
src/_pylibmcmodule.c: In function ‘PylibMC_Client_init’:
src/_pylibmcmodule.c:209:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int native_serialization, native_deserialization;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_deserialize_native’:
src/_pylibmcmodule.c:642:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
PyObject *retval = NULL;
^~~~~~~~
src/_pylibmcmodule.c: In function ‘PylibMC_Client_gets’:
src/_pylibmcmodule.c:785:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int miss = 0;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_SerializeValue’:
src/_pylibmcmodule.c:1168:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int success;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_serialize_native’:
src/_pylibmcmodule.c:1247:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
const char *value_str = (value_obj == Py_True) ? "1" : "0";
^~~~~
src/_pylibmcmodule.c:1252:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
PyObject *tmp = PyObject_Str(value_obj);
^~~~~~~~
src/_pylibmcmodule.c: In function ‘PylibMC_Client_get_multi’:
src/_pylibmcmodule.c:1793:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
Py_ssize_t key_idx = 0;
^~~~~~~~~~
cc1: some warnings being treated as errors
error: command 'gcc' failed with exit status 1
----------------------------------------
Failed building wheel for pylibmc
Running setup.py clean for pylibmc
Failed to build pylibmc
Installing collected packages: pylibmc
Running setup.py install for pylibmc ... error
Complete output from command /home/przemek/usosApi34/bin/python3.4 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-859hcscx/pylibmc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-1r6f9jsj/install-record.txt --single-version-externally-managed --compile --install-headers /home/przemek/usosApi34/include/site/python3.4/pylibmc:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.4
creating build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/__main__.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/__init__.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/client.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/consts.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/test.py -> build/lib.linux-x86_64-3.4/pylibmc
copying src/pylibmc/pools.py -> build/lib.linux-x86_64-3.4/pylibmc
running build_ext
building '_pylibmc' extension
creating build/temp.linux-x86_64-3.4
creating build/temp.linux-x86_64-3.4/src
gcc -pthread -Wno-unused-result -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE_ZLIB -I/home/przemek/opt/python-3.4.0/include/python3.4m -c src/_pylibmcmodule.c -o build/temp.linux-x86_64-3.4/src/_pylibmcmodule.o -fno-strict-aliasing -std=c99
src/_pylibmcmodule.c: In function ‘PylibMC_Client_init’:
src/_pylibmcmodule.c:209:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int native_serialization, native_deserialization;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_deserialize_native’:
src/_pylibmcmodule.c:642:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
PyObject *retval = NULL;
^~~~~~~~
src/_pylibmcmodule.c: In function ‘PylibMC_Client_gets’:
src/_pylibmcmodule.c:785:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int miss = 0;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_SerializeValue’:
src/_pylibmcmodule.c:1168:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int success;
^~~
src/_pylibmcmodule.c: In function ‘_PylibMC_serialize_native’:
src/_pylibmcmodule.c:1247:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
const char *value_str = (value_obj == Py_True) ? "1" : "0";
^~~~~
src/_pylibmcmodule.c:1252:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
PyObject *tmp = PyObject_Str(value_obj);
^~~~~~~~
src/_pylibmcmodule.c: In function ‘PylibMC_Client_get_multi’:
src/_pylibmcmodule.c:1793:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
Py_ssize_t key_idx = 0;
^~~~~~~~~~
cc1: some warnings being treated as errors
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/home/przemek/usosApi34/bin/python3.4 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-859hcscx/pylibmc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-1r6f9jsj/install-record.txt --single-version-externally-managed --compile --install-headers /home/przemek/usosApi34/include/site/python3.4/pylibmc" failed with error code 1 in /tmp/pip-install-859hcscx/pylibmc/
然后你需要在资产下有两个文件,一个是f.i en.json,一个是英文,另一个是西班牙文,es.json包含两个对象。在en.json中:
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
....
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
...
imports: [
...
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
]
...
在es.json中:
{
"something": "something translation in English"
}
然后,在您提供代码的组件中:
{
"something": "something translation in Spanish"
}
在模板中:
import { TranslateService } from '@ngx-translate/core';
...
constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
}
....
请注意,您的服务对象必须包含某些字符串,以便进行翻译。
答案 1 :(得分:0)
use translate.get('jsonKey').subscribe( (response: any)=> {
services = response;
});
注意 - //确保您的json结构正确