Skip to content

Commit 52b9f59

Browse files
Add the ability to override the locale used EddyVerbruggen#74
1 parent deb55c0 commit 52b9f59

File tree

7 files changed

+585
-543
lines changed

7 files changed

+585
-543
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export class AppModule { }
6464

6565
#### Template
6666
```xml
67-
<Label text="{{ 'Hello world !' | L }}"></Label>
68-
<Label text="{{ 'I am %s' | L:'user name' }}"></Label>
67+
<Label text="{{ 'Hello world !' | L }}"/>
68+
<Label text="{{ 'I am %s' | L:'user name' }}"/>
6969
```
7070

7171
#### Script
@@ -85,8 +85,8 @@ application.setResources({ L: localize });
8585

8686
#### Template
8787
```xml
88-
<Label text="{{ L('Hello world !') }}"></Label>
89-
<Label text="{{ L('I am %s', 'user name') }}"></Label>
88+
<Label text="{{ L('Hello world !') }}"/>
89+
<Label text="{{ L('I am %s', 'user name') }}"/>
9090
```
9191

9292
#### Script
@@ -105,7 +105,7 @@ Vue.filter("L", localize);
105105
```
106106

107107
#### Template
108-
```xml
108+
```html
109109
<Label :text="'Hello world !'|L"></Label>
110110
<Label :text="'I am %s'|L('user name')"></Label>
111111
```
@@ -167,7 +167,13 @@ Keys starting with `ios.info.plist.` are used to localize iOS properties:
167167

168168
### How to change the language dynamically at runtime?
169169
This plugin uses the native capabilities of each platform, language selection is therefore made by the OS.
170-
There is no plan to implement this feature in the near future.
170+
171+
On iOS you can programmatically override this language since plugin version 4.2.0 by doing this:
172+
173+
```typescript
174+
import { overrideLocale } from "nativescript-localize/localize";
175+
const localeOverriddenSuccessfully = overrideLocale("en-GB"); // or "nl-NL", etc
176+
```
171177

172178
## Troubleshooting
173179
### The angular localization pipe does not work when in a modal context

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { localize } from "./localize";
1+
import { localize, overrideLocale } from "./localize";
22

33
module.exports = localize;
44
module.exports.default = localize;
55
module.exports.localize = localize;
6+
module.exports.overrideLocale = overrideLocale;

src/localize.android.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ export function localize(key: string, ...args: string[]): string {
2323
}
2424
return vsprintf(localizedString, args);
2525
}
26+
27+
export function overrideLocale(locale: string): boolean {
28+
console.log("overrideLocale is not (yet) implemented on Android");
29+
return false;
30+
}

src/localize.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export declare function localize(key: string, ...args: string[]): string;
2+
export declare function overrideLocale(locale: string): boolean;

src/localize.ios.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { vsprintf } from "sprintf-js";
2-
import * as utils from "utils/utils";
32

43
import { convertAtSignToStringSign } from "./placeholder";
54
import { encodeKey } from "./resource";
65

6+
let bundle;
7+
78
const getBundle = (function () {
8-
let bundle = null;
99
return function () {
10-
if (bundle === null) {
10+
if (!bundle) {
1111
bundle = NSBundle.mainBundle;
1212
}
1313
return bundle;
@@ -18,3 +18,16 @@ export function localize(key: string, ...args: string[]): string {
1818
const localizedString = getBundle().localizedStringForKeyValueTable(encodeKey(key), key, null);
1919
return vsprintf(convertAtSignToStringSign(localizedString), args);
2020
}
21+
22+
export function overrideLocale(locale: string): boolean {
23+
const path = NSBundle.mainBundle.pathForResourceOfType(locale.substring(0, 2), "lproj");
24+
25+
if (!path) {
26+
return false;
27+
}
28+
29+
bundle = NSBundle.bundleWithPath(path);
30+
NSUserDefaults.standardUserDefaults.setObjectForKey([locale], "AppleLanguages");
31+
NSUserDefaults.standardUserDefaults.synchronize();
32+
return true;
33+
}

0 commit comments

Comments
 (0)