Skip to content

Commit

Permalink
add example to test the localizations
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschaller committed Aug 9, 2023
1 parent 0e36965 commit 48a41d5
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 28 deletions.
27 changes: 27 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/MapLibreLocalization.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mapbox.mapboxgl

import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.style.expressions.Expression
import com.mapbox.mapboxsdk.style.layers.PropertyFactory
import com.mapbox.mapboxsdk.style.layers.SymbolLayer

class MapLibreLocalization(private val mapBoxMap: MapboxMap) {

fun setMapLanguage(language: String) {
val layers = mapBoxMap.style?.layers ?: emptyList()

val symbolLayers = layers.filterIsInstance<SymbolLayer>()

for (layer in symbolLayers) {
val expression = layer.textField.expression ?: continue

val languageNameRegex = Regex("(name:[a-z][a-z])")

val newExpression = expression
.toString()
.replace(languageNameRegex, "name:$language")

layer.setProperties(PropertyFactory.textField(Expression.raw(newExpression)))
}
}
}
20 changes: 3 additions & 17 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.mapbox.mapboxsdk.style.sources.ImageSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import com.mapbox.mapboxsdk.style.types.Formatted;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -644,7 +645,7 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
try {
final Locale deviceLocale = Locale.getDefault();

setMapLanguage(deviceLocale.getLanguage());
new MapLibreLocalization(mapboxMap).setMapLanguage(deviceLocale.getLanguage());

result.success(null);
} catch (RuntimeException exception) {
Expand Down Expand Up @@ -674,7 +675,7 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
{
final String language = call.argument("language");
try {
setMapLanguage(language);
new MapLibreLocalization(mapboxMap).setMapLanguage(language);
result.success(null);
} catch (RuntimeException exception) {
Log.d(TAG, exception.toString());
Expand Down Expand Up @@ -1795,21 +1796,6 @@ public void setAttributionButtonMargins(int x, int y) {
}
}

private void setMapLanguage(String language){
final List<Layer> layers = mapboxMap.getStyle().getLayers();

final String expressionValue = String.format("['get', 'name:%s']", language);

final PropertyValue<Expression> newExpression =
PropertyFactory.textField(Expression.raw(expressionValue));

for (final Layer layer : layers) {
if(layer instanceof SymbolLayer) {
layer.setProperties(newExpression);
}
}
}

private void updateMyLocationEnabled() {
if (this.locationComponent == null && myLocationEnabled) {
enableLocationComponent(mapboxMap.getStyle());
Expand Down
1 change: 1 addition & 0 deletions example/lib/full_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class FullMapState extends State<FullMap> {
// ),
// ),
body: MaplibreMap(
styleString: "https://api.maptiler.com/maps/3dd4d51b-ae78-4074-8b31-b47a49f1b5ce/style.json?key=kZ5xAKKbPzxo3GeJ2odT",
// TODO: styleString: isLight ? MapboxStyles.LIGHT : MapboxStyles.DARK,
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
Expand Down
80 changes: 80 additions & 0 deletions example/lib/localized_map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:maplibre_gl/mapbox_gl.dart';

import 'page.dart';

class LocalizedMapPage extends ExamplePage {
const LocalizedMapPage({super.key})
: super(const Icon(Icons.map), 'Localized screen map');

@override
Widget build(BuildContext context) {
return const LocalizedMap();
}
}

class LocalizedMap extends StatefulWidget {
const LocalizedMap({super.key});

@override
State createState() => LocalizedMapState();
}

class LocalizedMapState extends State<LocalizedMap> {
final _mapReadyCompleter = Completer<MaplibreMapController>();

var _mapLanguage = "en";

@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
DropdownButton<String>(
value: _mapLanguage,
icon: const Icon(Icons.arrow_drop_down),
elevation: 16,
onChanged: (value) {
if (value == null) return;

setState(() => _mapLanguage = value);
_setMapLanguage();
},
items: ["en", "de", "es", "pl"]
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
Expanded(
child: MaplibreMap(
styleString:
"https://api.maptiler.com/maps/3dd4d51b-ae78-4074-8b31-b47a49f1b5ce/style.json?key=kZ5xAKKbPzxo3GeJ2odT",
onMapCreated: _onMapCreated,
initialCameraPosition:
const CameraPosition(target: LatLng(0.0, 0.0)),
onStyleLoadedCallback: _onStyleLoadedCallback,
),
),
],
),
);
}

void _onMapCreated(MaplibreMapController controller) {
_mapReadyCompleter.complete(controller);
}

void _onStyleLoadedCallback() {
_setMapLanguage();
}

Future<void> _setMapLanguage() async {
final controller = await _mapReadyCompleter.future;
controller.setMapLanguage(_mapLanguage);
}
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:location/location.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:maplibre_gl_example/get_map_informations.dart';
import 'package:maplibre_gl_example/given_bounds.dart';
import 'package:maplibre_gl_example/localized_map.dart';
import 'package:maplibre_gl_example/no_location_permission_page.dart';

import 'animate_camera.dart';
Expand Down Expand Up @@ -37,6 +38,7 @@ import 'package:maplibre_gl/mapbox_gl.dart';
final List<ExamplePage> _allPages = <ExamplePage>[
const MapUiPage(),
const FullMapPage(),
const LocalizedMapPage(),
const AnimateCameraPage(),
const MoveCameraPage(),
const PlaceSymbolPage(),
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
for layer in layers {
if let symbolLayer = layer as? MGLSymbolStyleLayer {

symbolLayer.text = NSExpression(forConstantValue: expressionValue)
symbolLayer.text = NSExpression(forKeyPath: expressionValue)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions maplibre_gl_web/lib/mapbox_gl_web.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library maplibre_gl_web;

import 'dart:async';
import 'dart:convert';
import 'dart:developer';
// FIXED HERE: https://github.com/dart-lang/linter/pull/1985
// ignore_for_file: avoid_web_libraries_in_flutter
import 'dart:html';
Expand Down
9 changes: 1 addition & 8 deletions maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,8 @@ class MaplibreMapController extends MapLibreGlPlatform

@override
Future<void> setMapLanguage(String language) async {
final List<dynamic> layers = _map.getStyle().layers;

final newExpression = ['get', 'name:' + language];

for (final layer in layers) {
if (layer.type == 'symbol') {
_map.setLayoutProperty(layer.id, 'text-field', newExpression);
}
}
//TODO
}

@override
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ dependencies:
git:
url: https://github.com/maplibre/flutter-maplibre-gl.git
path: maplibre_gl_platform_interface
ref: main
ref: 250-change-language-fixes
maplibre_gl_web:
git:
url: https://github.com/maplibre/flutter-maplibre-gl.git
path: maplibre_gl_web
ref: main
ref: 250-change-language-fixes

dependency_overrides:
maplibre_gl_platform_interface:
Expand Down

0 comments on commit 48a41d5

Please sign in to comment.