Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate programmatically with Angular 2 #57

Closed
magar91 opened this issue May 16, 2017 · 11 comments
Closed

translate programmatically with Angular 2 #57

magar91 opened this issue May 16, 2017 · 11 comments

Comments

@magar91
Copy link

magar91 commented May 16, 2017

how can I call the L() function to translate programmatically inside of a .ts class component instead of an .html? I used to be able to do it with global.L() in previous versions of nativescript but with the new version 3.0.1 I can't use the global variable as I used to

@rborn
Copy link
Collaborator

rborn commented May 17, 2017

@magar91 I don't know, no change was made to the way I declare L in the global scope.

@rborn
Copy link
Collaborator

rborn commented May 17, 2017

@magar91 do you put require("globals"); at the beginning of your file?

@magar91
Copy link
Author

magar91 commented May 17, 2017

I'm using the plugin with angular 2 but I didn't use require("globals") before and it worked fine, I will try adding it. I'll come back with an answer soon

@magar91
Copy link
Author

magar91 commented May 17, 2017

@rborn I tried doing adding require("globals") but still I can't call the L() function in my .ts file, any idea how I can use the L() function?

EDIT:
I looked around the project issues and found this

I added the following import to my .ts file

import * as application from "tns-core-modules/application";

and used the L() function like this

application.getResources().L(value);

I can use the L() function now inside my .ts class

@rborn
Copy link
Collaborator

rborn commented May 18, 2017

@magar91 Thank you
I opened this ticket to see why it's doing this.

@rborn
Copy link
Collaborator

rborn commented May 19, 2017

@magar91 there are some replies in the NS ticket, could you give them a shot?
Thank you.

@magar91
Copy link
Author

magar91 commented May 19, 2017

Hi @rborn I tried adding this declare var global : any; as suggested here and it works perfectly

@rborn
Copy link
Collaborator

rborn commented May 19, 2017

@magar91 where did you do this? can it be done in the plugin? So it becomes seamless for other devs?

@magar91
Copy link
Author

magar91 commented May 19, 2017

I added it in the file where I used the function, here is an example of where I used it:

import { Component, OnInit } from "@angular/core";

import { Item } from "./item";
import { ItemService } from "./item.service";

declare var global:any;

@Component({
    selector: "ns-items",
    moduleId: module.id,
    templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
    items: Item[];

    constructor(private itemService: ItemService) { }

    ngOnInit(): void {
        console.info(global.L("translate"))
        this.items = this.itemService.getItems();
    }
}

to be honest I don't know if it can be done in the plugin, I'm sorry

@rborn
Copy link
Collaborator

rborn commented May 19, 2017

ok, thank you, I'll update the docs with your explanations 👍

@rborn rborn closed this as completed May 19, 2017
@isamilo
Copy link

isamilo commented Sep 12, 2020

anyone can help please "ERROR in ../node_modules/nativescript-i18n/angular.ts
Module build failed (from ../node_modules/@ngtools/webpack/src/index.js):
Error: D:\Projects\masaa manne\BarPoint\node_modules\nativescript-i18n\angular.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property"
I try to use localization in my Project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants